| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var isRelative = require('is-relative'); |
| 4 | var isWindows = require('is-windows'); |
| 5 | |
| 6 | /** |
| 7 | * Expose `isAbsolute` |
| 8 | */ |
| 9 | |
| 10 | module.exports = isAbsolute; |
| 11 | |
| 12 | /** |
| 13 | * Returns true if a file path is absolute. |
| 14 | * |
| 15 | * @param {String} `fp` |
| 16 | * @return {Boolean} |
| 17 | */ |
| 18 | |
| 19 | function isAbsolute(fp) { |
| 20 | if (typeof fp !== 'string') { |
| 21 | throw new TypeError('isAbsolute expects a string.'); |
| 22 | } |
| 23 | return isWindows() ? isAbsolute.win32(fp) : isAbsolute.posix(fp); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Test posix paths. |
| 28 | */ |
| 29 | |
| 30 | isAbsolute.posix = function posixPath(fp) { |
| 31 | return fp.charAt(0) === '/'; |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * Test windows paths. |
| 36 | */ |
| 37 | |
| 38 | isAbsolute.win32 = function win32(fp) { |
| 39 | if (/[a-z]/i.test(fp.charAt(0)) && fp.charAt(1) === ':' && fp.charAt(2) === '\\') { |
| 40 | return true; |
| 41 | } |
| 42 | // Microsoft Azure absolute filepath |
| 43 | if (fp.slice(0, 2) === '\\\\') { |
| 44 | return true; |
| 45 | } |
| 46 | return !isRelative(fp); |
| 47 | }; |