Skip to content

Instantly share code, notes, and snippets.

@enten
Last active August 8, 2017 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enten/97b5f9ee266b0820a7406655ead59651 to your computer and use it in GitHub Desktop.
Save enten/97b5f9ee266b0820a7406655ead59651 to your computer and use it in GitHub Desktop.
Determines if filename is a relative path.
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
@enten
Copy link
Author

enten commented Aug 8, 2017

This function can't be used to detect a relative module ID.

To do that, the test typeof moduleId === 'string' && moduleId[0] === '.' is enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment