@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.
Files changed (2) hide show
  1. package/README.md +52 -86
  2. 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 utilities for batched parallel processing.
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-size chunks
17
- const result = chunk([1, 2, 3, 4, 5], 2);
18
- // → [[1, 2], [3, 4], [5]]
19
-
20
- // Group filesystem paths by directory depth (deepest-first)
21
- const paths = ["src/a/b", "src", "src/c"];
22
- const grouped = groupByDepth(paths);
23
- // → [{ depth: 2, paths: ["src/a/b"] }, { depth: 1, paths: ["src", "src/c"] }]
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
- Process items in parallel batches using chunking:
30
+ ## Array Chunking
27
31
 
28
- ```typescript
29
- import { chunk } from "@hardlydifficult/collections";
32
+ Splits arrays into subarrays of a specified maximum size.
30
33
 
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
+ ### `chunk`
34
35
 
35
- for (const batch of batches) {
36
- await Promise.allSettled(batch.map(processItem));
37
- }
38
- ```
36
+ Splits a readonly array into chunks of the given size.
39
37
 
40
- ## Array Chunking (`chunk`)
38
+ **Signature:**
39
+ ```typescript
40
+ function chunk<T>(arr: readonly T[], size: number): T[][]
41
+ ```
41
42
 
42
- Splits an array into sub-arrays of a specified maximum size, preserving order.
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 numbers = [1, 2, 3, 4, 5, 6, 7];
48
- console.log(chunk(numbers, 3));
49
- // [[1, 2, 3], [4, 5, 6], [7]]
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
- | Parameter | Type | Description |
53
- |---------|------|-------------|
54
- | `arr` | `readonly T[]` | Input array to split (supports readonly arrays) |
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
- Returns `T[][]`: An array of chunks, where the last chunk may be smaller if the input length is not evenly divisible by `size`.
60
+ ## Path Depth Grouping
58
61
 
59
- **Examples:**
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
- ## Path Depth Grouping (`groupByDepth`)
64
+ ### `groupByDepth`
66
65
 
67
- Groups filesystem paths by slash-delimited depth, sorted deepest-first to support bottom-up directory processing.
66
+ Groups path strings by the number of `/`-separated segments, returning results in descending depth order.
68
67
 
68
+ **Signature:**
69
69
  ```typescript
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
- // ]
70
+ function groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]
79
71
  ```
80
72
 
81
- | Parameter | Type | Description |
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.
73
+ | Parameter | Type | Description |
74
+ |-----------|--------------------|--------------------------------------|
75
+ | `paths` | `readonly string[]`| Array of path strings to group |
86
76
 
87
- **Rules:**
88
- - An empty string (`""`) is treated as depth `0` (root)
89
- - Each `/` increases depth by 1
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 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"] },
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
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/collections",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [