path-scurry
Advanced tools
Comparing version 1.6.4 to 1.7.0
@@ -73,2 +73,3 @@ /// <reference types="node" /> | ||
relative?: string; | ||
relativePosix?: string; | ||
parent?: PathBase; | ||
@@ -243,2 +244,9 @@ /** | ||
/** | ||
* The relative path from the cwd, using / as the path separator. | ||
* If it does not share an ancestor with | ||
* the cwd, then this ends up being equivalent to the fullpathPosix() | ||
* On posix systems, this is identical to relative(). | ||
*/ | ||
relativePosix(): string; | ||
/** | ||
* The fully resolved path string for this Path entry | ||
@@ -248,2 +256,9 @@ */ | ||
/** | ||
* On platforms other than windows, this is identical to fullpath. | ||
* | ||
* On windows, this is overridden to return the forward-slash form of the | ||
* full UNC path. | ||
*/ | ||
fullpathPosix(): string; | ||
/** | ||
* Is the Path of an unknown type? | ||
@@ -624,2 +639,14 @@ * | ||
/** | ||
* Resolve one or more path strings to a resolved string, returning | ||
* the posix path. Identical to .resolve() on posix systems, but on | ||
* windows will return a forward-slash separated UNC path. | ||
* | ||
* Same interface as require('path').resolve. | ||
* | ||
* Much faster than path.resolve() when called multiple times for the same | ||
* path, because the resolved Path objects are cached. Much slower | ||
* otherwise. | ||
*/ | ||
resolvePosix(...paths: string[]): string; | ||
/** | ||
* find the relative path from the cwd to the supplied path string or entry | ||
@@ -629,2 +656,7 @@ */ | ||
/** | ||
* find the relative path from the cwd to the supplied path string or | ||
* entry, using / as the path delimiter, even on Windows. | ||
*/ | ||
relativePosix(entry?: PathBase | string): string; | ||
/** | ||
* Return the basename for the provided string or Path object | ||
@@ -631,0 +663,0 @@ */ |
@@ -287,3 +287,5 @@ "use strict"; | ||
#fullpath; | ||
#fullpathPosix; | ||
#relative; | ||
#relativePosix; | ||
#type; | ||
@@ -309,2 +311,3 @@ #children; | ||
this.#relative = opts.relative; | ||
this.#relativePosix = opts.relativePosix; | ||
this.parent = opts.parent; | ||
@@ -430,4 +433,2 @@ if (this.parent) { | ||
*/ | ||
// TODO: instead of taking a param here, set it to '' in the constructor | ||
// for the CWD, and set it to this.name for any roots. | ||
relative() { | ||
@@ -447,2 +448,22 @@ if (this.#relative !== undefined) { | ||
/** | ||
* The relative path from the cwd, using / as the path separator. | ||
* If it does not share an ancestor with | ||
* the cwd, then this ends up being equivalent to the fullpathPosix() | ||
* On posix systems, this is identical to relative(). | ||
*/ | ||
relativePosix() { | ||
if (this.sep === '/') | ||
return this.relative(); | ||
if (this.#relativePosix !== undefined) | ||
return this.#relativePosix; | ||
const name = this.name; | ||
const p = this.parent; | ||
if (!p) { | ||
return (this.#relativePosix = this.fullpathPosix()); | ||
} | ||
const pv = p.relativePosix(); | ||
const rp = pv + (!pv || !p.parent ? '' : '/') + name; | ||
return (this.#relativePosix = rp); | ||
} | ||
/** | ||
* The fully resolved path string for this Path entry | ||
@@ -464,2 +485,27 @@ */ | ||
/** | ||
* On platforms other than windows, this is identical to fullpath. | ||
* | ||
* On windows, this is overridden to return the forward-slash form of the | ||
* full UNC path. | ||
*/ | ||
fullpathPosix() { | ||
if (this.#fullpathPosix !== undefined) | ||
return this.#fullpathPosix; | ||
if (this.sep === '/') | ||
return (this.#fullpathPosix = this.fullpath()); | ||
if (!this.parent) { | ||
const p = this.fullpath().replace(/\\/g, '/'); | ||
if (/^[a-z]:\//i.test(p)) { | ||
return (this.#fullpathPosix = `//?/${p}`); | ||
} | ||
else { | ||
return (this.#fullpathPosix = p); | ||
} | ||
} | ||
const p = this.parent; | ||
const pfpp = p.fullpathPosix(); | ||
const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name; | ||
return (this.#fullpathPosix = fpp); | ||
} | ||
/** | ||
* Is the Path of an unknown type? | ||
@@ -1212,2 +1258,3 @@ * | ||
#resolveCache; | ||
#resolvePosixCache; | ||
#children; | ||
@@ -1239,2 +1286,3 @@ /** | ||
this.#resolveCache = new ResolveCache(); | ||
this.#resolvePosixCache = new ResolveCache(); | ||
this.#children = new ChildrenCache(childrenCacheSize); | ||
@@ -1260,4 +1308,6 @@ const split = cwdPath.substring(this.rootPath.length).split(sep); | ||
for (const part of split) { | ||
const l = len--; | ||
prev = prev.child(part, { | ||
relative: new Array(len--).fill('..').join(joinSep), | ||
relative: new Array(l).fill('..').join(joinSep), | ||
relativePosix: new Array(l).fill('..').join('/'), | ||
fullpath: (abs += (sawFirst ? '' : joinSep) + part), | ||
@@ -1318,2 +1368,34 @@ }); | ||
/** | ||
* Resolve one or more path strings to a resolved string, returning | ||
* the posix path. Identical to .resolve() on posix systems, but on | ||
* windows will return a forward-slash separated UNC path. | ||
* | ||
* Same interface as require('path').resolve. | ||
* | ||
* Much faster than path.resolve() when called multiple times for the same | ||
* path, because the resolved Path objects are cached. Much slower | ||
* otherwise. | ||
*/ | ||
resolvePosix(...paths) { | ||
// first figure out the minimum number of paths we have to test | ||
// we always start at cwd, but any absolutes will bump the start | ||
let r = ''; | ||
for (let i = paths.length - 1; i >= 0; i--) { | ||
const p = paths[i]; | ||
if (!p || p === '.') | ||
continue; | ||
r = r ? `${p}/${r}` : p; | ||
if (this.isAbsolute(p)) { | ||
break; | ||
} | ||
} | ||
const cached = this.#resolvePosixCache.get(r); | ||
if (cached !== undefined) { | ||
return cached; | ||
} | ||
const result = this.cwd.resolve(r).fullpathPosix(); | ||
this.#resolvePosixCache.set(r, result); | ||
return result; | ||
} | ||
/** | ||
* find the relative path from the cwd to the supplied path string or entry | ||
@@ -1328,2 +1410,12 @@ */ | ||
/** | ||
* find the relative path from the cwd to the supplied path string or | ||
* entry, using / as the path delimiter, even on Windows. | ||
*/ | ||
relativePosix(entry = this.cwd) { | ||
if (typeof entry === 'string') { | ||
entry = this.cwd.resolve(entry); | ||
} | ||
return entry.relativePosix(); | ||
} | ||
/** | ||
* Return the basename for the provided string or Path object | ||
@@ -1330,0 +1422,0 @@ */ |
@@ -73,2 +73,3 @@ /// <reference types="node" /> | ||
relative?: string; | ||
relativePosix?: string; | ||
parent?: PathBase; | ||
@@ -243,2 +244,9 @@ /** | ||
/** | ||
* The relative path from the cwd, using / as the path separator. | ||
* If it does not share an ancestor with | ||
* the cwd, then this ends up being equivalent to the fullpathPosix() | ||
* On posix systems, this is identical to relative(). | ||
*/ | ||
relativePosix(): string; | ||
/** | ||
* The fully resolved path string for this Path entry | ||
@@ -248,2 +256,9 @@ */ | ||
/** | ||
* On platforms other than windows, this is identical to fullpath. | ||
* | ||
* On windows, this is overridden to return the forward-slash form of the | ||
* full UNC path. | ||
*/ | ||
fullpathPosix(): string; | ||
/** | ||
* Is the Path of an unknown type? | ||
@@ -624,2 +639,14 @@ * | ||
/** | ||
* Resolve one or more path strings to a resolved string, returning | ||
* the posix path. Identical to .resolve() on posix systems, but on | ||
* windows will return a forward-slash separated UNC path. | ||
* | ||
* Same interface as require('path').resolve. | ||
* | ||
* Much faster than path.resolve() when called multiple times for the same | ||
* path, because the resolved Path objects are cached. Much slower | ||
* otherwise. | ||
*/ | ||
resolvePosix(...paths: string[]): string; | ||
/** | ||
* find the relative path from the cwd to the supplied path string or entry | ||
@@ -629,2 +656,7 @@ */ | ||
/** | ||
* find the relative path from the cwd to the supplied path string or | ||
* entry, using / as the path delimiter, even on Windows. | ||
*/ | ||
relativePosix(entry?: PathBase | string): string; | ||
/** | ||
* Return the basename for the provided string or Path object | ||
@@ -631,0 +663,0 @@ */ |
@@ -259,3 +259,5 @@ import { LRUCache } from 'lru-cache'; | ||
#fullpath; | ||
#fullpathPosix; | ||
#relative; | ||
#relativePosix; | ||
#type; | ||
@@ -281,2 +283,3 @@ #children; | ||
this.#relative = opts.relative; | ||
this.#relativePosix = opts.relativePosix; | ||
this.parent = opts.parent; | ||
@@ -402,4 +405,2 @@ if (this.parent) { | ||
*/ | ||
// TODO: instead of taking a param here, set it to '' in the constructor | ||
// for the CWD, and set it to this.name for any roots. | ||
relative() { | ||
@@ -419,2 +420,22 @@ if (this.#relative !== undefined) { | ||
/** | ||
* The relative path from the cwd, using / as the path separator. | ||
* If it does not share an ancestor with | ||
* the cwd, then this ends up being equivalent to the fullpathPosix() | ||
* On posix systems, this is identical to relative(). | ||
*/ | ||
relativePosix() { | ||
if (this.sep === '/') | ||
return this.relative(); | ||
if (this.#relativePosix !== undefined) | ||
return this.#relativePosix; | ||
const name = this.name; | ||
const p = this.parent; | ||
if (!p) { | ||
return (this.#relativePosix = this.fullpathPosix()); | ||
} | ||
const pv = p.relativePosix(); | ||
const rp = pv + (!pv || !p.parent ? '' : '/') + name; | ||
return (this.#relativePosix = rp); | ||
} | ||
/** | ||
* The fully resolved path string for this Path entry | ||
@@ -436,2 +457,27 @@ */ | ||
/** | ||
* On platforms other than windows, this is identical to fullpath. | ||
* | ||
* On windows, this is overridden to return the forward-slash form of the | ||
* full UNC path. | ||
*/ | ||
fullpathPosix() { | ||
if (this.#fullpathPosix !== undefined) | ||
return this.#fullpathPosix; | ||
if (this.sep === '/') | ||
return (this.#fullpathPosix = this.fullpath()); | ||
if (!this.parent) { | ||
const p = this.fullpath().replace(/\\/g, '/'); | ||
if (/^[a-z]:\//i.test(p)) { | ||
return (this.#fullpathPosix = `//?/${p}`); | ||
} | ||
else { | ||
return (this.#fullpathPosix = p); | ||
} | ||
} | ||
const p = this.parent; | ||
const pfpp = p.fullpathPosix(); | ||
const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name; | ||
return (this.#fullpathPosix = fpp); | ||
} | ||
/** | ||
* Is the Path of an unknown type? | ||
@@ -1181,2 +1227,3 @@ * | ||
#resolveCache; | ||
#resolvePosixCache; | ||
#children; | ||
@@ -1208,2 +1255,3 @@ /** | ||
this.#resolveCache = new ResolveCache(); | ||
this.#resolvePosixCache = new ResolveCache(); | ||
this.#children = new ChildrenCache(childrenCacheSize); | ||
@@ -1229,4 +1277,6 @@ const split = cwdPath.substring(this.rootPath.length).split(sep); | ||
for (const part of split) { | ||
const l = len--; | ||
prev = prev.child(part, { | ||
relative: new Array(len--).fill('..').join(joinSep), | ||
relative: new Array(l).fill('..').join(joinSep), | ||
relativePosix: new Array(l).fill('..').join('/'), | ||
fullpath: (abs += (sawFirst ? '' : joinSep) + part), | ||
@@ -1287,2 +1337,34 @@ }); | ||
/** | ||
* Resolve one or more path strings to a resolved string, returning | ||
* the posix path. Identical to .resolve() on posix systems, but on | ||
* windows will return a forward-slash separated UNC path. | ||
* | ||
* Same interface as require('path').resolve. | ||
* | ||
* Much faster than path.resolve() when called multiple times for the same | ||
* path, because the resolved Path objects are cached. Much slower | ||
* otherwise. | ||
*/ | ||
resolvePosix(...paths) { | ||
// first figure out the minimum number of paths we have to test | ||
// we always start at cwd, but any absolutes will bump the start | ||
let r = ''; | ||
for (let i = paths.length - 1; i >= 0; i--) { | ||
const p = paths[i]; | ||
if (!p || p === '.') | ||
continue; | ||
r = r ? `${p}/${r}` : p; | ||
if (this.isAbsolute(p)) { | ||
break; | ||
} | ||
} | ||
const cached = this.#resolvePosixCache.get(r); | ||
if (cached !== undefined) { | ||
return cached; | ||
} | ||
const result = this.cwd.resolve(r).fullpathPosix(); | ||
this.#resolvePosixCache.set(r, result); | ||
return result; | ||
} | ||
/** | ||
* find the relative path from the cwd to the supplied path string or entry | ||
@@ -1297,2 +1379,12 @@ */ | ||
/** | ||
* find the relative path from the cwd to the supplied path string or | ||
* entry, using / as the path delimiter, even on Windows. | ||
*/ | ||
relativePosix(entry = this.cwd) { | ||
if (typeof entry === 'string') { | ||
entry = this.cwd.resolve(entry); | ||
} | ||
return entry.relativePosix(); | ||
} | ||
/** | ||
* Return the basename for the provided string or Path object | ||
@@ -1299,0 +1391,0 @@ */ |
{ | ||
"name": "path-scurry", | ||
"version": "1.6.4", | ||
"version": "1.7.0", | ||
"description": "walk paths fast and efficiently", | ||
@@ -5,0 +5,0 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://blog.izs.me)", |
@@ -237,3 +237,3 @@ # path-scurry | ||
- `fs` An object that will be used to override the default `fs` | ||
methods. Any methods that are not overridden will use Node's | ||
methods. Any methods that are not overridden will use Node's | ||
built-in implementations. | ||
@@ -357,2 +357,13 @@ | ||
#### `pw.resolvePosix(...paths: string[])` | ||
Caching `path.resolve()`, but always using posix style paths. | ||
This is identical to `pw.resolve(...paths)` on posix systems (ie, | ||
everywhere except Windows). | ||
On Windows, it returns the full absolute UNC path using `/` | ||
separators. Ie, instead of `'C:\\foo\\bar`, it would return | ||
`//?/C:/foo/bar`. | ||
#### `pw.relative(path: string | Path): string` | ||
@@ -366,2 +377,19 @@ | ||
#### `pw.relativePosix(path: string | Path): string` | ||
Return the relative path from the PathWalker cwd to the supplied | ||
path string or entry, using `/` path separators. | ||
If the nearest common ancestor is the root, then an absolute path | ||
is returned. | ||
On posix platforms (ie, all platforms except Windows), this is | ||
identical to `pw.relative(path)`. | ||
On Windows systems, it returns the resulting string as a | ||
`/`-delimited path. If an absolute path is returned (because the | ||
target does not share a common ancestor with `pw.cwd`), then a | ||
full absolute UNC path will be returned. Ie, instead of | ||
`'C:\\foo\\bar`, it would return `//?/C:/foo/bar`. | ||
#### `pw.basename(path: string | Path): string` | ||
@@ -470,5 +498,5 @@ | ||
**Important**: *always* test the path name against any test | ||
**Important**: _always_ test the path name against any test | ||
string using the `isNamed` method, and not by directly comparing | ||
this string. Otherwise, unicode path strings that the system | ||
this string. Otherwise, unicode path strings that the system | ||
sees as identical will not be properly treated as the same path, | ||
@@ -479,3 +507,3 @@ leading to incorrect behavior and possible security issues. | ||
Return true if the path is a match for the given path name. This | ||
Return true if the path is a match for the given path name. This | ||
handles case sensitivity and unicode normalization. | ||
@@ -499,2 +527,11 @@ | ||
#### `path.fullpathPosix()` | ||
The fully resolved path to the entry, using `/` separators. | ||
On posix systems, this is identical to `path.fullpath()`. On | ||
windows, this will return a fully resolved absolute UNC path | ||
using `/` separators. Eg, instead of `'C:\\foo\\bar'`, it will | ||
return `'//?/C:/foo/bar'`. | ||
#### `path.isFile()`, `path.isDirectory()`, etc. | ||
@@ -522,2 +559,19 @@ | ||
#### `path.relativePosix(): string` | ||
Return the relative path from the PathWalker cwd to the supplied | ||
path string or entry, using `/` path separators. | ||
If the nearest common ancestor is the root, then an absolute path | ||
is returned. | ||
On posix platforms (ie, all platforms except Windows), this is | ||
identical to `pw.relative(path)`. | ||
On Windows systems, it returns the resulting string as a | ||
`/`-delimited path. If an absolute path is returned (because the | ||
target does not share a common ancestor with `pw.cwd`), then a | ||
full absolute UNC path will be returned. Ie, instead of | ||
`'C:\\foo\\bar`, it would return `//?/C:/foo/bar`. | ||
#### `async path.readdir()` | ||
@@ -524,0 +578,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
514567
6043
614