@weborigami/async-tree 0.0.67-beta.2 → 0.0.68

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.67-beta.2",
3
+ "version": "0.0.68",
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.0.67-beta.2"
14
+ "@weborigami/types": "0.0.68"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/src/Tree.js CHANGED
@@ -332,14 +332,9 @@ export async function plain(treelike) {
332
332
  if (tree instanceof ObjectTree && keys.length === 0) {
333
333
  return tree.object instanceof Array ? [] : {};
334
334
  }
335
- const object = {};
336
- for (let i = 0; i < keys.length; i++) {
337
- // Normalize slashes in keys.
338
- const key = trailingSlash.remove(keys[i]);
339
- const value = values[i];
340
- object[key] = value;
341
- }
342
- return castArrayLike(object);
335
+ // Normalize slashes in keys.
336
+ keys = keys.map(trailingSlash.remove);
337
+ return castArrayLike(keys, values);
343
338
  });
344
339
  }
345
340
 
@@ -2,7 +2,7 @@ import { AsyncTree } from "@weborigami/types";
2
2
  import { Packed, PlainObject, StringLike } from "../index.ts";
3
3
 
4
4
  export function box(value: any): any;
5
- export function castArrayLike(object: any): any;
5
+ export function castArrayLike(keys: any[], values: any[]): any;
6
6
  export function getRealmObjectPrototype(object: any): any;
7
7
  export const hiddenFileNames: string[];
8
8
  export function isPacked(obj: any): obj is Packed;
package/src/utilities.js CHANGED
@@ -27,24 +27,38 @@ export function box(value) {
27
27
  }
28
28
 
29
29
  /**
30
- * If the given plain object has only sequential integer keys, return it as an
31
- * array. Otherwise return it as is.
30
+ * Create an array or plain object from the given keys and values.
32
31
  *
33
- * @param {any} object
32
+ * If the given plain object has only sequential integer keys, return the
33
+ * values as an array. Otherwise, create a plain object with the keys and
34
+ * values.
35
+ *
36
+ * @param {any[]} keys
37
+ * @param {any[]} values
34
38
  */
35
- export function castArrayLike(object) {
36
- let hasKeys = false;
37
- let expectedIndex = 0;
38
- for (const key in object) {
39
- hasKeys = true;
40
- const index = Number(key);
41
- if (key === "" || isNaN(index) || index !== expectedIndex) {
42
- // Not an array-like object.
43
- return object;
39
+ export function castArrayLike(keys, values) {
40
+ let isArrayLike = false;
41
+
42
+ // Need at least one key to count as an array
43
+ if (keys.length > 0) {
44
+ // Assume it's an array
45
+ isArrayLike = true;
46
+ // Then check if all the keys are sequential integers
47
+ let expectedIndex = 0;
48
+ for (const key of keys) {
49
+ const index = Number(key);
50
+ if (key === "" || isNaN(index) || index !== expectedIndex) {
51
+ // Not array-like
52
+ isArrayLike = false;
53
+ break;
54
+ }
55
+ expectedIndex++;
44
56
  }
45
- expectedIndex++;
46
57
  }
47
- return hasKeys ? Object.values(object) : object;
58
+
59
+ return isArrayLike
60
+ ? values
61
+ : Object.fromEntries(keys.map((key, i) => [key, values[i]]));
48
62
  }
49
63
 
50
64
  /**