@elyukai/utils 0.1.6 → 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,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.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
+
16
+ ## [0.1.7](https://github.com/elyukai/ts-utils/compare/v0.1.6...v0.1.7) (2026-02-05)
17
+
18
+
19
+ ### Features
20
+
21
+ * add bind function for partiall application ([bc11da5](https://github.com/elyukai/ts-utils/commit/bc11da568c8ce15c071432307abcb7143f40b5ee))
22
+
5
23
  ## [0.1.6](https://github.com/elyukai/ts-utils/compare/v0.1.5...v0.1.6) (2026-01-28)
6
24
 
7
25
 
@@ -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
  /**
@@ -50,3 +50,21 @@ export declare const not: <T>(predicate: (value: T) => boolean) => (value: T) =>
50
50
  * ```
51
51
  */
52
52
  export declare const on: <T, U, V>(accessor: (value: T) => U, combinator: (a: U, b: U) => V) => ((a: T, b: T) => V);
53
+ /**
54
+ * Returns a function that binds the given arguments to the provided function.
55
+ *
56
+ * This is useful for creating partially applied functions or for fixing certain arguments of a function while leaving others flexible.
57
+ *
58
+ * @param fn The function to bind arguments to.
59
+ * @param fixedArgs The arguments to bind to the function.
60
+ * @returns A new function that takes the remaining arguments and calls the original function with both the fixed and remaining arguments.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const add = (a: number, b: number) => a + b;
65
+ * const add5 = bind(add, 5);
66
+ *
67
+ * console.log(add5(10)); // Output: 15
68
+ * ```
69
+ */
70
+ export declare const bind: <A extends unknown[], B extends unknown[], R>(fn: (...args: [...A, ...B]) => R, ...fixedArgs: A) => ((...restArgs: B) => R);
package/dist/function.js CHANGED
@@ -54,3 +54,21 @@ export const not = (predicate) => (value) => !predicate(value);
54
54
  * ```
55
55
  */
56
56
  export const on = (accessor, combinator) => (a, b) => combinator(accessor(a), accessor(b));
57
+ /**
58
+ * Returns a function that binds the given arguments to the provided function.
59
+ *
60
+ * This is useful for creating partially applied functions or for fixing certain arguments of a function while leaving others flexible.
61
+ *
62
+ * @param fn The function to bind arguments to.
63
+ * @param fixedArgs The arguments to bind to the function.
64
+ * @returns A new function that takes the remaining arguments and calls the original function with both the fixed and remaining arguments.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const add = (a: number, b: number) => a + b;
69
+ * const add5 = bind(add, 5);
70
+ *
71
+ * console.log(add5(10)); // Output: 15
72
+ * ```
73
+ */
74
+ export const bind = (fn, ...fixedArgs) => (...restArgs) => fn(...fixedArgs, ...restArgs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elyukai/utils",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "A set of JavaScript helper functions.",
5
5
  "files": [
6
6
  "dist",