@hardlydifficult/collections 1.0.5 → 1.0.6

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.
Files changed (2) hide show
  1. package/README.md +38 -84
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @hardlydifficult/collections
2
2
 
3
- A TypeScript utility library providing array chunking and path depth grouping utilities for batched parallel processing.
3
+ A TypeScript utility library providing array chunking and path depth grouping functions.
4
4
 
5
5
  ## Installation
6
6
 
@@ -13,115 +13,69 @@ npm install @hardlydifficult/collections
13
13
  ```typescript
14
14
  import { chunk, groupByDepth } from "@hardlydifficult/collections";
15
15
 
16
- // Split an array into fixed-size chunks
17
- const result = chunk([1, 2, 3, 4, 5], 2);
16
+ // Split an array into chunks
17
+ const items = [1, 2, 3, 4, 5];
18
+ console.log(chunk(items, 2));
18
19
  // → [[1, 2], [3, 4], [5]]
19
20
 
20
- // Group filesystem paths by directory depth (deepest-first)
21
- const paths = ["src/a/b", "src", "src/c"];
22
- const grouped = groupByDepth(paths);
23
- // → [{ depth: 2, paths: ["src/a/b"] }, { depth: 1, paths: ["src", "src/c"] }]
21
+ // Group filesystem paths by directory depth
22
+ const paths = ["src/app", "src", "src/utils/helper"];
23
+ console.log(groupByDepth(paths));
24
+ // → [{ depth: 3, paths: ["src/app"] }, { depth: 2, paths: ["src"] }, { depth: 1, paths: ["src/utils/helper"] }]
24
25
  ```
25
26
 
26
- Process items in parallel batches using chunking:
27
+ ## Chunk Arrays
27
28
 
28
- ```typescript
29
- import { chunk } from "@hardlydifficult/collections";
30
-
31
- const items = [1, 2, 3, 4, 5, 6, 7];
32
- const batches = chunk(items, 3);
33
- // [[1, 2, 3], [4, 5, 6], [7]]
34
-
35
- for (const batch of batches) {
36
- await Promise.allSettled(batch.map(processItem));
37
- }
38
- ```
29
+ Split an array into fixed-size subarrays.
39
30
 
40
- ## Array Chunking (`chunk`)
31
+ ### `chunk`
41
32
 
42
- Splits an array into sub-arrays of a specified maximum size, preserving order.
33
+ Splits an array into chunks of a specified maximum size. The last chunk may be smaller than the requested size.
43
34
 
44
35
  ```typescript
45
36
  import { chunk } from "@hardlydifficult/collections";
46
37
 
47
- const numbers = [1, 2, 3, 4, 5, 6, 7];
48
- console.log(chunk(numbers, 3));
38
+ const result = chunk([1, 2, 3, 4, 5, 6, 7], 3);
49
39
  // → [[1, 2, 3], [4, 5, 6], [7]]
50
40
  ```
51
41
 
52
- | Parameter | Type | Description |
53
- |---------|------|-------------|
54
- | `arr` | `readonly T[]` | Input array to split (supports readonly arrays) |
55
- | `size` | `number` | Maximum chunk size (must be ≥1) |
42
+ **Signature**
43
+
44
+ | Parameter | Type | Description |
45
+ |-----------|-------------------|--------------------------|
46
+ | `arr` | `readonly T[]` | Input array to chunk |
47
+ | `size` | `number` | Maximum chunk size |
48
+ | **Returns** | `T[][]` | Array of subarrays |
56
49
 
57
- Returns `T[][]`: An array of chunks, where the last chunk may be smaller if the input length is not evenly divisible by `size`.
50
+ ## Group Paths by Depth
58
51
 
59
- **Examples:**
60
- - **Full-sized chunks only**: `chunk([1,2,3,4,5,6], 2)` → `[[1,2], [3,4], [5,6]]`
61
- - **Final smaller chunk**: `chunk([1,2,3,4,5], 3)` → `[[1,2,3], [4,5]]`
62
- - **Single oversized chunk**: `chunk([1,2,3], 5)` → `[[1,2,3]]`
63
- - **Empty input**: `chunk([], 3)` → `[]`
52
+ Organize filesystem paths by slash-delimited depth, sorted deepest-first for bottom-up directory processing.
64
53
 
65
- ## Path Depth Grouping (`groupByDepth`)
54
+ ### `groupByDepth`
66
55
 
67
- Groups filesystem paths by slash-delimited depth, sorted deepest-first to support bottom-up directory processing.
56
+ Groups an array of path strings by the number of `/`-separated segments. Paths with deeper nesting appear first in the result, enabling bottom-up processing.
68
57
 
69
58
  ```typescript
70
59
  import { groupByDepth } from "@hardlydifficult/collections";
71
60
 
72
- const paths = ["src/a/b", "src", "src/c", ""];
61
+ const paths = ["src/app/index.ts", "src/app", "src", "README.md"];
73
62
  const result = groupByDepth(paths);
74
63
  // → [
75
- // { depth: 3, paths: ["src/a/b"] },
76
- // { depth: 2, paths: ["src", "src/c"] },
77
- // { depth: 0, paths: [""] }
78
- // ]
64
+ // { depth: 3, paths: ["src/app/index.ts"] },
65
+ // { depth: 2, paths: ["src/app"] },
66
+ // { depth: 1, paths: ["src", "README.md"] }
67
+ // ]
79
68
  ```
80
69
 
81
- | Parameter | Type | Description |
82
- |---------|------|-------------|
83
- | `paths` | `readonly string[]` | Array of path strings (slashes `/` delimit depth) |
84
-
85
- Returns `{ depth: number; paths: string[] }[]`: An array of depth groups sorted in descending order by depth. Empty string `""` is treated as depth `0` (root). Order of paths within each group is preserved from the input.
70
+ **Signature**
86
71
 
87
- **Rules:**
88
- - An empty string (`""`) is treated as depth `0` (root)
89
- - Each `/` increases depth by 1
90
- - Paths are grouped by depth, and groups are sorted deepest-first
72
+ | Parameter | Type | Description |
73
+ |-----------|--------------------|------------------------------------|
74
+ | `paths` | `readonly string[]`| Array of path strings |
75
+ | **Returns** | `{ depth: number; paths: string[] }[]` | Groups sorted deepest-first |
91
76
 
92
- **Examples:**
93
-
94
- - **Mixed depths**:
95
- ```typescript
96
- groupByDepth(["a/b/c", "a/b", "a", ""]);
97
- // [
98
- // { depth: 3, paths: ["a/b/c"] },
99
- // { depth: 2, paths: ["a/b"] },
100
- // { depth: 1, paths: ["a"] },
101
- // { depth: 0, paths: [""] },
102
- // ]
103
- ```
104
-
105
- - **Same depth grouping**:
106
- ```typescript
107
- groupByDepth(["x/y", "a/b", "m/n"]);
108
- // [{ depth: 2, paths: ["x/y", "a/b", "m/n"] }]
109
- ```
110
-
111
- - **Empty input**: `groupByDepth([])` → `[]`
112
-
113
- ```typescript
114
- import { groupByDepth } from "@hardlydifficult/collections";
115
-
116
- const dirs = ["src/services/summarize", "src/services", "src", "src/utils"];
117
- const grouped = groupByDepth(dirs);
118
- // [
119
- // { depth: 3, paths: ["src/services/summarize"] },
120
- // { depth: 2, paths: ["src/services", "src/utils"] },
121
- // { depth: 1, paths: ["src"] },
122
- // ]
77
+ **Notes**
123
78
 
124
- for (const { paths: dirsAtDepth } of grouped) {
125
- await Promise.allSettled(dirsAtDepth.map(summarizeDir));
126
- }
127
- ```
79
+ - Empty string paths (`""`) are assigned depth `0`.
80
+ - Paths are grouped in descending order of depth (deepest first).
81
+ - Order of paths within each depth group matches their appearance in the input array.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/collections",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [