@weborigami/async-tree 0.0.70 → 0.0.71-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/index.ts CHANGED
@@ -19,7 +19,9 @@ export type HasString = {
19
19
  * A packed value is one that can be written to a file via fs.writeFile or into
20
20
  * an HTTP response via response.write, or readily converted to such a form.
21
21
  */
22
- export type Packed = ArrayBuffer | Buffer | ReadableStream | string | String | TypedArray;
22
+ export type Packed = (ArrayBuffer | Buffer | ReadableStream | string | String | TypedArray) & {
23
+ unpack?(): Promise<any>;
24
+ };
23
25
 
24
26
  export type PlainObject = {
25
27
  [key: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.0.70",
3
+ "version": "0.0.71-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.6.2"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.0.70"
14
+ "@weborigami/types": "0.0.71-beta.1"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
@@ -18,6 +18,12 @@ export default function createGroupByTransform(groupKeyFn) {
18
18
  }
19
19
 
20
20
  const tree = Tree.from(treelike);
21
+
22
+ const keys = Array.from(await tree.keys());
23
+
24
+ // Are all the keys integers?
25
+ const isArray = keys.every((key) => !Number.isNaN(parseInt(key)));
26
+
21
27
  const result = {};
22
28
  for (const key of await tree.keys()) {
23
29
  const value = await tree.get(key);
@@ -35,10 +41,14 @@ export default function createGroupByTransform(groupKeyFn) {
35
41
  groups = Tree.from(groups);
36
42
 
37
43
  // Add the value to each group.
38
- for (const groupKey of await groups.keys()) {
39
- const group = await groups.get(groupKey);
40
- result[group] ??= [];
41
- result[group].push(value);
44
+ for (const group of await Tree.values(groups)) {
45
+ if (isArray) {
46
+ result[group] ??= [];
47
+ result[group].push(value);
48
+ } else {
49
+ result[group] ??= {};
50
+ result[group][key] = value;
51
+ }
42
52
  }
43
53
  }
44
54
 
@@ -4,7 +4,7 @@ import { Tree } from "../../src/internal.js";
4
4
  import groupFn from "../../src/operations/groupFn.js";
5
5
 
6
6
  describe("groupFn transform", () => {
7
- test("groups using a group key function", async () => {
7
+ test("groups an array using a group key function", async () => {
8
8
  const fonts = [
9
9
  { name: "Aboreto", tags: ["Sans Serif"] },
10
10
  { name: "Albert Sans", tags: ["Geometric", "Sans Serif"] },
@@ -24,4 +24,31 @@ describe("groupFn transform", () => {
24
24
  Serif: [{ name: "Alegreya", tags: ["Serif"] }],
25
25
  });
26
26
  });
27
+
28
+ test("groups an object using a group key function", async () => {
29
+ const fonts = {
30
+ Aboreto: { tags: ["Sans Serif"] },
31
+ "Albert Sans": { tags: ["Geometric", "Sans Serif"] },
32
+ Alegreya: { tags: ["Serif"] },
33
+ "Work Sans": { tags: ["Grotesque", "Sans Serif"] },
34
+ };
35
+ const tree = Tree.from(fonts);
36
+ const grouped = await groupFn((value, key, tree) => value.tags)(tree);
37
+ assert.deepEqual(await Tree.plain(grouped), {
38
+ Geometric: {
39
+ "Albert Sans": { tags: ["Geometric", "Sans Serif"] },
40
+ },
41
+ Grotesque: {
42
+ "Work Sans": { tags: ["Grotesque", "Sans Serif"] },
43
+ },
44
+ "Sans Serif": {
45
+ Aboreto: { tags: ["Sans Serif"] },
46
+ "Albert Sans": { tags: ["Geometric", "Sans Serif"] },
47
+ "Work Sans": { tags: ["Grotesque", "Sans Serif"] },
48
+ },
49
+ Serif: {
50
+ Alegreya: { tags: ["Serif"] },
51
+ },
52
+ });
53
+ });
27
54
  });