@weborigami/async-tree 0.5.1 → 0.5.2-test.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/package.json +2 -2
- package/src/Tree.d.ts +1 -1
- package/src/Tree.js +23 -5
- package/test/Tree.test.js +21 -0
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/async-tree",
|
|
3
|
-
"version": "0.5.1",
|
|
3
|
+
"version": "0.5.2-test.1",
|
|
4
4
|
"description": "Asynchronous tree drivers based on standard JavaScript classes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./main.js",
|
|
7
7
|
"browser": "./browser.js",
|
|
8
8
|
"types": "./index.ts",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@weborigami/types": "0.5.1"
|
|
10
|
+
"@weborigami/types": "0.5.2-test.1"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@types/node": "24.3.0",
|
package/src/Tree.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export function isTraversable(obj: any): boolean;
|
|
|
13
13
|
export function isTreelike(obj: any): obj is Treelike;
|
|
14
14
|
export function map(tree: Treelike, options: TreeMapOptions|ValueKeyFn): AsyncTree;
|
|
15
15
|
export function mapReduce(tree: Treelike, mapFn: ValueKeyFn | null, reduceFn: ReduceFn): Promise<any>;
|
|
16
|
-
export function paths(tree: Treelike, base?: string): string[];
|
|
16
|
+
export function paths(tree: Treelike, options?: { assumeSlashes?: boolean, base?: string }): string[];
|
|
17
17
|
export function plain(tree: Treelike): Promise<PlainObject>;
|
|
18
18
|
export function root(tree: Treelike): AsyncTree;
|
|
19
19
|
export function remove(AsyncTree: AsyncMutableTree, key: any): Promise<boolean>;
|
package/src/Tree.js
CHANGED
|
@@ -292,17 +292,35 @@ export async function mapReduce(treelike, valueFn, reduceFn) {
|
|
|
292
292
|
* Returns slash-separated paths for all values in the tree.
|
|
293
293
|
*
|
|
294
294
|
* @param {Treelike} treelike
|
|
295
|
-
* @param {string
|
|
295
|
+
* @param {{ assumeSlashes?: boolean, base?: string }} options
|
|
296
296
|
*/
|
|
297
|
-
export async function paths(treelike,
|
|
297
|
+
export async function paths(treelike, options = {}) {
|
|
298
298
|
const tree = from(treelike);
|
|
299
|
+
const base = options.base ?? "";
|
|
300
|
+
const assumeSlashes = options.assumeSlashes ?? false;
|
|
299
301
|
const result = [];
|
|
300
302
|
for (const key of await tree.keys()) {
|
|
301
303
|
const separator = trailingSlash.has(base) ? "" : "/";
|
|
302
304
|
const valuePath = base ? `${base}${separator}${key}` : key;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
305
|
+
let isSubtree;
|
|
306
|
+
let value;
|
|
307
|
+
if (assumeSlashes) {
|
|
308
|
+
// Subtree needs to have a trailing slash
|
|
309
|
+
isSubtree = trailingSlash.has(key);
|
|
310
|
+
if (isSubtree) {
|
|
311
|
+
// We'll need the value to recurse
|
|
312
|
+
value = await tree.get(key);
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
// Get value and check
|
|
316
|
+
value = await tree.get(key);
|
|
317
|
+
}
|
|
318
|
+
if (value) {
|
|
319
|
+
// If we got the value we can check if it's a subtree
|
|
320
|
+
isSubtree = isAsyncTree(value);
|
|
321
|
+
}
|
|
322
|
+
if (isSubtree) {
|
|
323
|
+
const subPaths = await paths(value, { assumeSlashes, base: valuePath });
|
|
306
324
|
result.push(...subPaths);
|
|
307
325
|
} else {
|
|
308
326
|
result.push(valuePath);
|
package/test/Tree.test.js
CHANGED
|
@@ -232,6 +232,27 @@ describe("Tree", () => {
|
|
|
232
232
|
assert.deepEqual(await Tree.paths(tree), ["a", "b", "c/d", "c/e"]);
|
|
233
233
|
});
|
|
234
234
|
|
|
235
|
+
test("paths can focus just on keys with trailing slashes", async () => {
|
|
236
|
+
const tree = new ObjectTree({
|
|
237
|
+
a: 1,
|
|
238
|
+
b: 2,
|
|
239
|
+
// This is a shallow ObjectTree, so `c` won't have a trailing slash
|
|
240
|
+
c: {
|
|
241
|
+
d: 3,
|
|
242
|
+
},
|
|
243
|
+
// Explicitly include a trailing slash to signal a subtree
|
|
244
|
+
"d/": new ObjectTree({
|
|
245
|
+
e: 4,
|
|
246
|
+
}),
|
|
247
|
+
});
|
|
248
|
+
assert.deepEqual(await Tree.paths(tree, { assumeSlashes: true }), [
|
|
249
|
+
"a",
|
|
250
|
+
"b",
|
|
251
|
+
"c",
|
|
252
|
+
"d/e",
|
|
253
|
+
]);
|
|
254
|
+
});
|
|
255
|
+
|
|
235
256
|
test("plain() produces a plain object version of a tree", async () => {
|
|
236
257
|
const tree = new ObjectTree({
|
|
237
258
|
a: 1,
|