nice-path 2.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.node-version +1 -1
- package/.npm-version +1 -0
- package/README.md +4 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +54 -8
- package/package.json +5 -5
package/.node-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v24.3.0
|
package/.npm-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
11.4.2
|
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
`nice-path` provides a class that represents a filesystem path (POSIX-style or Win32-style), which has various nice methods on it that make it easy to work with. It can be used as a replacement for the Node.js `path` builtin module, where you pass around Path objects and stringify them before use, rather than passing around strings.
|
|
4
4
|
|
|
5
|
+
> `nice-path`'s code is derived from [yavascript](https://github.com/suchipi/yavascript).
|
|
6
|
+
|
|
5
7
|
## Example
|
|
6
8
|
|
|
7
9
|
```ts
|
|
@@ -192,6 +194,8 @@ The `isAbsolute` method returns whether the target Path is an absolute path; tha
|
|
|
192
194
|
|
|
193
195
|
The `clone` method makes a second Path object containing the same segments and separator as the target.
|
|
194
196
|
|
|
197
|
+
Note that although the new Path has the same segments as the target Path, it doesn't use the same Array instance.
|
|
198
|
+
|
|
195
199
|
#### `relativeTo(dir, options?): Path` (instance method of Path)
|
|
196
200
|
|
|
197
201
|
The `relativeTo` method expresses the target path as a relative path, relative to the `dir` argument.
|
package/dist/index.d.ts
CHANGED
|
@@ -34,8 +34,21 @@ export declare class Path {
|
|
|
34
34
|
separator: string;
|
|
35
35
|
/** Create a new Path object using the provided input(s). */
|
|
36
36
|
constructor(...inputs: Array<string | Path | Array<string | Path>>);
|
|
37
|
+
/**
|
|
38
|
+
* Create a new Path object using the provided segments and, optionally,
|
|
39
|
+
* separator.
|
|
40
|
+
*
|
|
41
|
+
* NOTE: this doesn't set the `segments` directly; it passes them through a
|
|
42
|
+
* filtering step first, to remove any double-slashes or etc. To set the
|
|
43
|
+
* `.segments` directly, use {@link fromRaw}.
|
|
44
|
+
*/
|
|
45
|
+
static from(segments: Array<string>, separator?: string): Path;
|
|
37
46
|
/**
|
|
38
47
|
* Create a new Path object using the provided segments and separator.
|
|
48
|
+
*
|
|
49
|
+
* NOTE: this method doesn't do any sort of validation on `segments`; as such,
|
|
50
|
+
* it can be used to construct an invalid Path object. Consider using
|
|
51
|
+
* {@link from} instead.
|
|
39
52
|
*/
|
|
40
53
|
static fromRaw(segments: Array<string>, separator: string): Path;
|
|
41
54
|
/**
|
|
@@ -147,6 +160,8 @@ export declare class Path {
|
|
|
147
160
|
*
|
|
148
161
|
* @param value - What should be replaced
|
|
149
162
|
* @param replacement - What it should be replaced with
|
|
163
|
+
*
|
|
164
|
+
* NOTE: to remove segments, use an empty Array for `replacement`.
|
|
150
165
|
*/
|
|
151
166
|
replace(value: string | Path | Array<string | Path>, replacement: string | Path | Array<string | Path>): Path;
|
|
152
167
|
/**
|
|
@@ -164,4 +179,16 @@ export declare class Path {
|
|
|
164
179
|
* @param replacement - The new final segment(s) for the returned Path
|
|
165
180
|
*/
|
|
166
181
|
replaceLast(replacement: string | Path | Array<string | Path>): Path;
|
|
182
|
+
/**
|
|
183
|
+
* Return a boolean indicating whether this Path has the same separator and
|
|
184
|
+
* segments as another Path.
|
|
185
|
+
*
|
|
186
|
+
* To check only segments and not separator, use {@link Path.prototype.hasEqualSegments}.
|
|
187
|
+
*/
|
|
188
|
+
equals(other: string | Path | Array<string | Path>): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Return a boolean indicating whether this Path has the same segments as
|
|
191
|
+
* another Path. **Separator is not checked; use {@link Path.prototype.equals} for that.**
|
|
192
|
+
*/
|
|
193
|
+
hasEqualSegments(other: string | Path | Array<string | Path>): boolean;
|
|
167
194
|
}
|
package/dist/index.js
CHANGED
|
@@ -79,12 +79,31 @@ class Path {
|
|
|
79
79
|
this.segments = Path.splitToSegments(parts);
|
|
80
80
|
this.separator = Path.detectSeparator(parts, "/");
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a new Path object using the provided segments and, optionally,
|
|
84
|
+
* separator.
|
|
85
|
+
*
|
|
86
|
+
* NOTE: this doesn't set the `segments` directly; it passes them through a
|
|
87
|
+
* filtering step first, to remove any double-slashes or etc. To set the
|
|
88
|
+
* `.segments` directly, use {@link fromRaw}.
|
|
89
|
+
*/
|
|
90
|
+
static from(segments, separator) {
|
|
91
|
+
const separatorToUse = separator || Path.detectSeparator(segments, "/");
|
|
92
|
+
const path = new Path();
|
|
93
|
+
path.segments = validateSegments(segments, separatorToUse);
|
|
94
|
+
path.separator = separatorToUse;
|
|
95
|
+
return path;
|
|
96
|
+
}
|
|
82
97
|
/**
|
|
83
98
|
* Create a new Path object using the provided segments and separator.
|
|
99
|
+
*
|
|
100
|
+
* NOTE: this method doesn't do any sort of validation on `segments`; as such,
|
|
101
|
+
* it can be used to construct an invalid Path object. Consider using
|
|
102
|
+
* {@link from} instead.
|
|
84
103
|
*/
|
|
85
104
|
static fromRaw(segments, separator) {
|
|
86
105
|
const path = new Path();
|
|
87
|
-
path.segments =
|
|
106
|
+
path.segments = segments;
|
|
88
107
|
path.separator = separator;
|
|
89
108
|
return path;
|
|
90
109
|
}
|
|
@@ -132,7 +151,7 @@ class Path {
|
|
|
132
151
|
*/
|
|
133
152
|
concat(...others) {
|
|
134
153
|
const otherSegments = new Path(others.flat(1)).segments;
|
|
135
|
-
return Path.
|
|
154
|
+
return Path.from(this.segments.concat(otherSegments), this.separator);
|
|
136
155
|
}
|
|
137
156
|
/**
|
|
138
157
|
* Return whether this path is absolute; that is, whether it starts with
|
|
@@ -154,7 +173,7 @@ class Path {
|
|
|
154
173
|
* this one.
|
|
155
174
|
*/
|
|
156
175
|
clone() {
|
|
157
|
-
const theClone = this.constructor.fromRaw(this.segments, this.separator);
|
|
176
|
+
const theClone = this.constructor.fromRaw([...this.segments], this.separator);
|
|
158
177
|
return theClone;
|
|
159
178
|
}
|
|
160
179
|
/**
|
|
@@ -175,15 +194,15 @@ class Path {
|
|
|
175
194
|
}
|
|
176
195
|
if (dirSegments.length === 0) {
|
|
177
196
|
if (options.noLeadingDot) {
|
|
178
|
-
return Path.
|
|
197
|
+
return Path.from(ownSegments, this.separator);
|
|
179
198
|
}
|
|
180
199
|
else {
|
|
181
|
-
return Path.
|
|
200
|
+
return Path.from([".", ...ownSegments], this.separator);
|
|
182
201
|
}
|
|
183
202
|
}
|
|
184
203
|
else {
|
|
185
204
|
const dotDots = dirSegments.map((_) => "..");
|
|
186
|
-
return Path.
|
|
205
|
+
return Path.from([...dotDots, ...ownSegments], this.separator);
|
|
187
206
|
}
|
|
188
207
|
}
|
|
189
208
|
/**
|
|
@@ -314,6 +333,8 @@ class Path {
|
|
|
314
333
|
*
|
|
315
334
|
* @param value - What should be replaced
|
|
316
335
|
* @param replacement - What it should be replaced with
|
|
336
|
+
*
|
|
337
|
+
* NOTE: to remove segments, use an empty Array for `replacement`.
|
|
317
338
|
*/
|
|
318
339
|
replace(value, replacement) {
|
|
319
340
|
value = new Path(value);
|
|
@@ -328,7 +349,7 @@ class Path {
|
|
|
328
349
|
...replacement.segments,
|
|
329
350
|
...this.segments.slice(matchIndex + value.segments.length),
|
|
330
351
|
];
|
|
331
|
-
return Path.
|
|
352
|
+
return Path.from(newSegments, this.separator);
|
|
332
353
|
}
|
|
333
354
|
}
|
|
334
355
|
/**
|
|
@@ -366,7 +387,32 @@ class Path {
|
|
|
366
387
|
const segments = [...this.segments];
|
|
367
388
|
segments.pop();
|
|
368
389
|
segments.push(...replacement.segments);
|
|
369
|
-
return Path.
|
|
390
|
+
return Path.from(segments, this.separator);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Return a boolean indicating whether this Path has the same separator and
|
|
394
|
+
* segments as another Path.
|
|
395
|
+
*
|
|
396
|
+
* To check only segments and not separator, use {@link Path.prototype.hasEqualSegments}.
|
|
397
|
+
*/
|
|
398
|
+
equals(other) {
|
|
399
|
+
if (!(other instanceof Path)) {
|
|
400
|
+
other = new Path(other);
|
|
401
|
+
}
|
|
402
|
+
return other.separator === this.separator && this.hasEqualSegments(other);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Return a boolean indicating whether this Path has the same segments as
|
|
406
|
+
* another Path. **Separator is not checked; use {@link Path.prototype.equals} for that.**
|
|
407
|
+
*/
|
|
408
|
+
hasEqualSegments(other) {
|
|
409
|
+
if (!(other instanceof Path)) {
|
|
410
|
+
other = new Path(other);
|
|
411
|
+
}
|
|
412
|
+
return (this.segments.length === other.segments.length &&
|
|
413
|
+
this.segments.every((segment, index) => {
|
|
414
|
+
return segment === other.segments[index];
|
|
415
|
+
}));
|
|
370
416
|
}
|
|
371
417
|
}
|
|
372
418
|
exports.Path = Path;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nice-path",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"repository": {
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
"file"
|
|
18
18
|
],
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/node": "^
|
|
21
|
-
"prettier": "^3.
|
|
22
|
-
"typescript": "^5.
|
|
23
|
-
"vitest": "^
|
|
20
|
+
"@types/node": "^24.0.11",
|
|
21
|
+
"prettier": "^3.6.2",
|
|
22
|
+
"typescript": "^5.8.3",
|
|
23
|
+
"vitest": "^3.2.4"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "rm -rf dist && tsc",
|