@hardlydifficult/collections 1.0.5 → 1.0.6
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 +38 -84
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/collections
|
|
2
2
|
|
|
3
|
-
A TypeScript utility library providing array chunking and path depth grouping
|
|
3
|
+
A TypeScript utility library providing array chunking and path depth grouping functions.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,115 +13,69 @@ npm install @hardlydifficult/collections
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { chunk, groupByDepth } from "@hardlydifficult/collections";
|
|
15
15
|
|
|
16
|
-
// Split an array into
|
|
17
|
-
const
|
|
16
|
+
// Split an array into chunks
|
|
17
|
+
const items = [1, 2, 3, 4, 5];
|
|
18
|
+
console.log(chunk(items, 2));
|
|
18
19
|
// → [[1, 2], [3, 4], [5]]
|
|
19
20
|
|
|
20
|
-
// Group filesystem paths by directory depth
|
|
21
|
-
const paths = ["src/
|
|
22
|
-
|
|
23
|
-
// → [{ depth:
|
|
21
|
+
// Group filesystem paths by directory depth
|
|
22
|
+
const paths = ["src/app", "src", "src/utils/helper"];
|
|
23
|
+
console.log(groupByDepth(paths));
|
|
24
|
+
// → [{ depth: 3, paths: ["src/app"] }, { depth: 2, paths: ["src"] }, { depth: 1, paths: ["src/utils/helper"] }]
|
|
24
25
|
```
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
## Chunk Arrays
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
import { chunk } from "@hardlydifficult/collections";
|
|
30
|
-
|
|
31
|
-
const items = [1, 2, 3, 4, 5, 6, 7];
|
|
32
|
-
const batches = chunk(items, 3);
|
|
33
|
-
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
34
|
-
|
|
35
|
-
for (const batch of batches) {
|
|
36
|
-
await Promise.allSettled(batch.map(processItem));
|
|
37
|
-
}
|
|
38
|
-
```
|
|
29
|
+
Split an array into fixed-size subarrays.
|
|
39
30
|
|
|
40
|
-
|
|
31
|
+
### `chunk`
|
|
41
32
|
|
|
42
|
-
Splits an array into
|
|
33
|
+
Splits an array into chunks of a specified maximum size. The last chunk may be smaller than the requested size.
|
|
43
34
|
|
|
44
35
|
```typescript
|
|
45
36
|
import { chunk } from "@hardlydifficult/collections";
|
|
46
37
|
|
|
47
|
-
const
|
|
48
|
-
console.log(chunk(numbers, 3));
|
|
38
|
+
const result = chunk([1, 2, 3, 4, 5, 6, 7], 3);
|
|
49
39
|
// → [[1, 2, 3], [4, 5, 6], [7]]
|
|
50
40
|
```
|
|
51
41
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
|
55
|
-
|
|
42
|
+
**Signature**
|
|
43
|
+
|
|
44
|
+
| Parameter | Type | Description |
|
|
45
|
+
|-----------|-------------------|--------------------------|
|
|
46
|
+
| `arr` | `readonly T[]` | Input array to chunk |
|
|
47
|
+
| `size` | `number` | Maximum chunk size |
|
|
48
|
+
| **Returns** | `T[][]` | Array of subarrays |
|
|
56
49
|
|
|
57
|
-
|
|
50
|
+
## Group Paths by Depth
|
|
58
51
|
|
|
59
|
-
|
|
60
|
-
- **Full-sized chunks only**: `chunk([1,2,3,4,5,6], 2)` → `[[1,2], [3,4], [5,6]]`
|
|
61
|
-
- **Final smaller chunk**: `chunk([1,2,3,4,5], 3)` → `[[1,2,3], [4,5]]`
|
|
62
|
-
- **Single oversized chunk**: `chunk([1,2,3], 5)` → `[[1,2,3]]`
|
|
63
|
-
- **Empty input**: `chunk([], 3)` → `[]`
|
|
52
|
+
Organize filesystem paths by slash-delimited depth, sorted deepest-first for bottom-up directory processing.
|
|
64
53
|
|
|
65
|
-
|
|
54
|
+
### `groupByDepth`
|
|
66
55
|
|
|
67
|
-
Groups
|
|
56
|
+
Groups an array of path strings by the number of `/`-separated segments. Paths with deeper nesting appear first in the result, enabling bottom-up processing.
|
|
68
57
|
|
|
69
58
|
```typescript
|
|
70
59
|
import { groupByDepth } from "@hardlydifficult/collections";
|
|
71
60
|
|
|
72
|
-
const paths = ["src/
|
|
61
|
+
const paths = ["src/app/index.ts", "src/app", "src", "README.md"];
|
|
73
62
|
const result = groupByDepth(paths);
|
|
74
63
|
// → [
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
64
|
+
// { depth: 3, paths: ["src/app/index.ts"] },
|
|
65
|
+
// { depth: 2, paths: ["src/app"] },
|
|
66
|
+
// { depth: 1, paths: ["src", "README.md"] }
|
|
67
|
+
// ]
|
|
79
68
|
```
|
|
80
69
|
|
|
81
|
-
|
|
82
|
-
|---------|------|-------------|
|
|
83
|
-
| `paths` | `readonly string[]` | Array of path strings (slashes `/` delimit depth) |
|
|
84
|
-
|
|
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.
|
|
70
|
+
**Signature**
|
|
86
71
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
72
|
+
| Parameter | Type | Description |
|
|
73
|
+
|-----------|--------------------|------------------------------------|
|
|
74
|
+
| `paths` | `readonly string[]`| Array of path strings |
|
|
75
|
+
| **Returns** | `{ depth: number; paths: string[] }[]` | Groups sorted deepest-first |
|
|
91
76
|
|
|
92
|
-
**
|
|
93
|
-
|
|
94
|
-
- **Mixed depths**:
|
|
95
|
-
```typescript
|
|
96
|
-
groupByDepth(["a/b/c", "a/b", "a", ""]);
|
|
97
|
-
// [
|
|
98
|
-
// { depth: 3, paths: ["a/b/c"] },
|
|
99
|
-
// { depth: 2, paths: ["a/b"] },
|
|
100
|
-
// { depth: 1, paths: ["a"] },
|
|
101
|
-
// { depth: 0, paths: [""] },
|
|
102
|
-
// ]
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
- **Same depth grouping**:
|
|
106
|
-
```typescript
|
|
107
|
-
groupByDepth(["x/y", "a/b", "m/n"]);
|
|
108
|
-
// [{ depth: 2, paths: ["x/y", "a/b", "m/n"] }]
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
- **Empty input**: `groupByDepth([])` → `[]`
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
import { groupByDepth } from "@hardlydifficult/collections";
|
|
115
|
-
|
|
116
|
-
const dirs = ["src/services/summarize", "src/services", "src", "src/utils"];
|
|
117
|
-
const grouped = groupByDepth(dirs);
|
|
118
|
-
// [
|
|
119
|
-
// { depth: 3, paths: ["src/services/summarize"] },
|
|
120
|
-
// { depth: 2, paths: ["src/services", "src/utils"] },
|
|
121
|
-
// { depth: 1, paths: ["src"] },
|
|
122
|
-
// ]
|
|
77
|
+
**Notes**
|
|
123
78
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
```
|
|
79
|
+
- Empty string paths (`""`) are assigned depth `0`.
|
|
80
|
+
- Paths are grouped in descending order of depth (deepest first).
|
|
81
|
+
- Order of paths within each depth group matches their appearance in the input array.
|