@weborigami/async-tree 0.0.65 → 0.0.66-beta.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.0.65",
3
+ "version": "0.0.66-beta.1",
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.5.4"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.0.65"
14
+ "@weborigami/types": "0.0.66-beta.1"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/src/Tree.d.ts CHANGED
@@ -5,7 +5,7 @@ export function assign(target: Treelike, source: Treelike): Promise<AsyncTree>;
5
5
  export function clear(AsyncTree: AsyncMutableTree): Promise<void>;
6
6
  export function entries(AsyncTree: AsyncTree): Promise<IterableIterator<any>>;
7
7
  export function forEach(AsyncTree: AsyncTree, callbackfn: (value: any, key: any) => Promise<void>): Promise<void>;
8
- export function from(obj: any, options?: { deep: boolean }): AsyncTree;
8
+ export function from(obj: any, options?: { deep?: boolean, parent?: AsyncTree|null }): AsyncTree;
9
9
  export function has(AsyncTree: AsyncTree, key: any): Promise<boolean>;
10
10
  export function isAsyncMutableTree(obj: any): obj is AsyncMutableTree;
11
11
  export function isAsyncTree(obj: any): obj is AsyncTree;
package/src/Tree.js CHANGED
@@ -105,35 +105,38 @@ export async function forEach(tree, callbackFn) {
105
105
  *
106
106
  * If the object is a plain object, it will be converted to an ObjectTree. The
107
107
  * optional `deep` option can be set to `true` to convert a plain object to a
108
- * DeepObjectTree.
108
+ * DeepObjectTree. The optional `parent` parameter will be used as the default
109
+ * parent of the new tree.
109
110
  *
110
111
  * @param {Treelike | Object} object
111
- * @param {{ deep?: true }} [options]
112
+ * @param {{ deep?: true, parent?: AsyncTree|null }} [options]
112
113
  * @returns {AsyncTree}
113
114
  */
114
115
  export function from(object, options = {}) {
116
+ let tree;
115
117
  if (isAsyncTree(object)) {
116
118
  // Argument already supports the tree interface.
117
119
  // @ts-ignore
118
120
  return object;
119
121
  } else if (typeof object === "function") {
120
- return new FunctionTree(object);
122
+ tree = new FunctionTree(object);
121
123
  } else if (object instanceof Map) {
122
- return new MapTree(object);
124
+ tree = new MapTree(object);
123
125
  } else if (object instanceof Set) {
124
- return new SetTree(object);
126
+ tree = new SetTree(object);
125
127
  } else if (isPlainObject(object) || object instanceof Array) {
126
- return options.deep ? new DeepObjectTree(object) : new ObjectTree(object);
128
+ tree = options.deep ? new DeepObjectTree(object) : new ObjectTree(object);
127
129
  } else if (isUnpackable(object)) {
128
130
  async function AsyncFunction() {} // Sample async function
129
- return object.unpack instanceof AsyncFunction.constructor
130
- ? // Async unpack: return a deferred tree.
131
- new DeferredTree(object.unpack)
132
- : // Synchronous unpack: cast the result of unpack() to a tree.
133
- from(object.unpack());
131
+ tree =
132
+ object.unpack instanceof AsyncFunction.constructor
133
+ ? // Async unpack: return a deferred tree.
134
+ new DeferredTree(object.unpack)
135
+ : // Synchronous unpack: cast the result of unpack() to a tree.
136
+ from(object.unpack());
134
137
  } else if (object && typeof object === "object") {
135
138
  // An instance of some class.
136
- return new ObjectTree(object);
139
+ tree = new ObjectTree(object);
137
140
  } else if (
138
141
  typeof object === "string" ||
139
142
  typeof object === "number" ||
@@ -141,10 +144,15 @@ export function from(object, options = {}) {
141
144
  ) {
142
145
  // A primitive value; box it into an object and construct a tree.
143
146
  const boxed = utilities.box(object);
144
- return new ObjectTree(boxed);
147
+ tree = new ObjectTree(boxed);
148
+ } else {
149
+ throw new TypeError("Couldn't convert argument to an async tree");
145
150
  }
146
151
 
147
- throw new TypeError("Couldn't convert argument to an async tree");
152
+ if (!tree.parent && options.parent) {
153
+ tree.parent = options.parent;
154
+ }
155
+ return tree;
148
156
  }
149
157
 
150
158
  /**