@hardlydifficult/collections 1.0.1 → 1.0.3

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 +79 -28
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @hardlydifficult/collections
2
2
 
3
- Array and collection utilities for batched parallel processing.
3
+ Array chunking and path depth grouping utilities for batched parallel processing.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,7 +10,24 @@ npm install @hardlydifficult/collections
10
10
 
11
11
  ## Quick Start
12
12
 
13
- Split an array into chunks for parallel batch processing:
13
+ ```typescript
14
+ import { chunk, groupByDepth } from "@hardlydifficult/collections";
15
+
16
+ // Split an array into fixed-size chunks
17
+ const items = [1, 2, 3, 4, 5];
18
+ const chunks = chunk(items, 2);
19
+ // => [[1, 2], [3, 4], [5]]
20
+
21
+ // Group filesystem paths by directory depth (deepest first)
22
+ const paths = ["src/app", "src", "src/utils/helper"];
23
+ const grouped = groupByDepth(paths);
24
+ // => [
25
+ // { depth: 3, paths: ["src/app"] },
26
+ // { depth: 1, paths: ["src"] }
27
+ // ]
28
+ ```
29
+
30
+ Process items in parallel batches using chunking:
14
31
 
15
32
  ```typescript
16
33
  import { chunk } from "@hardlydifficult/collections";
@@ -24,30 +41,77 @@ for (const batch of batches) {
24
41
  }
25
42
  ```
26
43
 
27
- ## Chunking Arrays
44
+ ## Array Utilities
28
45
 
29
- ### `chunk<T>(arr: readonly T[], size: number): T[][]`
46
+ ### chunk
30
47
 
31
- Split an array into chunks of a given size. The final chunk may be smaller than the specified size. Useful for processing items in parallel batches with a concurrency limit.
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 |
58
+
59
+ The final chunk may be smaller than the specified size. Useful for limiting concurrency in batched operations.
60
+
61
+ **Examples:**
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)` → `[]`
32
66
 
33
67
  ```typescript
34
68
  import { chunk } from "@hardlydifficult/collections";
35
69
 
36
- const items = [1, 2, 3, 4, 5, 6, 7];
37
- const batches = chunk(items, 3);
38
- // [[1, 2, 3], [4, 5, 6], [7]]
70
+ const result = chunk([1, 2, 3, 4, 5, 6, 7], 3);
71
+ // => [[1, 2, 3], [4, 5, 6], [7]]
72
+ ```
39
73
 
40
- // Process in parallel batches
41
- for (const batch of batches) {
42
- await Promise.allSettled(batch.map(processItem));
43
- }
74
+ ## Path Utilities
75
+
76
+ ### groupByDepth
77
+
78
+ Groups path strings by their slash-separated depth, sorted deepest-first for bottom-up directory processing.
79
+
80
+ ```typescript
81
+ groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]
44
82
  ```
45
83
 
46
- ## Grouping by Depth
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:**
47
96
 
48
- ### `groupByDepth(paths: readonly string[]): { depth: number; paths: string[] }[]`
97
+ - **Mixed depths**:
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
+ ```
49
107
 
50
- Group `/`-separated path strings by their depth, sorted deepest-first. Useful for bottom-up directory processing where children must be handled before parents.
108
+ - **Same depth grouping**:
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([])` → `[]`
51
115
 
52
116
  ```typescript
53
117
  import { groupByDepth } from "@hardlydifficult/collections";
@@ -60,20 +124,7 @@ const grouped = groupByDepth(dirs);
60
124
  // { depth: 1, paths: ["src"] },
61
125
  // ]
62
126
 
63
- // Process directories bottom-up, parallelizing within each depth level
64
127
  for (const { paths: dirsAtDepth } of grouped) {
65
128
  await Promise.allSettled(dirsAtDepth.map(summarizeDir));
66
129
  }
67
- ```
68
-
69
- Depth is calculated by counting `/`-separated segments. An empty string is treated as depth 0 (root).
70
-
71
- ```typescript
72
- groupByDepth(["a/b/c", "a/b", "a", ""]);
73
- // [
74
- // { depth: 3, paths: ["a/b/c"] },
75
- // { depth: 2, paths: ["a/b"] },
76
- // { depth: 1, paths: ["a"] },
77
- // { depth: 0, paths: [""] },
78
- // ]
79
130
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/collections",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [