@weborigami/async-tree 0.2.1 → 0.2.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jan Miksovsky and other contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
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
  "devDependencies": {
10
- "@types/node": "22.7.4",
11
- "typescript": "5.6.2"
10
+ "@types/node": "22.10.2",
11
+ "typescript": "5.7.2"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.2.1"
14
+ "@weborigami/types": "0.2.3"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/shared.js CHANGED
@@ -29,4 +29,5 @@ export { default as sort } from "./src/operations/sort.js";
29
29
  export { default as take } from "./src/operations/take.js";
30
30
  export * as symbols from "./src/symbols.js";
31
31
  export * as trailingSlash from "./src/trailingSlash.js";
32
+ export { default as TraverseError } from "./src/TraverseError.js";
32
33
  export * from "./src/utilities.js";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Error class thrown by Tree.traverseOrThrow()
3
+ */
4
+ export default class TraverseError extends ReferenceError {
5
+ constructor(message, options) {
6
+ super(message);
7
+ this.name = "TraverseError";
8
+ Object.assign(this, options);
9
+ }
10
+ }
package/src/Tree.js CHANGED
@@ -5,6 +5,7 @@ import SetTree from "./drivers/SetTree.js";
5
5
  import { DeepObjectTree, ObjectTree } from "./internal.js";
6
6
  import * as symbols from "./symbols.js";
7
7
  import * as trailingSlash from "./trailingSlash.js";
8
+ import TraverseError from "./TraverseError.js";
8
9
  import * as utilities from "./utilities.js";
9
10
  import {
10
11
  castArrayLike,
@@ -476,15 +477,6 @@ export async function traversePath(tree, path) {
476
477
  return traverse(tree, ...keys);
477
478
  }
478
479
 
479
- // Error class thrown by traverseOrThrow()
480
- class TraverseError extends ReferenceError {
481
- constructor(message, options) {
482
- super(message);
483
- this.name = "TraverseError";
484
- Object.assign(this, options);
485
- }
486
- }
487
-
488
480
  /**
489
481
  * Return the values in the specific node of the tree.
490
482
  *
@@ -180,15 +180,7 @@ export default class FileTree {
180
180
  }
181
181
 
182
182
  if (packed) {
183
- // Single writeable value.
184
- if (value instanceof ArrayBuffer) {
185
- // Convert ArrayBuffer to Uint8Array, which Node.js can write directly.
186
- value = new Uint8Array(value);
187
- }
188
- // Ensure this directory exists.
189
- await fs.mkdir(this.dirname, { recursive: true });
190
- // Write out the value as the contents of a file.
191
- await fs.writeFile(destPath, value);
183
+ await writeFile(value, destPath);
192
184
  } else if (isPlainObject(value) && Object.keys(value).length === 0) {
193
185
  // Special case: empty object means create an empty directory.
194
186
  await fs.mkdir(destPath, { recursive: true });
@@ -266,3 +258,26 @@ async function stat(filePath) {
266
258
  throw error;
267
259
  }
268
260
  }
261
+
262
+ // Write a value to a file.
263
+ async function writeFile(value, destPath) {
264
+ // Single writeable value.
265
+ if (value instanceof ArrayBuffer) {
266
+ // Convert ArrayBuffer to Uint8Array, which Node.js can write directly.
267
+ value = new Uint8Array(value);
268
+ }
269
+ // Ensure this directory exists.
270
+ const dirname = path.dirname(destPath);
271
+ await fs.mkdir(dirname, { recursive: true });
272
+
273
+ // Write out the value as the contents of a file.
274
+ try {
275
+ await fs.writeFile(destPath, value);
276
+ } catch (/** @type {any} */ error) {
277
+ if (error.code === "EISDIR" /* Is a directory */) {
278
+ throw new Error(
279
+ `Tried to overwrite a directory with a single file: ${destPath}`
280
+ );
281
+ }
282
+ }
283
+ }
@@ -66,6 +66,11 @@ export default class ObjectTree {
66
66
  * Enumerate the object's keys.
67
67
  */
68
68
  async keys() {
69
+ // Defer to symbols.keys if defined
70
+ if (typeof this.object[symbols.keys] === "function") {
71
+ return this.object[symbols.keys]();
72
+ }
73
+
69
74
  // Walk up the prototype chain to realm's Object.prototype.
70
75
  let obj = this.object;
71
76
  const objectPrototype = getRealmObjectPrototype(obj);
package/src/symbols.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export const deep = Symbol("deep");
2
+ export const keys = Symbol("keys");
2
3
  export const parent = Symbol("parent");
@@ -44,11 +44,11 @@ assert.deepEqual = (actual, expected) => {
44
44
  throw new Error(`Expected ${expected} but got ${actual}`);
45
45
  };
46
46
 
47
- assert.rejects = async (promise) => {
47
+ assert.rejects = async (fn) => {
48
48
  try {
49
- await promise;
50
- throw new Error("Expected promise to reject but it resolved");
49
+ await fn();
51
50
  } catch (error) {
52
51
  return;
53
52
  }
53
+ throw new Error("Expected promise to reject but it resolved");
54
54
  };
@@ -13,22 +13,22 @@
13
13
  </script>
14
14
  <!-- Omit FileTree.test.js, which is Node.js only -->
15
15
  <!-- Omit SiteTree.test.js, which requires mocks -->
16
- <script type="module" src="../BrowserFileTree.test.js"></script>
17
- <script type="module" src="../DeferredTree.test.js"></script>
18
- <script type="module" src="../FunctionTree.test.js"></script>
19
- <script type="module" src="../MapTree.test.js"></script>
20
- <script type="module" src="../ObjectTree.test.js"></script>
21
- <script type="module" src="../SetTree.test.js"></script>
22
16
  <script type="module" src="../Tree.test.js"></script>
17
+ <script type="module" src="../drivers/BrowserFileTree.test.js"></script>
18
+ <script type="module" src="../drivers/DeferredTree.test.js"></script>
19
+ <script type="module" src="../drivers/FunctionTree.test.js"></script>
20
+ <script type="module" src="../drivers/MapTree.test.js"></script>
21
+ <script type="module" src="../drivers/ObjectTree.test.js"></script>
22
+ <script type="module" src="../drivers/SetTree.test.js"></script>
23
23
  <script type="module" src="../operations/cache.test.js"></script>
24
24
  <script type="module" src="../operations/merge.test.js"></script>
25
25
  <script type="module" src="../operations/deepMerge.test.js"></script>
26
- <script type="module" src="../transforms/cachedKeyMaps.test.js"></script>
26
+ <script type="module" src="../operations/cachedKeyFunctions.test.js"></script>
27
27
  <script
28
28
  type="module"
29
- src="../transforms/keyFunctionsForExtensions.test.js"
29
+ src="../operations/keyFunctionsForExtensions.test.js"
30
30
  ></script>
31
- <script type="module" src="../transforms/mapFn.test.js"></script>
31
+ <script type="module" src="../operations/map.test.js"></script>
32
32
  <script type="module" src="../utilities.test.js"></script>
33
33
  </head>
34
34
  <body></body>
@@ -145,7 +145,12 @@ async function createFixture() {
145
145
  }
146
146
 
147
147
  async function strings(tree) {
148
- return Tree.plain(Tree.map(tree, (value) => text(value)));
148
+ return Tree.plain(
149
+ Tree.map(tree, {
150
+ deep: true,
151
+ value: (value) => text(value),
152
+ })
153
+ );
149
154
  }
150
155
 
151
156
  function text(arrayBuffer) {
@@ -145,6 +145,13 @@ describe("ObjectTree", () => {
145
145
  const method = await tree.get("toString");
146
146
  assert.equal(method(), "123");
147
147
  });
148
+
149
+ test("defers to symbols.keys for keys if defined", async () => {
150
+ const tree = new ObjectTree({
151
+ [symbols.keys]: () => ["a", "b", "c"],
152
+ });
153
+ assert.deepEqual(Array.from(await tree.keys()), ["a", "b", "c"]);
154
+ });
148
155
  });
149
156
 
150
157
  function createFixture() {