@weborigami/async-tree 0.0.43 → 0.0.45

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
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.3.3"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.0.43"
14
+ "@weborigami/types": "0.0.45"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/src/ObjectTree.js CHANGED
@@ -41,6 +41,11 @@ export default class ObjectTree {
41
41
  value.parent = this;
42
42
  }
43
43
 
44
+ if (typeof value === "function" && !Object.hasOwn(this.object, key)) {
45
+ // Value is an inherited method; bind it to the object.
46
+ value = value.bind(this.object);
47
+ }
48
+
44
49
  return value;
45
50
  }
46
51
 
package/src/SetTree.js CHANGED
@@ -11,7 +11,7 @@ export default class SetTree {
11
11
  * @param {Set} set
12
12
  */
13
13
  constructor(set) {
14
- this.values = [...set];
14
+ this.values = Array.from(set);
15
15
  this.parent = null;
16
16
  }
17
17
 
package/src/Tree.js CHANGED
@@ -74,7 +74,7 @@ export async function clear(tree) {
74
74
  * @param {AsyncTree} tree
75
75
  */
76
76
  export async function entries(tree) {
77
- const keys = [...(await tree.keys())];
77
+ const keys = Array.from(await tree.keys());
78
78
  const promises = keys.map(async (key) => [key, await tree.get(key)]);
79
79
  return Promise.all(promises);
80
80
  }
@@ -87,7 +87,7 @@ export async function entries(tree) {
87
87
  * @param {Function} callbackFn
88
88
  */
89
89
  export async function forEach(tree, callbackFn) {
90
- const keys = [...(await tree.keys())];
90
+ const keys = Array.from(await tree.keys());
91
91
  const promises = keys.map(async (key) => {
92
92
  const value = await tree.get(key);
93
93
  return callbackFn(value, key);
@@ -430,7 +430,7 @@ class TraverseError extends ReferenceError {
430
430
  * @param {AsyncTree} tree
431
431
  */
432
432
  export async function values(tree) {
433
- const keys = [...(await tree.keys())];
433
+ const keys = Array.from(await tree.keys());
434
434
  const promises = keys.map(async (key) => tree.get(key));
435
435
  return Promise.all(promises);
436
436
  }
@@ -100,7 +100,7 @@ export default function createMapTransform(options) {
100
100
  if (keyMap) {
101
101
  transformed.keys = async () => {
102
102
  // Apply the keyMap to source keys for leaf values (not subtrees).
103
- const sourceKeys = [...(await tree.keys())];
103
+ const sourceKeys = Array.from(await tree.keys());
104
104
  const mapped = await Promise.all(
105
105
  sourceKeys.map(async (sourceKey) => {
106
106
  let resultKey;
@@ -29,7 +29,7 @@ export default async function regExpKeys(tree) {
29
29
  },
30
30
 
31
31
  async keys() {
32
- return [...map.keys()];
32
+ return map.keys();
33
33
  },
34
34
  };
35
35
 
@@ -13,7 +13,7 @@ export default function createSortTransform(compareFn) {
13
13
  return function sortTransform(tree) {
14
14
  const transform = Object.create(tree);
15
15
  transform.keys = async () => {
16
- const keys = [...(await tree.keys())];
16
+ const keys = Array.from(await tree.keys());
17
17
  keys.sort(compareFn);
18
18
  return keys;
19
19
  };
@@ -22,7 +22,8 @@ export default function createSortByTransform(sortKeyFn) {
22
22
  keysAndSortKeys.sort((a, b) =>
23
23
  a.sortKey < b.sortKey ? -1 : a.sortKey > b.sortKey ? 1 : 0
24
24
  );
25
- return keysAndSortKeys.map(({ key }) => key);
25
+ const sortedKeys = keysAndSortKeys.map(({ key }) => key);
26
+ return sortedKeys;
26
27
  };
27
28
  return transform;
28
29
  };
@@ -9,10 +9,11 @@ if (isBrowser) {
9
9
  describe("BrowserFileTree", async () => {
10
10
  test("can get the keys of the tree", async () => {
11
11
  const fixture = await createFixture();
12
- assert.deepEqual(
13
- [...(await fixture.keys())],
14
- ["Alice.md", "Bob.md", "Carol.md"]
15
- );
12
+ assert.deepEqual(Array.from(await fixture.keys()), [
13
+ "Alice.md",
14
+ "Bob.md",
15
+ "Carol.md",
16
+ ]);
16
17
  });
17
18
 
18
19
  test("can get the value for a key", async () => {
@@ -12,10 +12,11 @@ const tempDirectory = path.join(dirname, "fixtures/temp");
12
12
  describe("FileTree", async () => {
13
13
  test("can get the keys of the tree", async () => {
14
14
  const fixture = createFixture("fixtures/markdown");
15
- assert.deepEqual(
16
- [...(await fixture.keys())],
17
- ["Alice.md", "Bob.md", "Carol.md"]
18
- );
15
+ assert.deepEqual(Array.from(await fixture.keys()), [
16
+ "Alice.md",
17
+ "Bob.md",
18
+ "Carol.md",
19
+ ]);
19
20
  });
20
21
 
21
22
  test("can get the value for a key", async () => {
@@ -5,10 +5,11 @@ import FunctionTree from "../src/FunctionTree.js";
5
5
  describe("FunctionTree", async () => {
6
6
  test("can get the keys of the tree", async () => {
7
7
  const fixture = createFixture();
8
- assert.deepEqual(
9
- [...(await fixture.keys())],
10
- ["Alice.md", "Bob.md", "Carol.md"]
11
- );
8
+ assert.deepEqual(Array.from(await fixture.keys()), [
9
+ "Alice.md",
10
+ "Bob.md",
11
+ "Carol.md",
12
+ ]);
12
13
  });
13
14
 
14
15
  test("can get the value for a key", async () => {
@@ -5,7 +5,7 @@ import MapTree from "../src/MapTree.js";
5
5
  describe("MapTree", () => {
6
6
  test("can get the keys of the tree", async () => {
7
7
  const fixture = createFixture();
8
- assert.deepEqual([...(await fixture.keys())], ["a", "b", "c"]);
8
+ assert.deepEqual(Array.from(await fixture.keys()), ["a", "b", "c"]);
9
9
  });
10
10
 
11
11
  test("can get the value for a key", async () => {
@@ -6,10 +6,11 @@ import * as Tree from "../src/Tree.js";
6
6
  describe("ObjectTree", () => {
7
7
  test("can get the keys of the tree", async () => {
8
8
  const fixture = createFixture();
9
- assert.deepEqual(
10
- [...(await fixture.keys())],
11
- ["Alice.md", "Bob.md", "Carol.md"]
12
- );
9
+ assert.deepEqual(Array.from(await fixture.keys()), [
10
+ "Alice.md",
11
+ "Bob.md",
12
+ "Carol.md",
13
+ ]);
13
14
  });
14
15
 
15
16
  test("can get the value for a key", async () => {
@@ -115,6 +116,13 @@ describe("ObjectTree", () => {
115
116
  });
116
117
  assert.equal(await tree.get("subtree"), subtree);
117
118
  });
119
+
120
+ test("method on an object is bound to the object", async () => {
121
+ const n = new Number(123);
122
+ const tree = new ObjectTree(n);
123
+ const method = await tree.get("toString");
124
+ assert.equal(method(), "123");
125
+ });
118
126
  });
119
127
 
120
128
  function createFixture() {
@@ -7,7 +7,7 @@ describe("SetTree", () => {
7
7
  test("can get the keys of the tree", async () => {
8
8
  const set = new Set(["a", "b", "c"]);
9
9
  const fixture = new SetTree(set);
10
- assert.deepEqual([...(await fixture.keys())], [0, 1, 2]);
10
+ assert.deepEqual(Array.from(await fixture.keys()), [0, 1, 2]);
11
11
  });
12
12
 
13
13
  test("can get the value for a key", async () => {
@@ -43,7 +43,11 @@ describe("SiteTree", () => {
43
43
  const fixture = new SiteTree(mockHost);
44
44
  const about = fixture.resolve("about");
45
45
  const keys = await about.keys();
46
- assert.deepEqual([...keys], ["Alice.html", "Bob.html", "Carol.html"]);
46
+ assert.deepEqual(Array.from(keys), [
47
+ "Alice.html",
48
+ "Bob.html",
49
+ "Carol.html",
50
+ ]);
47
51
  });
48
52
 
49
53
  test("can get the value for a key", async () => {
package/test/Tree.test.js CHANGED
@@ -55,19 +55,16 @@ describe("Tree", () => {
55
55
  test("clear() removes all values", async () => {
56
56
  const fixture = createFixture();
57
57
  await Tree.clear(fixture);
58
- assert.deepEqual([...(await Tree.entries(fixture))], []);
58
+ assert.deepEqual(Array.from(await Tree.entries(fixture)), []);
59
59
  });
60
60
 
61
61
  test("entries() returns the [key, value] pairs", async () => {
62
62
  const fixture = createFixture();
63
- assert.deepEqual(
64
- [...(await Tree.entries(fixture))],
65
- [
66
- ["Alice.md", "Hello, **Alice**."],
67
- ["Bob.md", "Hello, **Bob**."],
68
- ["Carol.md", "Hello, **Carol**."],
69
- ]
70
- );
63
+ assert.deepEqual(Array.from(await Tree.entries(fixture)), [
64
+ ["Alice.md", "Hello, **Alice**."],
65
+ ["Bob.md", "Hello, **Bob**."],
66
+ ["Carol.md", "Hello, **Carol**."],
67
+ ]);
71
68
  });
72
69
 
73
70
  test("forEach() invokes a callback for each entry", async () => {
@@ -246,13 +243,10 @@ describe("Tree", () => {
246
243
  test("remove method removes a value", async () => {
247
244
  const fixture = createFixture();
248
245
  await Tree.remove(fixture, "Alice.md");
249
- assert.deepEqual(
250
- [...(await Tree.entries(fixture))],
251
- [
252
- ["Bob.md", "Hello, **Bob**."],
253
- ["Carol.md", "Hello, **Carol**."],
254
- ]
255
- );
246
+ assert.deepEqual(Array.from(await Tree.entries(fixture)), [
247
+ ["Bob.md", "Hello, **Bob**."],
248
+ ["Carol.md", "Hello, **Carol**."],
249
+ ]);
256
250
  });
257
251
 
258
252
  test("toFunction returns a function that invokes a tree's get() method", async () => {
@@ -337,10 +331,11 @@ describe("Tree", () => {
337
331
 
338
332
  test("values() returns the store's values", async () => {
339
333
  const fixture = createFixture();
340
- assert.deepEqual(
341
- [...(await Tree.values(fixture))],
342
- ["Hello, **Alice**.", "Hello, **Bob**.", "Hello, **Carol**."]
343
- );
334
+ assert.deepEqual(Array.from(await Tree.values(fixture)), [
335
+ "Hello, **Alice**.",
336
+ "Hello, **Bob**.",
337
+ "Hello, **Carol**.",
338
+ ]);
344
339
  });
345
340
  });
346
341