@hardlydifficult/collections 1.0.0 → 1.0.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/README.md +35 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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
|
+
Split an array into chunks for parallel batch processing:
|
|
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 chunks of a given size. Useful for processing items in parallel batches with a concurrency limit.
|
|
31
|
+
Split an array into chunks of a given size. The final chunk may be smaller than the specified size. Useful for processing items in parallel batches with a concurrency limit.
|
|
16
32
|
|
|
17
33
|
```typescript
|
|
18
34
|
import { chunk } from "@hardlydifficult/collections";
|
|
@@ -21,11 +37,14 @@ const items = [1, 2, 3, 4, 5, 6, 7];
|
|
|
21
37
|
const batches = chunk(items, 3);
|
|
22
38
|
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
23
39
|
|
|
40
|
+
// Process in parallel batches
|
|
24
41
|
for (const batch of batches) {
|
|
25
42
|
await Promise.allSettled(batch.map(processItem));
|
|
26
43
|
}
|
|
27
44
|
```
|
|
28
45
|
|
|
46
|
+
## Grouping by Depth
|
|
47
|
+
|
|
29
48
|
### `groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]`
|
|
30
49
|
|
|
31
50
|
Group `/`-separated path strings by their depth, sorted deepest-first. Useful for bottom-up directory processing where children must be handled before parents.
|
|
@@ -34,7 +53,7 @@ Group `/`-separated path strings by their depth, sorted deepest-first. Useful fo
|
|
|
34
53
|
import { groupByDepth } from "@hardlydifficult/collections";
|
|
35
54
|
|
|
36
55
|
const dirs = ["src/services/summarize", "src/services", "src", "src/utils"];
|
|
37
|
-
groupByDepth(dirs);
|
|
56
|
+
const grouped = groupByDepth(dirs);
|
|
38
57
|
// [
|
|
39
58
|
// { depth: 3, paths: ["src/services/summarize"] },
|
|
40
59
|
// { depth: 2, paths: ["src/services", "src/utils"] },
|
|
@@ -42,7 +61,19 @@ groupByDepth(dirs);
|
|
|
42
61
|
// ]
|
|
43
62
|
|
|
44
63
|
// Process directories bottom-up, parallelizing within each depth level
|
|
45
|
-
for (const { paths: dirsAtDepth } of
|
|
64
|
+
for (const { paths: dirsAtDepth } of grouped) {
|
|
46
65
|
await Promise.allSettled(dirsAtDepth.map(summarizeDir));
|
|
47
66
|
}
|
|
48
67
|
```
|
|
68
|
+
|
|
69
|
+
Depth is calculated by counting `/`-separated segments. An empty string is treated as depth 0 (root).
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
groupByDepth(["a/b/c", "a/b", "a", ""]);
|
|
73
|
+
// [
|
|
74
|
+
// { depth: 3, paths: ["a/b/c"] },
|
|
75
|
+
// { depth: 2, paths: ["a/b"] },
|
|
76
|
+
// { depth: 1, paths: ["a"] },
|
|
77
|
+
// { depth: 0, paths: [""] },
|
|
78
|
+
// ]
|
|
79
|
+
```
|