Debian Codegraph @

path2/posix/resolve.js

raw
1 'use strict';
2
3 var normalizeArray = require('../_normalize-array')
4 , cwd = require('./_cwd');
5
6 module.exports = function () {
7 var resolvedPath = '', resolvedAbsolute = false, i, path;
8
9 for (i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
10 path = (i >= 0) ? arguments[i] : cwd;
11
12 // Skip empty and invalid entries
13 if (typeof path !== 'string') {
14 throw new TypeError('Arguments to path.resolve must be strings');
15 }
16 if (!path) continue;
17
18 resolvedPath = path + '/' + resolvedPath;
19 resolvedAbsolute = path.charAt(0) === '/';
20 }
21
22 // At this point the path should be resolved to a full absolute path, but
23 // handle relative paths to be safe (might happen when process.cwd() fails)
24
25 // Normalize the path
26 resolvedPath = normalizeArray(resolvedPath.split('/').filter(function (p) {
27 return !!p;
28 }), !resolvedAbsolute).join('/');
29
30 return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
31 };