@hardlydifficult/collections 1.0.4 → 1.0.5
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 +33 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/collections
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript utility library providing array chunking and path depth grouping utilities for batched parallel processing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,17 +14,13 @@ npm install @hardlydifficult/collections
|
|
|
14
14
|
import { chunk, groupByDepth } from "@hardlydifficult/collections";
|
|
15
15
|
|
|
16
16
|
// Split an array into fixed-size chunks
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
// => [[1, 2], [3, 4], [5]]
|
|
17
|
+
const result = chunk([1, 2, 3, 4, 5], 2);
|
|
18
|
+
// → [[1, 2], [3, 4], [5]]
|
|
20
19
|
|
|
21
|
-
// Group filesystem paths by directory depth (deepest
|
|
22
|
-
const paths = ["src/
|
|
20
|
+
// Group filesystem paths by directory depth (deepest-first)
|
|
21
|
+
const paths = ["src/a/b", "src", "src/c"];
|
|
23
22
|
const grouped = groupByDepth(paths);
|
|
24
|
-
//
|
|
25
|
-
// { depth: 3, paths: ["src/app"] },
|
|
26
|
-
// { depth: 1, paths: ["src"] }
|
|
27
|
-
// ]
|
|
23
|
+
// → [{ depth: 2, paths: ["src/a/b"] }, { depth: 1, paths: ["src", "src/c"] }]
|
|
28
24
|
```
|
|
29
25
|
|
|
30
26
|
Process items in parallel batches using chunking:
|
|
@@ -41,22 +37,24 @@ for (const batch of batches) {
|
|
|
41
37
|
}
|
|
42
38
|
```
|
|
43
39
|
|
|
44
|
-
## Array
|
|
45
|
-
|
|
46
|
-
### chunk
|
|
40
|
+
## Array Chunking (`chunk`)
|
|
47
41
|
|
|
48
|
-
Splits an array into sub-arrays of a specified maximum size.
|
|
42
|
+
Splits an array into sub-arrays of a specified maximum size, preserving order.
|
|
49
43
|
|
|
50
44
|
```typescript
|
|
51
|
-
|
|
45
|
+
import { chunk } from "@hardlydifficult/collections";
|
|
46
|
+
|
|
47
|
+
const numbers = [1, 2, 3, 4, 5, 6, 7];
|
|
48
|
+
console.log(chunk(numbers, 3));
|
|
49
|
+
// → [[1, 2, 3], [4, 5, 6], [7]]
|
|
52
50
|
```
|
|
53
51
|
|
|
54
|
-
| Parameter | Type
|
|
55
|
-
|
|
56
|
-
| `arr`
|
|
57
|
-
| `size`
|
|
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) |
|
|
58
56
|
|
|
59
|
-
|
|
57
|
+
Returns `T[][]`: An array of chunks, where the last chunk may be smaller if the input length is not evenly divisible by `size`.
|
|
60
58
|
|
|
61
59
|
**Examples:**
|
|
62
60
|
- **Full-sized chunks only**: `chunk([1,2,3,4,5,6], 2)` → `[[1,2], [3,4], [5,6]]`
|
|
@@ -64,28 +62,27 @@ The final chunk may be smaller than the specified size. Useful for limiting conc
|
|
|
64
62
|
- **Single oversized chunk**: `chunk([1,2,3], 5)` → `[[1,2,3]]`
|
|
65
63
|
- **Empty input**: `chunk([], 3)` → `[]`
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
import { chunk } from "@hardlydifficult/collections";
|
|
65
|
+
## Path Depth Grouping (`groupByDepth`)
|
|
69
66
|
|
|
70
|
-
|
|
71
|
-
// => [[1, 2, 3], [4, 5, 6], [7]]
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Path Utilities
|
|
75
|
-
|
|
76
|
-
### groupByDepth
|
|
77
|
-
|
|
78
|
-
Groups path strings by their slash-separated depth, sorted deepest-first for bottom-up directory processing.
|
|
67
|
+
Groups filesystem paths by slash-delimited depth, sorted deepest-first to support bottom-up directory processing.
|
|
79
68
|
|
|
80
69
|
```typescript
|
|
81
|
-
|
|
70
|
+
import { groupByDepth } from "@hardlydifficult/collections";
|
|
71
|
+
|
|
72
|
+
const paths = ["src/a/b", "src", "src/c", ""];
|
|
73
|
+
const result = groupByDepth(paths);
|
|
74
|
+
// → [
|
|
75
|
+
// { depth: 3, paths: ["src/a/b"] },
|
|
76
|
+
// { depth: 2, paths: ["src", "src/c"] },
|
|
77
|
+
// { depth: 0, paths: [""] }
|
|
78
|
+
// ]
|
|
82
79
|
```
|
|
83
80
|
|
|
84
|
-
| Parameter | Type
|
|
85
|
-
|
|
86
|
-
| `paths`
|
|
81
|
+
| Parameter | Type | Description |
|
|
82
|
+
|---------|------|-------------|
|
|
83
|
+
| `paths` | `readonly string[]` | Array of path strings (slashes `/` delimit depth) |
|
|
87
84
|
|
|
88
|
-
|
|
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.
|
|
89
86
|
|
|
90
87
|
**Rules:**
|
|
91
88
|
- An empty string (`""`) is treated as depth `0` (root)
|