blob: 06d67311e061c3d2070451d7c2c39e9366766da1 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var isRelative = require('is-relative');
4var isWindows = require('is-windows');
5
6/**
7 * Expose `isAbsolute`
8 */
9
10module.exports = isAbsolute;
11
12/**
13 * Returns true if a file path is absolute.
14 *
15 * @param {String} `fp`
16 * @return {Boolean}
17 */
18
19function 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
30isAbsolute.posix = function posixPath(fp) {
31 return fp.charAt(0) === '/';
32};
33
34/**
35 * Test windows paths.
36 */
37
38isAbsolute.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};