tar
Version:
tar for JavaScript
67 lines • 2.62 kB
JavaScript
;
// The path rules of Windows that the extraction needs. Each function
// takes a path with `/` or `\` separators.
Object.defineProperty(exports, "__esModule", { value: true });
exports.win32Dirname = exports.win32Resolve = exports.win32IsAbsolute = exports.win32Root = void 0;
const path_js_1 = require("./path.js");
const isSep = (code) => code === 47 || code === 92;
const isDrive = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
/**
* The root of a path: a slash, a drive such as `c:` or `c:/`, or a
* UNC root such as `//host/share/`. The same result as the `root` of
* `path.win32.parse`.
*/
const win32Root = (path) => {
const len = path.length;
let end = 0;
if (isSep(path.charCodeAt(0))) {
end = 1;
if (isSep(path.charCodeAt(1))) {
// `//host/share` and what follows it
let j = 2;
let last = j;
while (j < len && !isSep(path.charCodeAt(j)))
j++;
if (j < len && j !== last) {
last = j;
while (j < len && isSep(path.charCodeAt(j)))
j++;
if (j < len && j !== last) {
last = j;
while (j < len && !isSep(path.charCodeAt(j)))
j++;
if (j === len)
end = j;
else if (j !== last)
end = j + 1;
}
}
}
}
else if (isDrive(path.charCodeAt(0)) && path.charCodeAt(1) === 58) {
end = isSep(path.charCodeAt(2)) ? 3 : 2;
}
return path.slice(0, end);
};
exports.win32Root = win32Root;
/** True for a path with a root that is not relative to a drive. */
const win32IsAbsolute = (path) => isSep(path.charCodeAt(0)) || (0, exports.win32Root)(path).length === 3;
exports.win32IsAbsolute = win32IsAbsolute;
/**
* Resolve `path` from the absolute path `base`. Both have `/`
* separators.
*/
const win32Resolve = (base, path) => {
const full = (0, exports.win32IsAbsolute)(path) ? path : base + '/' + path;
const root = (0, exports.win32Root)(full);
return root + (0, path_js_1.resolve)('/', full.slice(root.length)).slice(1);
};
exports.win32Resolve = win32Resolve;
/** The parent directory of an absolute path with `/` separators. */
const win32Dirname = (path) => {
const root = (0, exports.win32Root)(path);
const dir = (0, path_js_1.dirname)(path.slice(root.length));
return dir === '.' ? root : root + dir;
};
exports.win32Dirname = win32Dirname;
//# sourceMappingURL=win32-path.js.map