@elyukai/utils 0.2.0 → 0.2.2

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/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [0.2.2](https://github.com/elyukai/ts-utils/compare/v0.2.1...v0.2.2) (2026-02-11)
6
+
7
+
8
+ ### Features
9
+
10
+ * add FixedArray helper ([4a1b61a](https://github.com/elyukai/ts-utils/commit/4a1b61a51ad53c35767adf716d4a64ef10f4dc10))
11
+ * add static groupBy method to dictionary ([2ee4802](https://github.com/elyukai/ts-utils/commit/2ee4802db8f1b821819f4ced5f79079153a6c11e))
12
+
13
+ ## [0.2.1](https://github.com/elyukai/ts-utils/compare/v0.2.0...v0.2.1) (2026-02-07)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * specific key type ([a8a7975](https://github.com/elyukai/ts-utils/commit/a8a79750d4323e44b17b9ba01152150de12b7792))
19
+
5
20
  ## [0.2.0](https://github.com/elyukai/ts-utils/compare/v0.1.7...v0.2.0) (2026-02-05)
6
21
 
7
22
 
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A helper type that builds a tuple type of a desired length.
3
+ * @module
4
+ */
5
+ /**
6
+ * A recursive type that constructs an array of type `T` with a length of `N`.
7
+ */
8
+ type FixedArrayAux<T, N extends number, A extends T[]> = A["length"] extends N ? A : FixedArrayAux<T, N, [...A, T]>;
9
+ /**
10
+ * A type representing an array of a fixed size `N` containing elements of type `T`.
11
+ */
12
+ export type FixedArray<T, N extends number> = FixedArrayAux<T, N, []>;
13
+ export {};
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A helper type that builds a tuple type of a desired length.
3
+ * @module
4
+ */
5
+ export {};
@@ -21,6 +21,10 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
21
21
  * Constructs a dictionary from an array of key-value pairs.
22
22
  */
23
23
  static fromEntries<T extends AnyNonNullish | null, K extends string = string>(entries: [K, T][]): Dictionary<T, K>;
24
+ /**
25
+ * Groups the given items by the key returned by the given function and returns a dictionary mapping each key to an array of items with that key.
26
+ */
27
+ static groupBy<T, K extends string>(items: T[], keyFn: (item: T) => K): Dictionary<T[], K>;
24
28
  /**
25
29
  * Returns the value associated with the given key, or `undefined` if the key does not exist.
26
30
  */
@@ -89,7 +93,7 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
89
93
  /**
90
94
  * Returns the first key that matches the given predicate, or `undefined` if no such key exists.
91
95
  */
92
- findKey(predicate: (value: T, key: K) => boolean): string | undefined;
96
+ findKey(predicate: (value: T, key: K) => boolean): K | undefined;
93
97
  /**
94
98
  * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
95
99
  */
@@ -109,7 +113,7 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
109
113
  /**
110
114
  * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
111
115
  */
112
- reduce<U>(reducer: (acc: U, value: T, key: string) => U, initialValue: U): U;
116
+ reduce<U>(reducer: (acc: U, value: T, key: K) => U, initialValue: U): U;
113
117
  /**
114
118
  * Converts the dictionary to a plain object.
115
119
  *
@@ -25,6 +25,22 @@ export class Dictionary {
25
25
  static fromEntries(entries) {
26
26
  return new Dictionary(Object.fromEntries(entries));
27
27
  }
28
+ /**
29
+ * Groups the given items by the key returned by the given function and returns a dictionary mapping each key to an array of items with that key.
30
+ */
31
+ static groupBy(items, keyFn) {
32
+ const record = {};
33
+ for (const item of items) {
34
+ const key = keyFn(item);
35
+ if (record[key] === undefined) {
36
+ record[key] = [item];
37
+ }
38
+ else {
39
+ record[key].push(item);
40
+ }
41
+ }
42
+ return new Dictionary(record);
43
+ }
28
44
  /**
29
45
  * Returns the value associated with the given key, or `undefined` if the key does not exist.
30
46
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elyukai/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A set of JavaScript helper functions.",
5
5
  "files": [
6
6
  "dist",