@weborigami/async-tree 0.1.0 → 0.2.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/package.json +2 -2
- package/src/Tree.d.ts +1 -0
- package/src/Tree.js +13 -0
- package/src/drivers/FileTree.js +22 -2
- package/src/operations/concat.js +10 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/async-tree",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Asynchronous tree drivers based on standard JavaScript classes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./main.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"typescript": "5.6.2"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@weborigami/types": "0.
|
|
14
|
+
"@weborigami/types": "0.2.0"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "node --test --test-reporter=spec",
|
package/src/Tree.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export function map(tree: Treelike, options: TreeMapOptions|ValueKeyFn): AsyncTr
|
|
|
15
15
|
export function mapReduce(tree: Treelike, mapFn: ValueKeyFn | null, reduceFn: ReduceFn): Promise<any>;
|
|
16
16
|
export function paths(tree: Treelike, base?: string): string[];
|
|
17
17
|
export function plain(tree: Treelike): Promise<PlainObject>;
|
|
18
|
+
export function root(tree: Treelike): AsyncTree;
|
|
18
19
|
export function remove(AsyncTree: AsyncMutableTree, key: any): Promise<boolean>;
|
|
19
20
|
export function toFunction(tree: Treelike): Function;
|
|
20
21
|
export function traverse(tree: Treelike, ...keys: any[]): Promise<any>;
|
package/src/Tree.js
CHANGED
|
@@ -354,6 +354,19 @@ export async function remove(tree, key) {
|
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
356
|
|
|
357
|
+
/**
|
|
358
|
+
* Walk up the `parent` chain to find the root of the tree.
|
|
359
|
+
*
|
|
360
|
+
* @param {AsyncTree} tree
|
|
361
|
+
*/
|
|
362
|
+
export function root(tree) {
|
|
363
|
+
let current = from(tree);
|
|
364
|
+
while (current.parent) {
|
|
365
|
+
current = current.parent;
|
|
366
|
+
}
|
|
367
|
+
return current;
|
|
368
|
+
}
|
|
369
|
+
|
|
357
370
|
/**
|
|
358
371
|
* Returns a function that invokes the tree's `get` method.
|
|
359
372
|
*
|
package/src/drivers/FileTree.js
CHANGED
|
@@ -103,8 +103,11 @@ export default class FileTree {
|
|
|
103
103
|
entries = [];
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
// Add slashes to directory names.
|
|
107
|
+
let names = await Promise.all(
|
|
108
|
+
entries.map(async (entry) =>
|
|
109
|
+
trailingSlash.toggle(entry.name, await isDirectory(entry, this.dirname))
|
|
110
|
+
)
|
|
108
111
|
);
|
|
109
112
|
|
|
110
113
|
// Filter out unhelpful file names.
|
|
@@ -211,6 +214,23 @@ export default class FileTree {
|
|
|
211
214
|
}
|
|
212
215
|
}
|
|
213
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Return true if the entry is a directory or is a symbolic link to a directory.
|
|
219
|
+
*/
|
|
220
|
+
async function isDirectory(entry, dirname) {
|
|
221
|
+
if (entry.isSymbolicLink()) {
|
|
222
|
+
const entryPath = path.resolve(dirname, entry.name);
|
|
223
|
+
try {
|
|
224
|
+
const realPath = await fs.realpath(entryPath);
|
|
225
|
+
entry = await fs.stat(realPath);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
// The slash isn't crucial, so if link doesn't work that's okay
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return entry.isDirectory();
|
|
232
|
+
}
|
|
233
|
+
|
|
214
234
|
/**
|
|
215
235
|
* Return true if the object is a string or object with a non-trival `toString`
|
|
216
236
|
* method.
|
package/src/operations/concat.js
CHANGED
|
@@ -18,9 +18,17 @@ export default async function concatTreeValues(treelike) {
|
|
|
18
18
|
|
|
19
19
|
const strings = [];
|
|
20
20
|
for await (const value of deepValuesIterator(treelike, { expand: true })) {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
let string;
|
|
22
|
+
if (value === null) {
|
|
23
|
+
console.warn("Warning: Origami template encountered a null value");
|
|
24
|
+
string = "null";
|
|
25
|
+
} else if (value === undefined) {
|
|
26
|
+
console.warn("Warning: Origami template encountered an undefined value");
|
|
27
|
+
string = "undefined";
|
|
28
|
+
} else {
|
|
29
|
+
string = toString(value);
|
|
23
30
|
}
|
|
31
|
+
strings.push(string);
|
|
24
32
|
}
|
|
25
33
|
return strings.join("");
|
|
26
34
|
}
|