@hardlydifficult/collections 1.0.1 → 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 +36 -16
- 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
|
|
|
@@ -10,7 +10,7 @@ npm install @hardlydifficult/collections
|
|
|
10
10
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Process items in parallel batches using chunking:
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
import { chunk } from "@hardlydifficult/collections";
|
|
@@ -28,7 +28,7 @@ for (const batch of batches) {
|
|
|
28
28
|
|
|
29
29
|
### `chunk<T>(arr: readonly T[], size: number): T[][]`
|
|
30
30
|
|
|
31
|
-
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.
|
|
32
32
|
|
|
33
33
|
```typescript
|
|
34
34
|
import { chunk } from "@hardlydifficult/collections";
|
|
@@ -37,17 +37,23 @@ const items = [1, 2, 3, 4, 5, 6, 7];
|
|
|
37
37
|
const batches = chunk(items, 3);
|
|
38
38
|
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
39
39
|
|
|
40
|
-
// Process in parallel batches
|
|
41
40
|
for (const batch of batches) {
|
|
42
41
|
await Promise.allSettled(batch.map(processItem));
|
|
43
42
|
}
|
|
44
43
|
```
|
|
45
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
|
+
|
|
46
52
|
## Grouping by Depth
|
|
47
53
|
|
|
48
54
|
### `groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]`
|
|
49
55
|
|
|
50
|
-
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.
|
|
51
57
|
|
|
52
58
|
```typescript
|
|
53
59
|
import { groupByDepth } from "@hardlydifficult/collections";
|
|
@@ -60,20 +66,34 @@ const grouped = groupByDepth(dirs);
|
|
|
60
66
|
// { depth: 1, paths: ["src"] },
|
|
61
67
|
// ]
|
|
62
68
|
|
|
63
|
-
// Process directories bottom-up, parallelizing within each depth level
|
|
64
69
|
for (const { paths: dirsAtDepth } of grouped) {
|
|
65
70
|
await Promise.allSettled(dirsAtDepth.map(summarizeDir));
|
|
66
71
|
}
|
|
67
72
|
```
|
|
68
73
|
|
|
69
|
-
Depth
|
|
74
|
+
#### Depth Rules
|
|
70
75
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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([])` → `[]`
|