nice-path 2.0.0 → 3.0.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/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
  /**
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 = validateSegments(segments, separator);
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.fromRaw(this.segments.concat(otherSegments), this.separator);
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.fromRaw(ownSegments, this.separator);
197
+ return Path.from(ownSegments, this.separator);
179
198
  }
180
199
  else {
181
- return Path.fromRaw([".", ...ownSegments], this.separator);
200
+ return Path.from([".", ...ownSegments], this.separator);
182
201
  }
183
202
  }
184
203
  else {
185
204
  const dotDots = dirSegments.map((_) => "..");
186
- return Path.fromRaw([...dotDots, ...ownSegments], this.separator);
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.fromRaw(newSegments, this.separator);
352
+ return Path.from(newSegments, this.separator);
332
353
  }
333
354
  }
334
355
  /**
@@ -366,7 +387,7 @@ class Path {
366
387
  const segments = [...this.segments];
367
388
  segments.pop();
368
389
  segments.push(...replacement.segments);
369
- return Path.fromRaw(segments, this.separator);
390
+ return Path.from(segments, this.separator);
370
391
  }
371
392
  }
372
393
  exports.Path = Path;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nice-path",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {