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