@elyukai/utils 0.1.7 → 0.2.1
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 +18 -0
- package/dist/dictionary.d.ts +6 -8
- package/dist/dictionary.js +15 -16
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
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.1](https://github.com/elyukai/ts-utils/compare/v0.2.0...v0.2.1) (2026-02-07)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* specific key type ([a8a7975](https://github.com/elyukai/ts-utils/commit/a8a79750d4323e44b17b9ba01152150de12b7792))
|
|
11
|
+
|
|
12
|
+
## [0.2.0](https://github.com/elyukai/ts-utils/compare/v0.1.7...v0.2.0) (2026-02-05)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### ⚠ BREAKING CHANGES
|
|
16
|
+
|
|
17
|
+
* merge forEach and forEachAsync
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* merge forEach and forEachAsync ([15b419f](https://github.com/elyukai/ts-utils/commit/15b419fa962218a74133ff7ee85ca817c1fc989c))
|
|
22
|
+
|
|
5
23
|
## [0.1.7](https://github.com/elyukai/ts-utils/compare/v0.1.6...v0.1.7) (2026-02-05)
|
|
6
24
|
|
|
7
25
|
|
package/dist/dictionary.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { AnyNonNullish } from "./nullable.js";
|
|
|
7
7
|
* An immutable dictionary mapping strings to values.
|
|
8
8
|
*/
|
|
9
9
|
export declare class Dictionary<T extends AnyNonNullish | null, K extends string = string> {
|
|
10
|
+
#private;
|
|
10
11
|
private record;
|
|
11
12
|
/**
|
|
12
13
|
* Creates a new dictionary from an indexed object.
|
|
@@ -62,14 +63,11 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
|
|
|
62
63
|
keys(): K[];
|
|
63
64
|
/**
|
|
64
65
|
* Calls the given function for each key-value pair in the dictionary.
|
|
65
|
-
*/
|
|
66
|
-
forEach(fn: (value: T, key: K) => void): void;
|
|
67
|
-
/**
|
|
68
|
-
* Calls the given async function for each key-value pair in the dictionary.
|
|
69
66
|
*
|
|
70
|
-
*
|
|
67
|
+
* If the `async` flag is used, the function calls the function with the values sequentially.
|
|
71
68
|
*/
|
|
72
|
-
|
|
69
|
+
forEach(fn: (value: T, key: K) => Promise<void>, async: true): Promise<void>;
|
|
70
|
+
forEach(fn: (value: T, key: K) => void, async?: false): void;
|
|
73
71
|
/**
|
|
74
72
|
* Create, modify, or remove the value for the given key based on its current value.
|
|
75
73
|
*
|
|
@@ -91,7 +89,7 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
|
|
|
91
89
|
/**
|
|
92
90
|
* Returns the first key that matches the given predicate, or `undefined` if no such key exists.
|
|
93
91
|
*/
|
|
94
|
-
findKey(predicate: (value: T, key: K) => boolean):
|
|
92
|
+
findKey(predicate: (value: T, key: K) => boolean): K | undefined;
|
|
95
93
|
/**
|
|
96
94
|
* Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
|
|
97
95
|
*/
|
|
@@ -111,7 +109,7 @@ export declare class Dictionary<T extends AnyNonNullish | null, K extends string
|
|
|
111
109
|
/**
|
|
112
110
|
* 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.
|
|
113
111
|
*/
|
|
114
|
-
reduce<U>(reducer: (acc: U, value: T, key:
|
|
112
|
+
reduce<U>(reducer: (acc: U, value: T, key: K) => U, initialValue: U): U;
|
|
115
113
|
/**
|
|
116
114
|
* Converts the dictionary to a plain object.
|
|
117
115
|
*
|
package/dist/dictionary.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Introduces a Dictionary class that represents an immutable mapping from strings to values.
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
|
+
import { Lazy } from "./lazy.js";
|
|
5
6
|
import { omitKeys, omitUndefinedKeys } from "./object.js";
|
|
6
7
|
/**
|
|
7
8
|
* An immutable dictionary mapping strings to values.
|
|
@@ -64,11 +65,12 @@ export class Dictionary {
|
|
|
64
65
|
return this;
|
|
65
66
|
}
|
|
66
67
|
}
|
|
68
|
+
#size = Lazy.of(() => this.keys().length);
|
|
67
69
|
/**
|
|
68
70
|
* The number of entries in the dictionary.
|
|
69
71
|
*/
|
|
70
72
|
get size() {
|
|
71
|
-
return this.
|
|
73
|
+
return this.#size.value;
|
|
72
74
|
}
|
|
73
75
|
/**
|
|
74
76
|
* Returns an array of key-value pairs in the dictionary.
|
|
@@ -88,22 +90,19 @@ export class Dictionary {
|
|
|
88
90
|
keys() {
|
|
89
91
|
return Object.keys(this.record);
|
|
90
92
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
forEach(...args) {
|
|
94
|
+
const [fn, async] = args;
|
|
95
|
+
if (async === true) {
|
|
96
|
+
return (async () => {
|
|
97
|
+
for (const [key, value] of this.entries()) {
|
|
98
|
+
await fn(value, key);
|
|
99
|
+
}
|
|
100
|
+
})();
|
|
97
101
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* The calls are made sequentially.
|
|
103
|
-
*/
|
|
104
|
-
async forEachAsync(fn) {
|
|
105
|
-
for (const [key, value] of this.entries()) {
|
|
106
|
-
await fn(value, key);
|
|
102
|
+
else {
|
|
103
|
+
for (const [key, value] of this.entries()) {
|
|
104
|
+
fn(value, key);
|
|
105
|
+
}
|
|
107
106
|
}
|
|
108
107
|
}
|
|
109
108
|
/**
|