@hardlydifficult/collections 1.0.0 → 1.0.2
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/README.md +58 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/collections
|
|
2
2
|
|
|
3
|
-
Array and
|
|
3
|
+
Array and path-manipulation utilities for batched parallel processing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,11 +8,27 @@ Array and collection utilities for batched parallel processing.
|
|
|
8
8
|
npm install @hardlydifficult/collections
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Process items in parallel batches using chunking:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { chunk } from "@hardlydifficult/collections";
|
|
17
|
+
|
|
18
|
+
const items = [1, 2, 3, 4, 5, 6, 7];
|
|
19
|
+
const batches = chunk(items, 3);
|
|
20
|
+
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
21
|
+
|
|
22
|
+
for (const batch of batches) {
|
|
23
|
+
await Promise.allSettled(batch.map(processItem));
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Chunking Arrays
|
|
12
28
|
|
|
13
29
|
### `chunk<T>(arr: readonly T[], size: number): T[][]`
|
|
14
30
|
|
|
15
|
-
Split an array into
|
|
31
|
+
Split an array into subarrays of a specified maximum size. The final chunk may be smaller than the specified size. Useful for limiting concurrency in batched operations.
|
|
16
32
|
|
|
17
33
|
```typescript
|
|
18
34
|
import { chunk } from "@hardlydifficult/collections";
|
|
@@ -26,23 +42,58 @@ for (const batch of batches) {
|
|
|
26
42
|
}
|
|
27
43
|
```
|
|
28
44
|
|
|
45
|
+
#### Examples
|
|
46
|
+
|
|
47
|
+
- **Full-sized chunks only**: `chunk([1,2,3,4,5,6], 2)` → `[[1,2], [3,4], [5,6]]`
|
|
48
|
+
- **Final smaller chunk**: `chunk([1,2,3,4,5], 3)` → `[[1,2,3], [4,5]]`
|
|
49
|
+
- **Single oversized chunk**: `chunk([1,2,3], 5)` → `[[1,2,3]]`
|
|
50
|
+
- **Empty input**: `chunk([], 3)` → `[]`
|
|
51
|
+
|
|
52
|
+
## Grouping by Depth
|
|
53
|
+
|
|
29
54
|
### `groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]`
|
|
30
55
|
|
|
31
|
-
Group `/`-separated path strings by their depth, sorted deepest-first. Useful for bottom-up directory processing where
|
|
56
|
+
Group `/`-separated path strings by their directory depth (number of `/`-separated segments), sorted deepest-first. Useful for bottom-up directory processing where child directories must be handled before parents.
|
|
32
57
|
|
|
33
58
|
```typescript
|
|
34
59
|
import { groupByDepth } from "@hardlydifficult/collections";
|
|
35
60
|
|
|
36
61
|
const dirs = ["src/services/summarize", "src/services", "src", "src/utils"];
|
|
37
|
-
groupByDepth(dirs);
|
|
62
|
+
const grouped = groupByDepth(dirs);
|
|
38
63
|
// [
|
|
39
64
|
// { depth: 3, paths: ["src/services/summarize"] },
|
|
40
65
|
// { depth: 2, paths: ["src/services", "src/utils"] },
|
|
41
66
|
// { depth: 1, paths: ["src"] },
|
|
42
67
|
// ]
|
|
43
68
|
|
|
44
|
-
|
|
45
|
-
for (const { paths: dirsAtDepth } of groupByDepth(dirs)) {
|
|
69
|
+
for (const { paths: dirsAtDepth } of grouped) {
|
|
46
70
|
await Promise.allSettled(dirsAtDepth.map(summarizeDir));
|
|
47
71
|
}
|
|
48
72
|
```
|
|
73
|
+
|
|
74
|
+
#### Depth Rules
|
|
75
|
+
|
|
76
|
+
- An empty string (`""`) is treated as depth `0` (root)
|
|
77
|
+
- Each `/` increases depth by 1
|
|
78
|
+
- Paths are grouped by depth, and groups are sorted deepest-first
|
|
79
|
+
|
|
80
|
+
#### Examples
|
|
81
|
+
|
|
82
|
+
- **Mixed depths**:
|
|
83
|
+
```typescript
|
|
84
|
+
groupByDepth(["a/b/c", "a/b", "a", ""]);
|
|
85
|
+
// [
|
|
86
|
+
// { depth: 3, paths: ["a/b/c"] },
|
|
87
|
+
// { depth: 2, paths: ["a/b"] },
|
|
88
|
+
// { depth: 1, paths: ["a"] },
|
|
89
|
+
// { depth: 0, paths: [""] },
|
|
90
|
+
// ]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- **Same depth grouping**:
|
|
94
|
+
```typescript
|
|
95
|
+
groupByDepth(["x/y", "a/b", "m/n"]);
|
|
96
|
+
// [{ depth: 2, paths: ["x/y", "a/b", "m/n"] }]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
- **Empty input**: `groupByDepth([])` → `[]`
|