@rushstack/lookup-by-path 0.8.16 → 0.9.1

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/CHANGELOG.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@rushstack/lookup-by-path",
3
3
  "entries": [
4
+ {
5
+ "version": "0.9.1",
6
+ "tag": "@rushstack/lookup-by-path_v0.9.1",
7
+ "date": "Fri, 20 Feb 2026 00:15:04 GMT",
8
+ "comments": {
9
+ "patch": [
10
+ {
11
+ "comment": "Add `\"node\"` condition before `\"import\"` in the `\"exports\"` map so that Node.js uses the CJS output (which handles extensionless imports), while bundlers still use ESM via `\"import\"`. Fixes https://github.com/microsoft/rushstack/issues/5644."
12
+ }
13
+ ],
14
+ "dependency": [
15
+ {
16
+ "comment": "Updating dependency \"@rushstack/heft\" to `1.2.1`"
17
+ }
18
+ ]
19
+ }
20
+ },
21
+ {
22
+ "version": "0.9.0",
23
+ "tag": "@rushstack/lookup-by-path_v0.9.0",
24
+ "date": "Thu, 19 Feb 2026 00:04:53 GMT",
25
+ "comments": {
26
+ "minor": [
27
+ {
28
+ "comment": "Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `\"exports\"` field in `package.json`."
29
+ }
30
+ ],
31
+ "dependency": [
32
+ {
33
+ "comment": "Updating dependency \"@rushstack/heft\" to `1.2.0`"
34
+ }
35
+ ]
36
+ }
37
+ },
4
38
  {
5
39
  "version": "0.8.16",
6
40
  "tag": "@rushstack/lookup-by-path_v0.8.16",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # Change Log - @rushstack/lookup-by-path
2
2
 
3
- This log was last generated on Sat, 07 Feb 2026 01:13:26 GMT and should not be manually modified.
3
+ This log was last generated on Fri, 20 Feb 2026 00:15:04 GMT and should not be manually modified.
4
+
5
+ ## 0.9.1
6
+ Fri, 20 Feb 2026 00:15:04 GMT
7
+
8
+ ### Patches
9
+
10
+ - Add `"node"` condition before `"import"` in the `"exports"` map so that Node.js uses the CJS output (which handles extensionless imports), while bundlers still use ESM via `"import"`. Fixes https://github.com/microsoft/rushstack/issues/5644.
11
+
12
+ ## 0.9.0
13
+ Thu, 19 Feb 2026 00:04:53 GMT
14
+
15
+ ### Minor changes
16
+
17
+ - Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `"exports"` field in `package.json`.
4
18
 
5
19
  ## 0.8.16
6
20
  Sat, 07 Feb 2026 01:13:26 GMT
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.56.2"
8
+ "packageVersion": "7.57.0"
9
9
  }
10
10
  ]
11
11
  }
@@ -0,0 +1,351 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ /**
4
+ * This class is used to associate path-like-strings, such as those returned by `git` commands,
5
+ * with entities that correspond with ancestor folders, such as Rush Projects or npm packages.
6
+ *
7
+ * It is optimized for efficiently locating the nearest ancestor path with an associated value.
8
+ *
9
+ * It is implemented as a Trie (https://en.wikipedia.org/wiki/Trie) data structure, with each edge
10
+ * being a path segment.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const trie = new LookupByPath([['foo', 1], ['bar', 2], ['foo/bar', 3]]);
15
+ * trie.findChildPath('foo'); // returns 1
16
+ * trie.findChildPath('foo/baz'); // returns 1
17
+ * trie.findChildPath('baz'); // returns undefined
18
+ * trie.findChildPath('foo/bar/baz'); returns 3
19
+ * trie.findChildPath('bar/foo/bar'); returns 2
20
+ * ```
21
+ * @beta
22
+ */
23
+ export class LookupByPath {
24
+ /**
25
+ * Constructs a new `LookupByPath`
26
+ *
27
+ * @param entries - Initial path-value pairs to populate the trie.
28
+ */
29
+ constructor(entries, delimiter) {
30
+ this._root = {
31
+ value: undefined,
32
+ children: undefined
33
+ };
34
+ this.delimiter = delimiter !== null && delimiter !== void 0 ? delimiter : '/';
35
+ this._size = 0;
36
+ if (entries) {
37
+ for (const [path, item] of entries) {
38
+ this.setItem(path, item);
39
+ }
40
+ }
41
+ }
42
+ /**
43
+ * Iterates over the segments of a serialized path.
44
+ *
45
+ * @example
46
+ *
47
+ * `LookupByPath.iteratePathSegments('foo/bar/baz')` yields 'foo', 'bar', 'baz'
48
+ *
49
+ * `LookupByPath.iteratePathSegments('foo\\bar\\baz', '\\')` yields 'foo', 'bar', 'baz'
50
+ */
51
+ static *iteratePathSegments(serializedPath, delimiter = '/') {
52
+ for (const prefixMatch of this._iteratePrefixes(serializedPath, delimiter)) {
53
+ yield prefixMatch.prefix;
54
+ }
55
+ }
56
+ static *_iteratePrefixes(input, delimiter = '/') {
57
+ if (!input) {
58
+ return;
59
+ }
60
+ let previousIndex = 0;
61
+ let nextIndex = input.indexOf(delimiter);
62
+ // Leading segments
63
+ while (nextIndex >= 0) {
64
+ yield {
65
+ prefix: input.slice(previousIndex, nextIndex),
66
+ index: nextIndex
67
+ };
68
+ previousIndex = nextIndex + 1;
69
+ nextIndex = input.indexOf(delimiter, previousIndex);
70
+ }
71
+ // Last segment
72
+ if (previousIndex < input.length) {
73
+ yield {
74
+ prefix: input.slice(previousIndex, input.length),
75
+ index: input.length
76
+ };
77
+ }
78
+ }
79
+ /**
80
+ * {@inheritdoc IReadonlyLookupByPath.size}
81
+ */
82
+ get size() {
83
+ return this._size;
84
+ }
85
+ /**
86
+ * {@inheritdoc IReadonlyLookupByPath.tree}
87
+ */
88
+ get tree() {
89
+ return this._root;
90
+ }
91
+ /**
92
+ * Deletes all entries from this `LookupByPath` instance.
93
+ *
94
+ * @returns this, for chained calls
95
+ */
96
+ clear() {
97
+ this._root.value = undefined;
98
+ this._root.children = undefined;
99
+ this._size = 0;
100
+ return this;
101
+ }
102
+ /**
103
+ * Associates the value with the specified serialized path.
104
+ * If a value is already associated, will overwrite.
105
+ *
106
+ * @returns this, for chained calls
107
+ */
108
+ setItem(serializedPath, value, delimiter = this.delimiter) {
109
+ return this.setItemFromSegments(LookupByPath.iteratePathSegments(serializedPath, delimiter), value);
110
+ }
111
+ /**
112
+ * Deletes an item if it exists.
113
+ * @param query - The path to the item to delete
114
+ * @param delimeter - Optional override delimeter for parsing the query
115
+ * @returns `true` if the item was found and deleted, `false` otherwise
116
+ * @remarks
117
+ * If the node has children with values, they will be retained.
118
+ */
119
+ deleteItem(query, delimeter = this.delimiter) {
120
+ const node = this._findNodeAtPrefix(query, delimeter);
121
+ if ((node === null || node === void 0 ? void 0 : node.value) !== undefined) {
122
+ node.value = undefined;
123
+ this._size--;
124
+ return true;
125
+ }
126
+ return false;
127
+ }
128
+ /**
129
+ * Deletes an item and all its children.
130
+ * @param query - The path to the item to delete
131
+ * @param delimeter - Optional override delimeter for parsing the query
132
+ * @returns `true` if any nodes were deleted, `false` otherwise
133
+ */
134
+ deleteSubtree(query, delimeter = this.delimiter) {
135
+ const queryNode = this._findNodeAtPrefix(query, delimeter);
136
+ if (!queryNode) {
137
+ return false;
138
+ }
139
+ const queue = [queryNode];
140
+ let removed = 0;
141
+ while (queue.length > 0) {
142
+ const node = queue.pop();
143
+ if (node.value !== undefined) {
144
+ node.value = undefined;
145
+ removed++;
146
+ }
147
+ if (node.children) {
148
+ for (const child of node.children.values()) {
149
+ queue.push(child);
150
+ }
151
+ node.children.clear();
152
+ }
153
+ }
154
+ this._size -= removed;
155
+ return removed > 0;
156
+ }
157
+ /**
158
+ * Associates the value with the specified path.
159
+ * If a value is already associated, will overwrite.
160
+ *
161
+ * @returns this, for chained calls
162
+ */
163
+ setItemFromSegments(pathSegments, value) {
164
+ let node = this._root;
165
+ for (const segment of pathSegments) {
166
+ if (!node.children) {
167
+ node.children = new Map();
168
+ }
169
+ let child = node.children.get(segment);
170
+ if (!child) {
171
+ node.children.set(segment, (child = {
172
+ value: undefined,
173
+ children: undefined
174
+ }));
175
+ }
176
+ node = child;
177
+ }
178
+ if (node.value === undefined) {
179
+ this._size++;
180
+ }
181
+ node.value = value;
182
+ return this;
183
+ }
184
+ /**
185
+ * {@inheritdoc IReadonlyLookupByPath}
186
+ */
187
+ findChildPath(childPath, delimiter = this.delimiter) {
188
+ return this.findChildPathFromSegments(LookupByPath.iteratePathSegments(childPath, delimiter));
189
+ }
190
+ /**
191
+ * {@inheritdoc IReadonlyLookupByPath}
192
+ */
193
+ findLongestPrefixMatch(query, delimiter = this.delimiter) {
194
+ return this._findLongestPrefixMatch(LookupByPath._iteratePrefixes(query, delimiter));
195
+ }
196
+ /**
197
+ * {@inheritdoc IReadonlyLookupByPath}
198
+ */
199
+ findChildPathFromSegments(childPathSegments) {
200
+ var _a;
201
+ let node = this._root;
202
+ let best = node.value;
203
+ // Trivial cases
204
+ if (node.children) {
205
+ for (const segment of childPathSegments) {
206
+ const child = node.children.get(segment);
207
+ if (!child) {
208
+ break;
209
+ }
210
+ node = child;
211
+ best = (_a = node.value) !== null && _a !== void 0 ? _a : best;
212
+ if (!node.children) {
213
+ break;
214
+ }
215
+ }
216
+ }
217
+ return best;
218
+ }
219
+ /**
220
+ * {@inheritdoc IReadonlyLookupByPath}
221
+ */
222
+ has(key, delimiter = this.delimiter) {
223
+ const match = this.findLongestPrefixMatch(key, delimiter);
224
+ return (match === null || match === void 0 ? void 0 : match.index) === key.length;
225
+ }
226
+ /**
227
+ * {@inheritdoc IReadonlyLookupByPath}
228
+ */
229
+ get(key, delimiter = this.delimiter) {
230
+ const match = this.findLongestPrefixMatch(key, delimiter);
231
+ return (match === null || match === void 0 ? void 0 : match.index) === key.length ? match.value : undefined;
232
+ }
233
+ /**
234
+ * {@inheritdoc IReadonlyLookupByPath}
235
+ */
236
+ groupByChild(infoByPath, delimiter = this.delimiter) {
237
+ const groupedInfoByChild = new Map();
238
+ for (const [path, info] of infoByPath) {
239
+ const child = this.findChildPath(path, delimiter);
240
+ if (child === undefined) {
241
+ continue;
242
+ }
243
+ let groupedInfo = groupedInfoByChild.get(child);
244
+ if (!groupedInfo) {
245
+ groupedInfo = new Map();
246
+ groupedInfoByChild.set(child, groupedInfo);
247
+ }
248
+ groupedInfo.set(path, info);
249
+ }
250
+ return groupedInfoByChild;
251
+ }
252
+ /**
253
+ * {@inheritdoc IReadonlyLookupByPath}
254
+ */
255
+ *entries(query, delimiter = this.delimiter) {
256
+ let root;
257
+ if (query) {
258
+ root = this._findNodeAtPrefix(query, delimiter);
259
+ if (!root) {
260
+ return;
261
+ }
262
+ }
263
+ else {
264
+ root = this._root;
265
+ }
266
+ const stack = [[query !== null && query !== void 0 ? query : '', root]];
267
+ while (stack.length > 0) {
268
+ const [prefix, node] = stack.pop();
269
+ if (node.value !== undefined) {
270
+ yield [prefix, node.value];
271
+ }
272
+ if (node.children) {
273
+ for (const [segment, child] of node.children) {
274
+ stack.push([prefix ? `${prefix}${delimiter}${segment}` : segment, child]);
275
+ }
276
+ }
277
+ }
278
+ }
279
+ /**
280
+ * {@inheritdoc IReadonlyLookupByPath}
281
+ */
282
+ [Symbol.iterator](query, delimiter = this.delimiter) {
283
+ return this.entries(query, delimiter);
284
+ }
285
+ /**
286
+ * {@inheritdoc IReadonlyLookupByPath}
287
+ */
288
+ getNodeAtPrefix(query, delimiter = this.delimiter) {
289
+ return this._findNodeAtPrefix(query, delimiter);
290
+ }
291
+ /**
292
+ * Iterates through progressively longer prefixes of a given string and returns as soon
293
+ * as the number of candidate items that match the prefix are 1 or 0.
294
+ *
295
+ * If a match is present, returns the matched itme and the length of the matched prefix.
296
+ *
297
+ * @returns the found item, or `undefined` if no item was found
298
+ */
299
+ _findLongestPrefixMatch(prefixes) {
300
+ let node = this._root;
301
+ let best = node.value
302
+ ? {
303
+ value: node.value,
304
+ index: 0,
305
+ lastMatch: undefined
306
+ }
307
+ : undefined;
308
+ // Trivial cases
309
+ if (node.children) {
310
+ for (const { prefix: hash, index } of prefixes) {
311
+ const child = node.children.get(hash);
312
+ if (!child) {
313
+ break;
314
+ }
315
+ node = child;
316
+ if (node.value !== undefined) {
317
+ best = {
318
+ value: node.value,
319
+ index,
320
+ lastMatch: best
321
+ };
322
+ }
323
+ if (!node.children) {
324
+ break;
325
+ }
326
+ }
327
+ }
328
+ return best;
329
+ }
330
+ /**
331
+ * Finds the node at the specified path, or `undefined` if no node was found.
332
+ *
333
+ * @param query - The path to the node to search for
334
+ * @returns The trie node at the specified path, or `undefined` if no node was found
335
+ */
336
+ _findNodeAtPrefix(query, delimiter = this.delimiter) {
337
+ let node = this._root;
338
+ for (const { prefix } of LookupByPath._iteratePrefixes(query, delimiter)) {
339
+ if (!node.children) {
340
+ return undefined;
341
+ }
342
+ const child = node.children.get(prefix);
343
+ if (!child) {
344
+ return undefined;
345
+ }
346
+ node = child;
347
+ }
348
+ return node;
349
+ }
350
+ }
351
+ //# sourceMappingURL=LookupByPath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LookupByPath.js","sourceRoot":"","sources":["../src/LookupByPath.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAsM3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,YAAY;IAgBvB;;;;OAIG;IACH,YAAmB,OAAmC,EAAE,SAAkB;QACxE,IAAI,CAAC,KAAK,GAAG;YACX,KAAK,EAAE,SAAS;YAChB,QAAQ,EAAE,SAAS;SACpB,CAAC;QAEF,IAAI,CAAC,SAAS,GAAG,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,GAAG,CAAC;QAClC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QAEf,IAAI,OAAO,EAAE,CAAC;YACZ,KAAK,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,OAAO,EAAE,CAAC;gBACnC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;;;;OAQG;IACI,MAAM,CAAC,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. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * A node in the path trie used in LookupByPath\n */\ninterface IPathTrieNode<TItem extends {}> {\n /**\n * The value that exactly matches the current relative path\n */\n value: TItem | undefined;\n\n /**\n * Child nodes by subfolder\n */\n children: Map<string, IPathTrieNode<TItem>> | undefined;\n}\n\n/**\n * Readonly view of a node in the path trie used in LookupByPath\n *\n * @remarks\n * This interface is used to facilitate parallel traversals for comparing two `LookupByPath` instances.\n *\n * @beta\n */\nexport interface IReadonlyPathTrieNode<TItem extends {}> {\n /**\n * The value that exactly matches the current relative path\n */\n readonly value: TItem | undefined;\n\n /**\n * Child nodes by subfolder\n */\n readonly children: ReadonlyMap<string, IReadonlyPathTrieNode<TItem>> | undefined;\n}\n\ninterface IPrefixEntry {\n /**\n * The prefix that was matched\n */\n prefix: string;\n\n /**\n * The index of the first character after the matched prefix\n */\n index: number;\n}\n\n/**\n * Object containing both the matched item and the start index of the remainder of the query.\n *\n * @beta\n */\nexport interface IPrefixMatch<TItem extends {}> {\n /**\n * The item that matched the prefix\n */\n value: TItem;\n\n /**\n * The index of the first character after the matched prefix\n */\n index: number;\n\n /**\n * The last match found (with a shorter prefix), if any\n */\n lastMatch?: IPrefixMatch<TItem>;\n}\n\n/**\n * The readonly component of `LookupByPath`, to simplify unit testing.\n *\n * @beta\n */\nexport interface IReadonlyLookupByPath<TItem extends {}> extends Iterable<[string, TItem]> {\n /**\n * Searches for the item associated with `childPath`, or the nearest ancestor of that path that\n * has an associated item.\n *\n * @returns the found item, or `undefined` if no item was found\n *\n * @example\n * ```ts\n * const trie = new LookupByPath([['foo', 1], ['foo/bar', 2]]);\n * trie.findChildPath('foo/baz'); // returns 1\n * trie.findChildPath('foo/bar/baz'); // returns 2\n * ```\n */\n findChildPath(childPath: string, delimiter?: string): TItem | undefined;\n\n /**\n * Searches for the item for which the recorded prefix is the longest matching prefix of `query`.\n * Obtains both the item and the length of the matched prefix, so that the remainder of the path can be\n * extracted.\n *\n * @returns the found item and the length of the matched prefix, or `undefined` if no item was found\n *\n * @example\n * ```ts\n * const trie = new LookupByPath([['foo', 1], ['foo/bar', 2]]);\n * trie.findLongestPrefixMatch('foo/baz'); // returns { item: 1, index: 3 }\n * trie.findLongestPrefixMatch('foo/bar/baz'); // returns { item: 2, index: 7 }\n * ```\n */\n findLongestPrefixMatch(query: string, delimiter?: string): IPrefixMatch<TItem> | undefined;\n\n /**\n * Searches for the item associated with `childPathSegments`, or the nearest ancestor of that path that\n * has an associated item.\n *\n * @returns the found item, or `undefined` if no item was found\n *\n * @example\n * ```ts\n * const trie = new LookupByPath([['foo', 1], ['foo/bar', 2]]);\n * trie.findChildPathFromSegments(['foo', 'baz']); // returns 1\n * trie.findChildPathFromSegments(['foo','bar', 'baz']); // returns 2\n * ```\n */\n findChildPathFromSegments(childPathSegments: Iterable<string>): TItem | undefined;\n\n /**\n * Determines if an entry exists exactly at the specified path.\n *\n * @returns `true` if an entry exists at the specified path, `false` otherwise\n */\n has(query: string, delimiter?: string): boolean;\n\n /**\n * Retrieves the entry that exists exactly at the specified path, if any.\n *\n * @returns The entry that exists exactly at the specified path, or `undefined` if no entry exists.\n */\n get(query: string, delimiter?: string): TItem | undefined;\n\n /**\n * Gets the number of entries in this trie.\n *\n * @returns The number of entries in this trie.\n */\n get size(): number;\n\n /**\n * @returns The root node of the trie, corresponding to the path ''\n */\n get tree(): IReadonlyPathTrieNode<TItem>;\n\n /**\n * Iterates over the entries in this trie.\n *\n * @param query - An optional query. If specified only entries that start with the query will be returned.\n *\n * @returns An iterator over the entries under the specified query (or the root if no query is specified).\n * @remarks\n * Keys in the returned iterator use the provided delimiter to join segments.\n * Iteration order is not specified.\n * @example\n * ```ts\n * const trie = new LookupByPath([['foo', 1], ['foo/bar', 2]]);\n * [...trie.entries(undefined, ',')); // returns [['foo', 1], ['foo,bar', 2]]\n * ```\n */\n entries(query?: string, delimiter?: string): IterableIterator<[string, TItem]>;\n\n /**\n * Iterates over the entries in this trie.\n *\n * @param query - An optional query. If specified only entries that start with the query will be returned.\n *\n * @returns An iterator over the entries under the specified query (or the root if no query is specified).\n * @remarks\n * Keys in the returned iterator use the provided delimiter to join segments.\n * Iteration order is not specified.\n */\n [Symbol.iterator](query?: string, delimiter?: string): IterableIterator<[string, TItem]>;\n\n /**\n * Groups the provided map of info by the nearest entry in the trie that contains the path. If the path\n * is not found in the trie, the info is ignored.\n *\n * @returns The grouped info, grouped by the nearest entry in the trie that contains the path\n *\n * @param infoByPath - The info to be grouped, keyed by path\n */\n groupByChild<TInfo>(infoByPath: Map<string, TInfo>, delimiter?: string): Map<TItem, Map<string, TInfo>>;\n\n /**\n * Retrieves the trie node at the specified prefix, if it exists.\n *\n * @param query - The prefix to check for\n * @param delimiter - The path delimiter\n * @returns The trie node at the specified prefix, or `undefined` if no node was found\n */\n getNodeAtPrefix(query: string, delimiter?: string): IReadonlyPathTrieNode<TItem> | undefined;\n}\n\n/**\n * This class is used to associate path-like-strings, such as those returned by `git` commands,\n * with entities that correspond with ancestor folders, such as Rush Projects or npm packages.\n *\n * It is optimized for efficiently locating the nearest ancestor path with an associated value.\n *\n * It is implemented as a Trie (https://en.wikipedia.org/wiki/Trie) data structure, with each edge\n * being a path segment.\n *\n * @example\n * ```ts\n * const trie = new LookupByPath([['foo', 1], ['bar', 2], ['foo/bar', 3]]);\n * trie.findChildPath('foo'); // returns 1\n * trie.findChildPath('foo/baz'); // returns 1\n * trie.findChildPath('baz'); // returns undefined\n * trie.findChildPath('foo/bar/baz'); returns 3\n * trie.findChildPath('bar/foo/bar'); returns 2\n * ```\n * @beta\n */\nexport class LookupByPath<TItem extends {}> implements IReadonlyLookupByPath<TItem> {\n /**\n * The delimiter used to split paths\n */\n public readonly delimiter: string;\n\n /**\n * The root node of the trie, corresponding to the path ''\n */\n private readonly _root: IPathTrieNode<TItem>;\n\n /**\n * The number of entries in this trie.\n */\n private _size: number;\n\n /**\n * Constructs a new `LookupByPath`\n *\n * @param entries - Initial path-value pairs to populate the trie.\n */\n public constructor(entries?: Iterable<[string, TItem]>, delimiter?: string) {\n this._root = {\n value: undefined,\n children: undefined\n };\n\n this.delimiter = delimiter ?? '/';\n this._size = 0;\n\n if (entries) {\n for (const [path, item] of entries) {\n this.setItem(path, item);\n }\n }\n }\n\n /**\n * Iterates over the segments of a serialized path.\n *\n * @example\n *\n * `LookupByPath.iteratePathSegments('foo/bar/baz')` yields 'foo', 'bar', 'baz'\n *\n * `LookupByPath.iteratePathSegments('foo\\\\bar\\\\baz', '\\\\')` yields 'foo', 'bar', 'baz'\n */\n public static *iteratePathSegments(serializedPath: string, delimiter: string = '/'): Iterable<string> {\n for (const prefixMatch of this._iteratePrefixes(serializedPath, delimiter)) {\n yield prefixMatch.prefix;\n }\n }\n\n private static *_iteratePrefixes(input: string, delimiter: string = '/'): Iterable<IPrefixEntry> {\n if (!input) {\n return;\n }\n\n let previousIndex: number = 0;\n let nextIndex: number = input.indexOf(delimiter);\n\n // Leading segments\n while (nextIndex >= 0) {\n yield {\n prefix: input.slice(previousIndex, nextIndex),\n index: nextIndex\n };\n previousIndex = nextIndex + 1;\n nextIndex = input.indexOf(delimiter, previousIndex);\n }\n\n // Last segment\n if (previousIndex < input.length) {\n yield {\n prefix: input.slice(previousIndex, input.length),\n index: input.length\n };\n }\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath.size}\n */\n public get size(): number {\n return this._size;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath.tree}\n */\n public get tree(): IReadonlyPathTrieNode<TItem> {\n return this._root;\n }\n\n /**\n * Deletes all entries from this `LookupByPath` instance.\n *\n * @returns this, for chained calls\n */\n public clear(): this {\n this._root.value = undefined;\n this._root.children = undefined;\n this._size = 0;\n return this;\n }\n\n /**\n * Associates the value with the specified serialized path.\n * If a value is already associated, will overwrite.\n *\n * @returns this, for chained calls\n */\n public setItem(serializedPath: string, value: TItem, delimiter: string = this.delimiter): this {\n return this.setItemFromSegments(LookupByPath.iteratePathSegments(serializedPath, delimiter), value);\n }\n\n /**\n * Deletes an item if it exists.\n * @param query - The path to the item to delete\n * @param delimeter - Optional override delimeter for parsing the query\n * @returns `true` if the item was found and deleted, `false` otherwise\n * @remarks\n * If the node has children with values, they will be retained.\n */\n public deleteItem(query: string, delimeter: string = this.delimiter): boolean {\n const node: IPathTrieNode<TItem> | undefined = this._findNodeAtPrefix(query, delimeter);\n if (node?.value !== undefined) {\n node.value = undefined;\n this._size--;\n return true;\n }\n\n return false;\n }\n\n /**\n * Deletes an item and all its children.\n * @param query - The path to the item to delete\n * @param delimeter - Optional override delimeter for parsing the query\n * @returns `true` if any nodes were deleted, `false` otherwise\n */\n public deleteSubtree(query: string, delimeter: string = this.delimiter): boolean {\n const queryNode: IPathTrieNode<TItem> | undefined = this._findNodeAtPrefix(query, delimeter);\n if (!queryNode) {\n return false;\n }\n\n const queue: IPathTrieNode<TItem>[] = [queryNode];\n let removed: number = 0;\n while (queue.length > 0) {\n const node: IPathTrieNode<TItem> = queue.pop()!;\n if (node.value !== undefined) {\n node.value = undefined;\n removed++;\n }\n if (node.children) {\n for (const child of node.children.values()) {\n queue.push(child);\n }\n node.children.clear();\n }\n }\n\n this._size -= removed;\n return removed > 0;\n }\n\n /**\n * Associates the value with the specified path.\n * If a value is already associated, will overwrite.\n *\n * @returns this, for chained calls\n */\n public setItemFromSegments(pathSegments: Iterable<string>, value: TItem): this {\n let node: IPathTrieNode<TItem> = this._root;\n for (const segment of pathSegments) {\n if (!node.children) {\n node.children = new Map();\n }\n let child: IPathTrieNode<TItem> | undefined = node.children.get(segment);\n if (!child) {\n node.children.set(\n segment,\n (child = {\n value: undefined,\n children: undefined\n })\n );\n }\n node = child;\n }\n if (node.value === undefined) {\n this._size++;\n }\n node.value = value;\n\n return this;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public findChildPath(childPath: string, delimiter: string = this.delimiter): TItem | undefined {\n return this.findChildPathFromSegments(LookupByPath.iteratePathSegments(childPath, delimiter));\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public findLongestPrefixMatch(\n query: string,\n delimiter: string = this.delimiter\n ): IPrefixMatch<TItem> | undefined {\n return this._findLongestPrefixMatch(LookupByPath._iteratePrefixes(query, delimiter));\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public findChildPathFromSegments(childPathSegments: Iterable<string>): TItem | undefined {\n let node: IPathTrieNode<TItem> = this._root;\n let best: TItem | undefined = node.value;\n // Trivial cases\n if (node.children) {\n for (const segment of childPathSegments) {\n const child: IPathTrieNode<TItem> | undefined = node.children.get(segment);\n if (!child) {\n break;\n }\n node = child;\n best = node.value ?? best;\n if (!node.children) {\n break;\n }\n }\n }\n\n return best;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public has(key: string, delimiter: string = this.delimiter): boolean {\n const match: IPrefixMatch<TItem> | undefined = this.findLongestPrefixMatch(key, delimiter);\n return match?.index === key.length;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public get(key: string, delimiter: string = this.delimiter): TItem | undefined {\n const match: IPrefixMatch<TItem> | undefined = this.findLongestPrefixMatch(key, delimiter);\n return match?.index === key.length ? match.value : undefined;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public groupByChild<TInfo>(\n infoByPath: Map<string, TInfo>,\n delimiter: string = this.delimiter\n ): Map<TItem, Map<string, TInfo>> {\n const groupedInfoByChild: Map<TItem, Map<string, TInfo>> = new Map();\n\n for (const [path, info] of infoByPath) {\n const child: TItem | undefined = this.findChildPath(path, delimiter);\n if (child === undefined) {\n continue;\n }\n let groupedInfo: Map<string, TInfo> | undefined = groupedInfoByChild.get(child);\n if (!groupedInfo) {\n groupedInfo = new Map();\n groupedInfoByChild.set(child, groupedInfo);\n }\n groupedInfo.set(path, info);\n }\n\n return groupedInfoByChild;\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public *entries(query?: string, delimiter: string = this.delimiter): IterableIterator<[string, TItem]> {\n let root: IPathTrieNode<TItem> | undefined;\n if (query) {\n root = this._findNodeAtPrefix(query, delimiter);\n if (!root) {\n return;\n }\n } else {\n root = this._root;\n }\n\n const stack: [string, IPathTrieNode<TItem>][] = [[query ?? '', root]];\n while (stack.length > 0) {\n const [prefix, node] = stack.pop()!;\n if (node.value !== undefined) {\n yield [prefix, node.value];\n }\n if (node.children) {\n for (const [segment, child] of node.children) {\n stack.push([prefix ? `${prefix}${delimiter}${segment}` : segment, child]);\n }\n }\n }\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public [Symbol.iterator](\n query?: string,\n delimiter: string = this.delimiter\n ): IterableIterator<[string, TItem]> {\n return this.entries(query, delimiter);\n }\n\n /**\n * {@inheritdoc IReadonlyLookupByPath}\n */\n public getNodeAtPrefix(\n query: string,\n delimiter: string = this.delimiter\n ): IReadonlyPathTrieNode<TItem> | undefined {\n return this._findNodeAtPrefix(query, delimiter);\n }\n\n /**\n * Iterates through progressively longer prefixes of a given string and returns as soon\n * as the number of candidate items that match the prefix are 1 or 0.\n *\n * If a match is present, returns the matched itme and the length of the matched prefix.\n *\n * @returns the found item, or `undefined` if no item was found\n */\n private _findLongestPrefixMatch(prefixes: Iterable<IPrefixEntry>): IPrefixMatch<TItem> | undefined {\n let node: IPathTrieNode<TItem> = this._root;\n let best: IPrefixMatch<TItem> | undefined = node.value\n ? {\n value: node.value,\n index: 0,\n lastMatch: undefined\n }\n : undefined;\n // Trivial cases\n if (node.children) {\n for (const { prefix: hash, index } of prefixes) {\n const child: IPathTrieNode<TItem> | undefined = node.children.get(hash);\n if (!child) {\n break;\n }\n node = child;\n if (node.value !== undefined) {\n best = {\n value: node.value,\n index,\n lastMatch: best\n };\n }\n if (!node.children) {\n break;\n }\n }\n }\n\n return best;\n }\n\n /**\n * Finds the node at the specified path, or `undefined` if no node was found.\n *\n * @param query - The path to the node to search for\n * @returns The trie node at the specified path, or `undefined` if no node was found\n */\n private _findNodeAtPrefix(\n query: string,\n delimiter: string = this.delimiter\n ): IPathTrieNode<TItem> | undefined {\n let node: IPathTrieNode<TItem> = this._root;\n for (const { prefix } of LookupByPath._iteratePrefixes(query, delimiter)) {\n if (!node.children) {\n return undefined;\n }\n const child: IPathTrieNode<TItem> | undefined = node.children.get(prefix);\n if (!child) {\n return undefined;\n }\n node = child;\n }\n return node;\n }\n}\n"]}
@@ -0,0 +1,73 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ /**
4
+ * Recursively compares two path tries to find the first shared node with a different value.
5
+ *
6
+ * @param options - The options for the comparison
7
+ * @returns The path to the first differing node, or undefined if they are identical
8
+ *
9
+ * @remarks
10
+ * Ignores any nodes that are not shared between the two tries.
11
+ *
12
+ * @beta
13
+ */
14
+ export function getFirstDifferenceInCommonNodes(options) {
15
+ const { first, second, prefix = '', delimiter = '/', equals = defaultEquals } = options;
16
+ return getFirstDifferenceInCommonNodesInternal({
17
+ first,
18
+ second,
19
+ prefix,
20
+ delimiter,
21
+ equals
22
+ });
23
+ }
24
+ /**
25
+ * Recursively compares two path tries to find the first shared node with a different value.
26
+ *
27
+ * @param options - The options for the comparison
28
+ * @returns The path to the first differing node, or undefined if they are identical
29
+ *
30
+ * @remarks
31
+ * Ignores any nodes that are not shared between the two tries.
32
+ * Separated out to avoid redundant parameter defaulting in the recursive calls.
33
+ */
34
+ function getFirstDifferenceInCommonNodesInternal(options) {
35
+ const { first, second, prefix, delimiter, equals } = options;
36
+ const firstItem = first.value;
37
+ const secondItem = second.value;
38
+ if (firstItem !== undefined && secondItem !== undefined && !equals(firstItem, secondItem)) {
39
+ // If this value was present in both tries with different values, return the prefix for this node.
40
+ return prefix;
41
+ }
42
+ const { children: firstChildren } = first;
43
+ const { children: secondChildren } = second;
44
+ if (firstChildren && secondChildren) {
45
+ for (const [key, firstChild] of firstChildren) {
46
+ const secondChild = secondChildren.get(key);
47
+ if (!secondChild) {
48
+ continue;
49
+ }
50
+ const result = getFirstDifferenceInCommonNodesInternal({
51
+ first: firstChild,
52
+ second: secondChild,
53
+ prefix: key,
54
+ delimiter,
55
+ equals
56
+ });
57
+ if (result !== undefined) {
58
+ return prefix ? `${prefix}${delimiter}${result}` : result;
59
+ }
60
+ }
61
+ }
62
+ return;
63
+ }
64
+ /**
65
+ * Default equality function for comparing two items, using strict equality.
66
+ * @param a - The first item to compare
67
+ * @param b - The second item to compare
68
+ * @returns True if the items are reference equal, false otherwise
69
+ */
70
+ function defaultEquals(a, b) {
71
+ return a === b;
72
+ }
73
+ //# sourceMappingURL=getFirstDifferenceInCommonNodes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFirstDifferenceInCommonNodes.js","sourceRoot":"","sources":["../src/getFirstDifferenceInCommonNodes.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAkC3D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,+BAA+B,CAC7C,OAAuD;IAEvD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,SAAS,GAAG,GAAG,EAAE,MAAM,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IAExF,OAAO,uCAAuC,CAAC;QAC7C,KAAK;QACL,MAAM;QACN,MAAM;QACN,SAAS;QACT,MAAM;KACP,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,uCAAuC,CAC9C,OAAiE;IAEjE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE7D,MAAM,SAAS,GAAsB,KAAK,CAAC,KAAK,CAAC;IACjD,MAAM,UAAU,GAAsB,MAAM,CAAC,KAAK,CAAC;IAEnD,IAAI,SAAS,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,EAAE,CAAC;QAC1F,kGAAkG;QAClG,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,GAAG,KAAK,CAAC;IAC1C,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;IAE5C,IAAI,aAAa,IAAI,cAAc,EAAE,CAAC;QACpC,KAAK,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,aAAa,EAAE,CAAC;YAC9C,MAAM,WAAW,GAA6C,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACtF,IAAI,CAAC,WAAW,EAAE,CAAC;gBACjB,SAAS;YACX,CAAC;YACD,MAAM,MAAM,GAAuB,uCAAuC,CAAC;gBACzE,KAAK,EAAE,UAAU;gBACjB,MAAM,EAAE,WAAW;gBACnB,MAAM,EAAE,GAAG;gBACX,SAAS;gBACT,MAAM;aACP,CAAC,CAAC;YAEH,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;YAC5D,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;AACT,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAQ,CAAQ,EAAE,CAAQ;IAC9C,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { IReadonlyPathTrieNode } from './LookupByPath';\n\n/**\n * Options for the getFirstDifferenceInCommonNodes function.\n * @beta\n */\nexport interface IGetFirstDifferenceInCommonNodesOptions<TItem extends {}> {\n /**\n * The first node to compare.\n */\n first: IReadonlyPathTrieNode<TItem>;\n /**\n * The second node to compare.\n */\n second: IReadonlyPathTrieNode<TItem>;\n /**\n * The path prefix to the current node.\n * @defaultValue ''\n */\n prefix?: string;\n /**\n * The delimiter used to join path segments.\n * @defaultValue '/'\n */\n delimiter?: string;\n /**\n * A function to compare the values of the nodes.\n * If not provided, strict equality (===) is used.\n */\n equals?: (a: TItem, b: TItem) => boolean;\n}\n\n/**\n * Recursively compares two path tries to find the first shared node with a different value.\n *\n * @param options - The options for the comparison\n * @returns The path to the first differing node, or undefined if they are identical\n *\n * @remarks\n * Ignores any nodes that are not shared between the two tries.\n *\n * @beta\n */\nexport function getFirstDifferenceInCommonNodes<TItem extends {}>(\n options: IGetFirstDifferenceInCommonNodesOptions<TItem>\n): string | undefined {\n const { first, second, prefix = '', delimiter = '/', equals = defaultEquals } = options;\n\n return getFirstDifferenceInCommonNodesInternal({\n first,\n second,\n prefix,\n delimiter,\n equals\n });\n}\n\n/**\n * Recursively compares two path tries to find the first shared node with a different value.\n *\n * @param options - The options for the comparison\n * @returns The path to the first differing node, or undefined if they are identical\n *\n * @remarks\n * Ignores any nodes that are not shared between the two tries.\n * Separated out to avoid redundant parameter defaulting in the recursive calls.\n */\nfunction getFirstDifferenceInCommonNodesInternal<TItem extends {}>(\n options: Required<IGetFirstDifferenceInCommonNodesOptions<TItem>>\n): string | undefined {\n const { first, second, prefix, delimiter, equals } = options;\n\n const firstItem: TItem | undefined = first.value;\n const secondItem: TItem | undefined = second.value;\n\n if (firstItem !== undefined && secondItem !== undefined && !equals(firstItem, secondItem)) {\n // If this value was present in both tries with different values, return the prefix for this node.\n return prefix;\n }\n\n const { children: firstChildren } = first;\n const { children: secondChildren } = second;\n\n if (firstChildren && secondChildren) {\n for (const [key, firstChild] of firstChildren) {\n const secondChild: IReadonlyPathTrieNode<TItem> | undefined = secondChildren.get(key);\n if (!secondChild) {\n continue;\n }\n const result: string | undefined = getFirstDifferenceInCommonNodesInternal({\n first: firstChild,\n second: secondChild,\n prefix: key,\n delimiter,\n equals\n });\n\n if (result !== undefined) {\n return prefix ? `${prefix}${delimiter}${result}` : result;\n }\n }\n }\n\n return;\n}\n\n/**\n * Default equality function for comparing two items, using strict equality.\n * @param a - The first item to compare\n * @param b - The second item to compare\n * @returns True if the items are reference equal, false otherwise\n */\nfunction defaultEquals<TItem>(a: TItem, b: TItem): boolean {\n return a === b;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ export { LookupByPath } from './LookupByPath';
4
+ export { getFirstDifferenceInCommonNodes } from './getFirstDifferenceInCommonNodes';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAS3D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * Strongly typed trie data structure for path and URL-like strings.\n *\n * @packageDocumentation\n */\n\nexport type { IPrefixMatch, IReadonlyLookupByPath, IReadonlyPathTrieNode } from './LookupByPath';\nexport { LookupByPath } from './LookupByPath';\nexport type { IGetFirstDifferenceInCommonNodesOptions } from './getFirstDifferenceInCommonNodes';\nexport { getFirstDifferenceInCommonNodes } from './getFirstDifferenceInCommonNodes';\n"]}
package/package.json CHANGED
@@ -1,9 +1,32 @@
1
1
  {
2
2
  "name": "@rushstack/lookup-by-path",
3
- "version": "0.8.16",
3
+ "version": "0.9.1",
4
4
  "description": "Strongly typed trie data structure for path and URL-like strings.",
5
- "main": "lib/index.js",
6
- "typings": "dist/lookup-by-path.d.ts",
5
+ "main": "./lib-commonjs/index.js",
6
+ "module": "./lib-esm/index.js",
7
+ "types": "./dist/lookup-by-path.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/lookup-by-path.d.ts",
11
+ "node": "./lib-commonjs/index.js",
12
+ "import": "./lib-esm/index.js",
13
+ "require": "./lib-commonjs/index.js"
14
+ },
15
+ "./lib/*": {
16
+ "types": "./lib-dts/*.d.ts",
17
+ "node": "./lib-commonjs/*.js",
18
+ "import": "./lib-esm/*.js",
19
+ "require": "./lib-commonjs/*.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "typesVersions": {
24
+ "*": {
25
+ "lib/*": [
26
+ "lib-dts/*"
27
+ ]
28
+ }
29
+ },
7
30
  "keywords": [
8
31
  "trie",
9
32
  "path",
@@ -19,7 +42,7 @@
19
42
  },
20
43
  "devDependencies": {
21
44
  "eslint": "~9.37.0",
22
- "@rushstack/heft": "1.1.14",
45
+ "@rushstack/heft": "1.2.1",
23
46
  "local-node-rig": "1.0.0"
24
47
  },
25
48
  "peerDependencies": {
@@ -30,6 +53,7 @@
30
53
  "optional": true
31
54
  }
32
55
  },
56
+ "sideEffects": false,
33
57
  "scripts": {
34
58
  "build": "heft build --clean",
35
59
  "_phase:build": "heft run --only build -- --clean",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes