tar
Version:
tar for JavaScript
60 lines • 2.25 kB
JavaScript
// The path rules of Windows that the extraction needs. Each function
// takes a path with `/` or `\` separators.
import { dirname, resolve } from './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`.
*/
export 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);
};
/** True for a path with a root that is not relative to a drive. */
export const win32IsAbsolute = (path) => isSep(path.charCodeAt(0)) || win32Root(path).length === 3;
/**
* Resolve `path` from the absolute path `base`. Both have `/`
* separators.
*/
export const win32Resolve = (base, path) => {
const full = win32IsAbsolute(path) ? path : base + '/' + path;
const root = win32Root(full);
return root + resolve('/', full.slice(root.length)).slice(1);
};
/** The parent directory of an absolute path with `/` separators. */
export const win32Dirname = (path) => {
const root = win32Root(path);
const dir = dirname(path.slice(root.length));
return dir === '.' ? root : root + dir;
};
//# sourceMappingURL=win32-path.js.map