@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.
Files changed (2) hide show
  1. package/README.md +54 -42
  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 functions.
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 items = [1, 2, 3, 4, 5];
18
- console.log(chunk(items, 2));
19
- // [[1, 2], [3, 4], [5]]
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 filesystem paths by directory depth
22
- const paths = ["src/app", "src", "src/utils/helper"];
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
- // → [{ depth: 3, paths: ["src/app"] }, { depth: 2, paths: ["src"] }, { depth: 1, paths: ["src/utils/helper"] }]
24
+ // Output: [
25
+ // { depth: 2, paths: ["src/components", "src/components/Button"] },
26
+ // { depth: 1, paths: ["src"] }
27
+ // ]
25
28
  ```
26
29
 
27
- ## Chunk Arrays
30
+ ## Array Chunking
28
31
 
29
- Split an array into fixed-size subarrays.
32
+ Splits arrays into subarrays of a specified maximum size.
30
33
 
31
34
  ### `chunk`
32
35
 
33
- Splits an array into chunks of a specified maximum size. The last chunk may be smaller than the requested size.
36
+ Splits a readonly array into chunks of the given size.
34
37
 
38
+ **Signature:**
35
39
  ```typescript
36
- import { chunk } from "@hardlydifficult/collections";
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
- **Signature**
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
- | Parameter | Type | Description |
45
- |-----------|-------------------|--------------------------|
46
- | `arr` | `readonly T[]` | Input array to chunk |
47
- | `size` | `number` | Maximum chunk size |
48
- | **Returns** | `T[][]` | Array of subarrays |
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
- ## Group Paths by Depth
60
+ ## Path Depth Grouping
51
61
 
52
- Organize filesystem paths by slash-delimited depth, sorted deepest-first for bottom-up directory processing.
62
+ Groups file paths by their slash-separated depth, sorted deepest-first.
53
63
 
54
64
  ### `groupByDepth`
55
65
 
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.
66
+ Groups path strings by the number of `/`-separated segments, returning results in descending depth order.
57
67
 
68
+ **Signature:**
58
69
  ```typescript
59
- import { groupByDepth } from "@hardlydifficult/collections";
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
- **Signature**
73
+ | Parameter | Type | Description |
74
+ |-----------|--------------------|--------------------------------------|
75
+ | `paths` | `readonly string[]`| Array of path strings to group |
71
76
 
72
- | Parameter | Type | Description |
73
- |-----------|--------------------|------------------------------------|
74
- | `paths` | `readonly string[]`| Array of path strings |
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
- **Notes**
81
+ **Example:**
82
+ ```typescript
83
+ import { groupByDepth } from "@hardlydifficult/collections";
78
84
 
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.
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
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/collections",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [