Last active
August 8, 2017 23:38
-
-
Save enten/97b5f9ee266b0820a7406655ead59651 to your computer and use it in GitHub Desktop.
Determines if filename is a relative path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {sep: platformSep} = require('path') | |
const {inspect} = require('util') | |
const POSIX_SEP = '/' | |
const WIN32_SEP = '\\' | |
const PATH_SEPARATORS = { | |
'aix': POSIX_SEP, | |
'darwin': POSIX_SEP, | |
'freebsd': POSIX_SEP, | |
'linux': POSIX_SEP, | |
'openbsd': POSIX_SEP, | |
'posix': POSIX_SEP, | |
'sunos': POSIX_SEP, | |
'unix': POSIX_SEP, | |
'win': WIN32_SEP, | |
'win32': WIN32_SEP, | |
} | |
function isRelative (filename, sep) { | |
if (typeof filename !== 'string') { | |
throw new TypeError('Path must be a string. Received ' + inspect(filename)) | |
} | |
sep = sep ? PATH_SEPARATORS[sep] || POSIX_SEP : platformSep | |
filename = filename[0] === ' ' ? filename.trim() : filename | |
return filename[0] === sep ? false : sep !== POSIX_SEP ? filename[1] !== ':' : true | |
} | |
module.exports = isRelative |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function can't be used to detect a relative module ID.
To do that, the test
typeof moduleId === 'string' && moduleId[0] === '.'
is enough.