@elyukai/utils 0.1.7 → 0.2.0

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,17 @@
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.0](https://github.com/elyukai/ts-utils/compare/v0.1.7...v0.2.0) (2026-02-05)
6
+
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ * merge forEach and forEachAsync
11
+
12
+ ### Features
13
+
14
+ * merge forEach and forEachAsync ([15b419f](https://github.com/elyukai/ts-utils/commit/15b419fa962218a74133ff7ee85ca817c1fc989c))
15
+
5
16
  ## [0.1.7](https://github.com/elyukai/ts-utils/compare/v0.1.6...v0.1.7) (2026-02-05)
6
17
 
7
18
 
@@ -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
- * The calls are made sequentially.
67
+ * If the `async` flag is used, the function calls the function with the values sequentially.
71
68
  */
72
- forEachAsync(fn: (value: T, key: K) => Promise<void>): Promise<void>;
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
  *
@@ -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.keys().length;
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
- * Calls the given function for each key-value pair in the dictionary.
93
- */
94
- forEach(fn) {
95
- for (const [key, value] of this.entries()) {
96
- fn(value, key);
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
- * Calls the given async function for each key-value pair in the dictionary.
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elyukai/utils",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "description": "A set of JavaScript helper functions.",
5
5
  "files": [
6
6
  "dist",