@hardlydifficult/collections 1.0.6 → 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 +54 -42
- 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,69 +13,81 @@ npm install @hardlydifficult/collections
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { chunk, groupByDepth } from "@hardlydifficult/collections";
|
|
15
15
|
|
|
16
|
-
// Split an array into chunks
|
|
17
|
-
const
|
|
18
|
-
console.log(chunk(
|
|
19
|
-
//
|
|
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
20
|
|
|
21
|
-
// Group
|
|
22
|
-
const paths = ["src/
|
|
21
|
+
// Group file paths by directory depth (deepest-first)
|
|
22
|
+
const paths = ["src/components", "src", "src/components/Button"];
|
|
23
23
|
console.log(groupByDepth(paths));
|
|
24
|
-
//
|
|
24
|
+
// Output: [
|
|
25
|
+
// { depth: 2, paths: ["src/components", "src/components/Button"] },
|
|
26
|
+
// { depth: 1, paths: ["src"] }
|
|
27
|
+
// ]
|
|
25
28
|
```
|
|
26
29
|
|
|
27
|
-
##
|
|
30
|
+
## Array Chunking
|
|
28
31
|
|
|
29
|
-
|
|
32
|
+
Splits arrays into subarrays of a specified maximum size.
|
|
30
33
|
|
|
31
34
|
### `chunk`
|
|
32
35
|
|
|
33
|
-
Splits
|
|
36
|
+
Splits a readonly array into chunks of the given size.
|
|
34
37
|
|
|
38
|
+
**Signature:**
|
|
35
39
|
```typescript
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const result = chunk([1, 2, 3, 4, 5, 6, 7], 3);
|
|
39
|
-
// → [[1, 2, 3], [4, 5, 6], [7]]
|
|
40
|
+
function chunk<T>(arr: readonly T[], size: number): T[][]
|
|
40
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 |
|
|
47
|
+
|
|
48
|
+
**Example:**
|
|
49
|
+
```typescript
|
|
50
|
+
import { chunk } from "@hardlydifficult/collections";
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
const items = ["a", "b", "c", "d", "e"];
|
|
53
|
+
console.log(chunk(items, 2));
|
|
54
|
+
// Output: [["a", "b"], ["c", "d"], ["e"]]
|
|
55
|
+
|
|
56
|
+
console.log(chunk([1, 2, 3], 5));
|
|
57
|
+
// Output: [[1, 2, 3]]
|
|
58
|
+
```
|
|
49
59
|
|
|
50
|
-
##
|
|
60
|
+
## Path Depth Grouping
|
|
51
61
|
|
|
52
|
-
|
|
62
|
+
Groups file paths by their slash-separated depth, sorted deepest-first.
|
|
53
63
|
|
|
54
64
|
### `groupByDepth`
|
|
55
65
|
|
|
56
|
-
Groups
|
|
66
|
+
Groups path strings by the number of `/`-separated segments, returning results in descending depth order.
|
|
57
67
|
|
|
68
|
+
**Signature:**
|
|
58
69
|
```typescript
|
|
59
|
-
|
|
60
|
-
|
|
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"] }
|
|
67
|
-
// ]
|
|
70
|
+
function groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]
|
|
68
71
|
```
|
|
69
72
|
|
|
70
|
-
|
|
73
|
+
| Parameter | Type | Description |
|
|
74
|
+
|-----------|--------------------|--------------------------------------|
|
|
75
|
+
| `paths` | `readonly string[]`| Array of path strings to group |
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
| **Returns** | `{ depth: number; paths: string[] }[]` | Groups sorted deepest-first |
|
|
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
|
|
76
80
|
|
|
77
|
-
**
|
|
81
|
+
**Example:**
|
|
82
|
+
```typescript
|
|
83
|
+
import { groupByDepth } from "@hardlydifficult/collections";
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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: [""] }
|
|
92
|
+
// ]
|
|
93
|
+
```
|