First functional version
This commit is contained in:
@ -19,28 +19,25 @@
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
|
||||
|
||||
"use strict";
|
||||
|
||||
// resolves . and .. elements in a path array with directory names there
|
||||
// must be no slashes or device names (c:\) in the array
|
||||
// (so also no leading and trailing slashes - it does not distinguish
|
||||
// relative and absolute paths)
|
||||
function normalizeArray(parts, allowAboveRoot) {
|
||||
function normalizeArray(parts: string[], allowAboveRoot: boolean) {
|
||||
var res = [];
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var p = parts[i];
|
||||
|
||||
// ignore empty parts
|
||||
if (!p || p === '.')
|
||||
continue;
|
||||
if (!p || p === ".") continue;
|
||||
|
||||
if (p === '..') {
|
||||
if (res.length && res[res.length - 1] !== '..') {
|
||||
if (p === "..") {
|
||||
if (res.length && res[res.length - 1] !== "..") {
|
||||
res.pop();
|
||||
} else if (allowAboveRoot) {
|
||||
res.push('..');
|
||||
res.push("..");
|
||||
}
|
||||
} else {
|
||||
res.push(p);
|
||||
@ -52,107 +49,91 @@ function normalizeArray(parts, allowAboveRoot) {
|
||||
|
||||
// returns an array with empty elements removed from either end of the input
|
||||
// array or the original array if no elements need to be removed
|
||||
function trimArray(arr) {
|
||||
function trimArray(arr: any[]) {
|
||||
var lastIndex = arr.length - 1;
|
||||
var start = 0;
|
||||
for (; start <= lastIndex; start++) {
|
||||
if (arr[start])
|
||||
break;
|
||||
if (arr[start]) break;
|
||||
}
|
||||
|
||||
var end = lastIndex;
|
||||
for (; end >= 0; end--) {
|
||||
if (arr[end])
|
||||
break;
|
||||
if (arr[end]) break;
|
||||
}
|
||||
|
||||
if (start === 0 && end === lastIndex)
|
||||
return arr;
|
||||
if (start > end)
|
||||
return [];
|
||||
if (start === 0 && end === lastIndex) return arr;
|
||||
if (start > end) return [];
|
||||
return arr.slice(start, end + 1);
|
||||
}
|
||||
|
||||
|
||||
// // Regex to split the tail part of the above into [*, dir, basename, ext]
|
||||
// const splitTailRe =
|
||||
// /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
|
||||
|
||||
// function normalizeUNCRoot(device) {
|
||||
// return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
|
||||
// }
|
||||
|
||||
// Split a filename into [root, dir, basename, ext], unix version
|
||||
// 'root' is just a slash, or nothing.
|
||||
const splitPathRe =
|
||||
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
|
||||
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
|
||||
|
||||
|
||||
function posixSplitPath(filename) {
|
||||
return splitPathRe.exec(filename).slice(1);
|
||||
function posixSplitPath(filename: string) {
|
||||
return splitPathRe.exec(filename)?.slice(1) as string[];
|
||||
}
|
||||
|
||||
export class Posix {
|
||||
static readonly sep = '/';
|
||||
static readonly delimiter = ':';
|
||||
static readonly sep = "/";
|
||||
static readonly delimiter = ":";
|
||||
|
||||
static resolve(...paths: string[]) {
|
||||
var resolvedPath = '',
|
||||
var resolvedPath = "",
|
||||
resolvedAbsolute = false;
|
||||
|
||||
for (var i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
||||
var path = (i >= 0) ? paths[i] : "/";
|
||||
var path = i >= 0 ? paths[i] : "/";
|
||||
|
||||
// Skip empty and invalid entries
|
||||
if (typeof path !== "string") {
|
||||
throw new TypeError('Arguments to path.resolve must be strings');
|
||||
throw new TypeError("Arguments to path.resolve must be strings");
|
||||
} else if (!path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
resolvedPath = path + '/' + resolvedPath;
|
||||
resolvedAbsolute = path[0] === '/';
|
||||
resolvedPath = path + "/" + resolvedPath;
|
||||
resolvedAbsolute = path[0] === "/";
|
||||
}
|
||||
|
||||
// At this point the path should be resolved to a full absolute path, but
|
||||
// handle relative paths to be safe (might happen when process.cwd() fails)
|
||||
|
||||
// Normalize the path
|
||||
resolvedPath = normalizeArray(resolvedPath.split('/'),
|
||||
!resolvedAbsolute).join('/');
|
||||
resolvedPath = normalizeArray(resolvedPath.split("/"), !resolvedAbsolute).join("/");
|
||||
|
||||
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
|
||||
return (resolvedAbsolute ? "/" : "") + resolvedPath || ".";
|
||||
}
|
||||
|
||||
static normalize(path: string) {
|
||||
var isAbsolute = Posix.isAbsolute(path),
|
||||
trailingSlash = path && path[path.length - 1] === '/';
|
||||
trailingSlash = path && path[path.length - 1] === "/";
|
||||
|
||||
// Normalize the path
|
||||
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
|
||||
path = normalizeArray(path.split("/"), !isAbsolute).join("/");
|
||||
|
||||
if (!path && !isAbsolute) {
|
||||
path = '.';
|
||||
path = ".";
|
||||
}
|
||||
if (path && trailingSlash) {
|
||||
path += '/';
|
||||
path += "/";
|
||||
}
|
||||
|
||||
return (isAbsolute ? '/' : '') + path;
|
||||
return (isAbsolute ? "/" : "") + path;
|
||||
}
|
||||
|
||||
static join(...paths: string[]) {
|
||||
var path = '';
|
||||
var path = "";
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
var segment = arguments[i];
|
||||
if (typeof segment !== "string") {
|
||||
throw new TypeError('Arguments to path.join must be strings');
|
||||
throw new TypeError("Arguments to path.join must be strings");
|
||||
}
|
||||
if (segment) {
|
||||
if (!path) {
|
||||
path += segment;
|
||||
} else {
|
||||
path += '/' + segment;
|
||||
path += "/" + segment;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -163,8 +144,8 @@ export class Posix {
|
||||
from = Posix.resolve(from).substr(1);
|
||||
to = Posix.resolve(to).substr(1);
|
||||
|
||||
var fromParts = trimArray(from.split('/'));
|
||||
var toParts = trimArray(to.split('/'));
|
||||
var fromParts = trimArray(from.split("/"));
|
||||
var toParts = trimArray(to.split("/"));
|
||||
|
||||
var length = Math.min(fromParts.length, toParts.length);
|
||||
var samePartsLength = length;
|
||||
@ -177,22 +158,22 @@ export class Posix {
|
||||
|
||||
var outputParts = [];
|
||||
for (var i = samePartsLength; i < fromParts.length; i++) {
|
||||
outputParts.push('..');
|
||||
outputParts.push("..");
|
||||
}
|
||||
|
||||
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
||||
|
||||
return outputParts.join('/');
|
||||
return outputParts.join("/");
|
||||
}
|
||||
|
||||
static dirname(path: string) {
|
||||
var result = posixSplitPath(path),
|
||||
var result = posixSplitPath(path) as string[],
|
||||
root = result[0],
|
||||
dir = result[1];
|
||||
|
||||
if (!root && !dir) {
|
||||
// No dirname whatsoever
|
||||
return '.';
|
||||
return ".";
|
||||
}
|
||||
|
||||
if (dir) {
|
||||
@ -218,52 +199,47 @@ export class Posix {
|
||||
|
||||
static format(pathObject: any) {
|
||||
if (typeof pathObject !== "object") {
|
||||
throw new TypeError(
|
||||
"Parameter 'pathObject' must be an object, not " + typeof pathObject
|
||||
);
|
||||
throw new TypeError("Parameter 'pathObject' must be an object, not " + typeof pathObject);
|
||||
}
|
||||
|
||||
var root = pathObject.root || '';
|
||||
var root = pathObject.root || "";
|
||||
|
||||
if (typeof root !== "string") {
|
||||
throw new TypeError(
|
||||
"'pathObject.root' must be a string or undefined, not " +
|
||||
typeof pathObject.root
|
||||
"'pathObject.root' must be a string or undefined, not " + typeof pathObject.root
|
||||
);
|
||||
}
|
||||
|
||||
var dir = pathObject.dir ? pathObject.dir + Posix.sep : '';
|
||||
var base = pathObject.base || '';
|
||||
var dir = pathObject.dir ? pathObject.dir + Posix.sep : "";
|
||||
var base = pathObject.base || "";
|
||||
return dir + base;
|
||||
}
|
||||
|
||||
static parse(pathString: string) {
|
||||
if (typeof pathString !== "string") {
|
||||
throw new TypeError(
|
||||
"Parameter 'pathString' must be a string, not " + typeof pathString
|
||||
);
|
||||
throw new TypeError("Parameter 'pathString' must be a string, not " + typeof pathString);
|
||||
}
|
||||
var allParts = posixSplitPath(pathString);
|
||||
if (!allParts || allParts.length !== 4) {
|
||||
throw new TypeError("Invalid path '" + pathString + "'");
|
||||
}
|
||||
allParts[1] = allParts[1] || '';
|
||||
allParts[2] = allParts[2] || '';
|
||||
allParts[3] = allParts[3] || '';
|
||||
allParts[1] = allParts[1] || "";
|
||||
allParts[2] = allParts[2] || "";
|
||||
allParts[3] = allParts[3] || "";
|
||||
|
||||
return {
|
||||
root: allParts[0],
|
||||
dir: allParts[0] + allParts[1].slice(0, -1),
|
||||
base: allParts[2],
|
||||
ext: allParts[3],
|
||||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
|
||||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length),
|
||||
};
|
||||
}
|
||||
|
||||
static isAbsolute(path: string) {
|
||||
return path.charAt(0) === '/';
|
||||
return path.charAt(0) === "/";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const Path = Posix;
|
||||
export default Path;
|
||||
export default Path;
|
||||
|
Reference in New Issue
Block a user