@hardlydifficult/collections 1.0.5 → 1.0.7
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 +52 -86
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/collections
|
|
2
2
|
|
|
3
|
-
A TypeScript utility library
|
|
3
|
+
A TypeScript utility library for array chunking and path depth grouping.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,115 +13,81 @@ npm install @hardlydifficult/collections
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { chunk, groupByDepth } from "@hardlydifficult/collections";
|
|
15
15
|
|
|
16
|
-
// Split an array into fixed
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
16
|
+
// Split an array into chunks of fixed size
|
|
17
|
+
const numbers = [1, 2, 3, 4, 5, 6, 7];
|
|
18
|
+
console.log(chunk(numbers, 3));
|
|
19
|
+
// Output: [[1, 2, 3], [4, 5, 6], [7]]
|
|
20
|
+
|
|
21
|
+
// Group file paths by directory depth (deepest-first)
|
|
22
|
+
const paths = ["src/components", "src", "src/components/Button"];
|
|
23
|
+
console.log(groupByDepth(paths));
|
|
24
|
+
// Output: [
|
|
25
|
+
// { depth: 2, paths: ["src/components", "src/components/Button"] },
|
|
26
|
+
// { depth: 1, paths: ["src"] }
|
|
27
|
+
// ]
|
|
24
28
|
```
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
## Array Chunking
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
import { chunk } from "@hardlydifficult/collections";
|
|
32
|
+
Splits arrays into subarrays of a specified maximum size.
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
const batches = chunk(items, 3);
|
|
33
|
-
// [[1, 2, 3], [4, 5, 6], [7]]
|
|
34
|
+
### `chunk`
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
await Promise.allSettled(batch.map(processItem));
|
|
37
|
-
}
|
|
38
|
-
```
|
|
36
|
+
Splits a readonly array into chunks of the given size.
|
|
39
37
|
|
|
40
|
-
|
|
38
|
+
**Signature:**
|
|
39
|
+
```typescript
|
|
40
|
+
function chunk<T>(arr: readonly T[], size: number): T[][]
|
|
41
|
+
```
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
| Parameter | Type | Description |
|
|
44
|
+
|-----------|-------------------|-------------------------------|
|
|
45
|
+
| `arr` | `readonly T[]` | The array to split |
|
|
46
|
+
| `size` | `number` | Maximum size of each chunk |
|
|
43
47
|
|
|
48
|
+
**Example:**
|
|
44
49
|
```typescript
|
|
45
50
|
import { chunk } from "@hardlydifficult/collections";
|
|
46
51
|
|
|
47
|
-
const
|
|
48
|
-
console.log(chunk(
|
|
49
|
-
//
|
|
50
|
-
```
|
|
52
|
+
const items = ["a", "b", "c", "d", "e"];
|
|
53
|
+
console.log(chunk(items, 2));
|
|
54
|
+
// Output: [["a", "b"], ["c", "d"], ["e"]]
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
| `size` | `number` | Maximum chunk size (must be ≥1) |
|
|
56
|
+
console.log(chunk([1, 2, 3], 5));
|
|
57
|
+
// Output: [[1, 2, 3]]
|
|
58
|
+
```
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
## Path Depth Grouping
|
|
58
61
|
|
|
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)` → `[]`
|
|
62
|
+
Groups file paths by their slash-separated depth, sorted deepest-first.
|
|
64
63
|
|
|
65
|
-
|
|
64
|
+
### `groupByDepth`
|
|
66
65
|
|
|
67
|
-
Groups
|
|
66
|
+
Groups path strings by the number of `/`-separated segments, returning results in descending depth order.
|
|
68
67
|
|
|
68
|
+
**Signature:**
|
|
69
69
|
```typescript
|
|
70
|
-
|
|
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
|
-
// ]
|
|
70
|
+
function groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]
|
|
79
71
|
```
|
|
80
72
|
|
|
81
|
-
| Parameter | Type
|
|
82
|
-
|
|
83
|
-
| `paths`
|
|
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.
|
|
73
|
+
| Parameter | Type | Description |
|
|
74
|
+
|-----------|--------------------|--------------------------------------|
|
|
75
|
+
| `paths` | `readonly string[]`| Array of path strings to group |
|
|
86
76
|
|
|
87
|
-
|
|
88
|
-
-
|
|
89
|
-
-
|
|
90
|
-
- Paths are grouped by depth, and groups are sorted deepest-first
|
|
91
|
-
|
|
92
|
-
**Examples:**
|
|
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([])` → `[]`
|
|
77
|
+
Returns an array of objects with:
|
|
78
|
+
- `depth`: number of slash-separated segments (empty string has depth 0)
|
|
79
|
+
- `paths`: array of paths at that depth, in original order
|
|
112
80
|
|
|
81
|
+
**Example:**
|
|
113
82
|
```typescript
|
|
114
83
|
import { groupByDepth } from "@hardlydifficult/collections";
|
|
115
84
|
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
// [
|
|
119
|
-
// { depth: 3, paths: ["
|
|
120
|
-
// { depth: 2, paths: ["
|
|
121
|
-
// { depth: 1, paths: ["
|
|
85
|
+
const paths = ["a/b/c", "a/b", "a", "", "x/y"];
|
|
86
|
+
console.log(groupByDepth(paths));
|
|
87
|
+
// Output: [
|
|
88
|
+
// { depth: 3, paths: ["a/b/c"] },
|
|
89
|
+
// { depth: 2, paths: ["a/b", "x/y"] },
|
|
90
|
+
// { depth: 1, paths: ["a"] },
|
|
91
|
+
// { depth: 0, paths: [""] }
|
|
122
92
|
// ]
|
|
123
|
-
|
|
124
|
-
for (const { paths: dirsAtDepth } of grouped) {
|
|
125
|
-
await Promise.allSettled(dirsAtDepth.map(summarizeDir));
|
|
126
|
-
}
|
|
127
93
|
```
|