First Commit
This commit is contained in:
14
src/helper/hex.ts
Normal file
14
src/helper/hex.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// Pre-Init
|
||||
const LUT_HEX_4b = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
||||
const LUT_HEX_8b = new Array(0x100);
|
||||
for (let n = 0; n < 0x100; n++) {
|
||||
LUT_HEX_8b[n] = `${LUT_HEX_4b[(n >>> 4) & 0xF]}${LUT_HEX_4b[n & 0xF]}`;
|
||||
}
|
||||
// End Pre-Init
|
||||
export function toHex(buffer: Uint8Array): string {
|
||||
let out = '';
|
||||
for (let idx = 0, edx = buffer.length; idx < edx; idx++) {
|
||||
out += LUT_HEX_8b[buffer[idx]];
|
||||
}
|
||||
return out;
|
||||
}
|
269
src/helper/path.ts
Normal file
269
src/helper/path.ts
Normal file
@ -0,0 +1,269 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'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) {
|
||||
var res = [];
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var p = parts[i];
|
||||
|
||||
// ignore empty parts
|
||||
if (!p || p === '.')
|
||||
continue;
|
||||
|
||||
if (p === '..') {
|
||||
if (res.length && res[res.length - 1] !== '..') {
|
||||
res.pop();
|
||||
} else if (allowAboveRoot) {
|
||||
res.push('..');
|
||||
}
|
||||
} else {
|
||||
res.push(p);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
var lastIndex = arr.length - 1;
|
||||
var start = 0;
|
||||
for (; start <= lastIndex; start++) {
|
||||
if (arr[start])
|
||||
break;
|
||||
}
|
||||
|
||||
var end = lastIndex;
|
||||
for (; end >= 0; end--) {
|
||||
if (arr[end])
|
||||
break;
|
||||
}
|
||||
|
||||
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}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
|
||||
|
||||
|
||||
function posixSplitPath(filename) {
|
||||
return splitPathRe.exec(filename).slice(1);
|
||||
}
|
||||
|
||||
export class Posix {
|
||||
static readonly sep = '/';
|
||||
static readonly delimiter = ':';
|
||||
|
||||
static resolve(...paths: string[]) {
|
||||
var resolvedPath = '',
|
||||
resolvedAbsolute = false;
|
||||
|
||||
for (var i = paths.length - 1; i >= -1 && !resolvedAbsolute; 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');
|
||||
} else if (!path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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('/');
|
||||
|
||||
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
|
||||
}
|
||||
|
||||
static normalize(path: string) {
|
||||
var isAbsolute = Posix.isAbsolute(path),
|
||||
trailingSlash = path && path[path.length - 1] === '/';
|
||||
|
||||
// Normalize the path
|
||||
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
|
||||
|
||||
if (!path && !isAbsolute) {
|
||||
path = '.';
|
||||
}
|
||||
if (path && trailingSlash) {
|
||||
path += '/';
|
||||
}
|
||||
|
||||
return (isAbsolute ? '/' : '') + path;
|
||||
}
|
||||
|
||||
static join(...paths: string[]) {
|
||||
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');
|
||||
}
|
||||
if (segment) {
|
||||
if (!path) {
|
||||
path += segment;
|
||||
} else {
|
||||
path += '/' + segment;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Posix.normalize(path);
|
||||
}
|
||||
|
||||
static relative(from: string, to: string) {
|
||||
from = Posix.resolve(from).substr(1);
|
||||
to = Posix.resolve(to).substr(1);
|
||||
|
||||
var fromParts = trimArray(from.split('/'));
|
||||
var toParts = trimArray(to.split('/'));
|
||||
|
||||
var length = Math.min(fromParts.length, toParts.length);
|
||||
var samePartsLength = length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (fromParts[i] !== toParts[i]) {
|
||||
samePartsLength = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var outputParts = [];
|
||||
for (var i = samePartsLength; i < fromParts.length; i++) {
|
||||
outputParts.push('..');
|
||||
}
|
||||
|
||||
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
||||
|
||||
return outputParts.join('/');
|
||||
}
|
||||
|
||||
static dirname(path: string) {
|
||||
var result = posixSplitPath(path),
|
||||
root = result[0],
|
||||
dir = result[1];
|
||||
|
||||
if (!root && !dir) {
|
||||
// No dirname whatsoever
|
||||
return '.';
|
||||
}
|
||||
|
||||
if (dir) {
|
||||
// It has a dirname, strip trailing slash
|
||||
dir = dir.substr(0, dir.length - 1);
|
||||
}
|
||||
|
||||
return root + dir;
|
||||
}
|
||||
|
||||
static basename(path: string, ext?: string) {
|
||||
var f = posixSplitPath(path)[2];
|
||||
// TODO: make this comparison case-insensitive on windows?
|
||||
if (ext && f.substr(-1 * ext.length) === ext) {
|
||||
f = f.substr(0, f.length - ext.length);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
static extname(path: string) {
|
||||
return posixSplitPath(path)[3];
|
||||
}
|
||||
|
||||
static format(pathObject: any) {
|
||||
if (typeof pathObject !== "object") {
|
||||
throw new TypeError(
|
||||
"Parameter 'pathObject' must be an object, not " + typeof pathObject
|
||||
);
|
||||
}
|
||||
|
||||
var root = pathObject.root || '';
|
||||
|
||||
if (typeof root !== "string") {
|
||||
throw new TypeError(
|
||||
"'pathObject.root' must be a string or undefined, not " +
|
||||
typeof pathObject.root
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
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] || '';
|
||||
|
||||
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)
|
||||
};
|
||||
}
|
||||
|
||||
static isAbsolute(path: string) {
|
||||
return path.charAt(0) === '/';
|
||||
}
|
||||
};
|
||||
|
||||
export const Path = Posix;
|
||||
export default Path;
|
Reference in New Issue
Block a user