@hardlydifficult/collections 1.0.2 → 1.0.4
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 +63 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/collections
|
|
2
2
|
|
|
3
|
-
Array and path
|
|
3
|
+
Array chunking and path depth grouping utilities for batched parallel processing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,6 +10,23 @@ npm install @hardlydifficult/collections
|
|
|
10
10
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
|
+
```typescript
|
|
14
|
+
import { chunk, groupByDepth } from "@hardlydifficult/collections";
|
|
15
|
+
|
|
16
|
+
// Split an array into fixed-size chunks
|
|
17
|
+
const items = [1, 2, 3, 4, 5];
|
|
18
|
+
const chunks = chunk(items, 2);
|
|
19
|
+
// => [[1, 2], [3, 4], [5]]
|
|
20
|
+
|
|
21
|
+
// Group filesystem paths by directory depth (deepest first)
|
|
22
|
+
const paths = ["src/app", "src", "src/utils/helper"];
|
|
23
|
+
const grouped = groupByDepth(paths);
|
|
24
|
+
// => [
|
|
25
|
+
// { depth: 3, paths: ["src/app"] },
|
|
26
|
+
// { depth: 1, paths: ["src"] }
|
|
27
|
+
// ]
|
|
28
|
+
```
|
|
29
|
+
|
|
13
30
|
Process items in parallel batches using chunking:
|
|
14
31
|
|
|
15
32
|
```typescript
|
|
@@ -24,60 +41,58 @@ for (const batch of batches) {
|
|
|
24
41
|
}
|
|
25
42
|
```
|
|
26
43
|
|
|
27
|
-
##
|
|
44
|
+
## Array Utilities
|
|
28
45
|
|
|
29
|
-
###
|
|
46
|
+
### chunk
|
|
30
47
|
|
|
31
|
-
|
|
48
|
+
Splits an array into sub-arrays of a specified maximum size.
|
|
32
49
|
|
|
33
50
|
```typescript
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const items = [1, 2, 3, 4, 5, 6, 7];
|
|
37
|
-
const batches = chunk(items, 3);
|
|
38
|
-
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
39
|
-
|
|
40
|
-
for (const batch of batches) {
|
|
41
|
-
await Promise.allSettled(batch.map(processItem));
|
|
42
|
-
}
|
|
51
|
+
chunk<T>(arr: readonly T[], size: number): T[][]
|
|
43
52
|
```
|
|
44
53
|
|
|
45
|
-
|
|
54
|
+
| Parameter | Type | Description |
|
|
55
|
+
|-----------|-------------------|-------------------------------------|
|
|
56
|
+
| `arr` | `readonly T[]` | The array to split |
|
|
57
|
+
| `size` | `number` | Maximum size of each sub-array |
|
|
46
58
|
|
|
59
|
+
The final chunk may be smaller than the specified size. Useful for limiting concurrency in batched operations.
|
|
60
|
+
|
|
61
|
+
**Examples:**
|
|
47
62
|
- **Full-sized chunks only**: `chunk([1,2,3,4,5,6], 2)` → `[[1,2], [3,4], [5,6]]`
|
|
48
63
|
- **Final smaller chunk**: `chunk([1,2,3,4,5], 3)` → `[[1,2,3], [4,5]]`
|
|
49
64
|
- **Single oversized chunk**: `chunk([1,2,3], 5)` → `[[1,2,3]]`
|
|
50
65
|
- **Empty input**: `chunk([], 3)` → `[]`
|
|
51
66
|
|
|
52
|
-
|
|
67
|
+
```typescript
|
|
68
|
+
import { chunk } from "@hardlydifficult/collections";
|
|
53
69
|
|
|
54
|
-
|
|
70
|
+
const result = chunk([1, 2, 3, 4, 5, 6, 7], 3);
|
|
71
|
+
// => [[1, 2, 3], [4, 5, 6], [7]]
|
|
72
|
+
```
|
|
55
73
|
|
|
56
|
-
|
|
74
|
+
## Path Utilities
|
|
57
75
|
|
|
58
|
-
|
|
59
|
-
import { groupByDepth } from "@hardlydifficult/collections";
|
|
76
|
+
### groupByDepth
|
|
60
77
|
|
|
61
|
-
|
|
62
|
-
const grouped = groupByDepth(dirs);
|
|
63
|
-
// [
|
|
64
|
-
// { depth: 3, paths: ["src/services/summarize"] },
|
|
65
|
-
// { depth: 2, paths: ["src/services", "src/utils"] },
|
|
66
|
-
// { depth: 1, paths: ["src"] },
|
|
67
|
-
// ]
|
|
78
|
+
Groups path strings by their slash-separated depth, sorted deepest-first for bottom-up directory processing.
|
|
68
79
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
80
|
+
```typescript
|
|
81
|
+
groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]
|
|
72
82
|
```
|
|
73
83
|
|
|
74
|
-
|
|
84
|
+
| Parameter | Type | Description |
|
|
85
|
+
|-----------|-------------------|------------------------------------|
|
|
86
|
+
| `paths` | `readonly string[]` | Filesystem paths to group |
|
|
75
87
|
|
|
88
|
+
The depth is determined by counting slash-separated segments (e.g., `"a/b/c"` has depth `3`). Empty string `""` is treated as depth `0`.
|
|
89
|
+
|
|
90
|
+
**Rules:**
|
|
76
91
|
- An empty string (`""`) is treated as depth `0` (root)
|
|
77
92
|
- Each `/` increases depth by 1
|
|
78
93
|
- Paths are grouped by depth, and groups are sorted deepest-first
|
|
79
94
|
|
|
80
|
-
|
|
95
|
+
**Examples:**
|
|
81
96
|
|
|
82
97
|
- **Mixed depths**:
|
|
83
98
|
```typescript
|
|
@@ -96,4 +111,20 @@ for (const { paths: dirsAtDepth } of grouped) {
|
|
|
96
111
|
// [{ depth: 2, paths: ["x/y", "a/b", "m/n"] }]
|
|
97
112
|
```
|
|
98
113
|
|
|
99
|
-
- **Empty input**: `groupByDepth([])` → `[]`
|
|
114
|
+
- **Empty input**: `groupByDepth([])` → `[]`
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { groupByDepth } from "@hardlydifficult/collections";
|
|
118
|
+
|
|
119
|
+
const dirs = ["src/services/summarize", "src/services", "src", "src/utils"];
|
|
120
|
+
const grouped = groupByDepth(dirs);
|
|
121
|
+
// [
|
|
122
|
+
// { depth: 3, paths: ["src/services/summarize"] },
|
|
123
|
+
// { depth: 2, paths: ["src/services", "src/utils"] },
|
|
124
|
+
// { depth: 1, paths: ["src"] },
|
|
125
|
+
// ]
|
|
126
|
+
|
|
127
|
+
for (const { paths: dirsAtDepth } of grouped) {
|
|
128
|
+
await Promise.allSettled(dirsAtDepth.map(summarizeDir));
|
|
129
|
+
}
|
|
130
|
+
```
|