@elyukai/utils 0.1.4 → 0.1.6

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.1.6](https://github.com/elyukai/ts-utils/compare/v0.1.5...v0.1.6) (2026-01-28)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * signature of map method ([3f8197f](https://github.com/elyukai/ts-utils/commit/3f8197fc7e8d5a003d493ae1460060e0c868e4db))
11
+
12
+ ## [0.1.5](https://github.com/elyukai/ts-utils/compare/v0.1.4...v0.1.5) (2026-01-28)
13
+
14
+
15
+ ### Features
16
+
17
+ * add toJSON to dictionary ([0418666](https://github.com/elyukai/ts-utils/commit/0418666bdb189c0c70813661ffb5763b31074ed0))
18
+ * types dictionary keys ([0df86ce](https://github.com/elyukai/ts-utils/commit/0df86ce50bbfdc13353b1a17ab3de9d9ca56d19d))
19
+
5
20
  ## [0.1.4](https://github.com/elyukai/ts-utils/compare/v0.1.3...v0.1.4) (2026-01-24)
6
21
 
7
22
 
@@ -6,44 +6,44 @@ import type { AnyNonNullish } from "./nullable.js";
6
6
  /**
7
7
  * An immutable dictionary mapping strings to values.
8
8
  */
9
- export declare class Dictionary<T extends AnyNonNullish | null> {
9
+ export declare class Dictionary<T extends AnyNonNullish | null, K extends string = string> {
10
10
  private record;
11
11
  /**
12
12
  * Creates a new dictionary from an indexed object.
13
13
  */
14
- constructor(record: Record<string, T>);
14
+ constructor(record: Partial<Record<K, T>>);
15
15
  /**
16
16
  * An empty dictionary.
17
17
  */
18
- static empty: Dictionary<never>;
18
+ static empty: Dictionary<never, never>;
19
19
  /**
20
20
  * Constructs a dictionary from an array of key-value pairs.
21
21
  */
22
- static fromEntries<T extends AnyNonNullish | null>(entries: [string, T][]): Dictionary<T>;
22
+ static fromEntries<T extends AnyNonNullish | null, K extends string = string>(entries: [K, T][]): Dictionary<T, K>;
23
23
  /**
24
24
  * Returns the value associated with the given key, or `undefined` if the key does not exist.
25
25
  */
26
- get(key: string): T | undefined;
26
+ get(key: K): T | undefined;
27
27
  /**
28
28
  * Returns the value associated with the given key mapped through the given function, or `undefined` if the key does not exist.
29
29
  */
30
- getMap<U>(key: string, mapFn: (value: T) => U): U | undefined;
30
+ getMap<U>(key: K, mapFn: (value: T) => U): U | undefined;
31
31
  /**
32
32
  * Checks if the dictionary has the given key.
33
33
  */
34
- has(key: string): boolean;
34
+ has(key: K): boolean;
35
35
  /**
36
36
  * Sets the given key to the given value.
37
37
  *
38
38
  * If the key already exists, its value will be overwritten.
39
39
  */
40
- set(key: string, value: T): Dictionary<T>;
40
+ set(key: K, value: T): Dictionary<T, K>;
41
41
  /**
42
42
  * Removes the given key from the dictionary.
43
- *
43
+ *hs
44
44
  * If the key does not exist, the same dictionary instance is returned.
45
45
  */
46
- remove(key: string): Dictionary<T>;
46
+ remove(key: K): Dictionary<T, K>;
47
47
  /**
48
48
  * The number of entries in the dictionary.
49
49
  */
@@ -51,7 +51,7 @@ export declare class Dictionary<T extends AnyNonNullish | null> {
51
51
  /**
52
52
  * Returns an array of key-value pairs in the dictionary.
53
53
  */
54
- entries(): [string, T][];
54
+ entries(): [K, T][];
55
55
  /**
56
56
  * Returns an array of values in the dictionary.
57
57
  */
@@ -59,17 +59,17 @@ export declare class Dictionary<T extends AnyNonNullish | null> {
59
59
  /**
60
60
  * Returns an array of keys in the dictionary.
61
61
  */
62
- keys(): string[];
62
+ keys(): K[];
63
63
  /**
64
64
  * Calls the given function for each key-value pair in the dictionary.
65
65
  */
66
- forEach(fn: (value: T, key: string) => void): void;
66
+ forEach(fn: (value: T, key: K) => void): void;
67
67
  /**
68
68
  * Calls the given async function for each key-value pair in the dictionary.
69
69
  *
70
70
  * The calls are made sequentially.
71
71
  */
72
- forEachAsync(fn: (value: T, key: string) => Promise<void>): Promise<void>;
72
+ forEachAsync(fn: (value: T, key: K) => Promise<void>): Promise<void>;
73
73
  /**
74
74
  * Create, modify, or remove the value for the given key based on its current value.
75
75
  *
@@ -77,39 +77,45 @@ export declare class Dictionary<T extends AnyNonNullish | null> {
77
77
  * If the `modifyFn` returns `undefined`, the key will be removed from the dictionary.
78
78
  * Otherwise, the key will be set to the new value returned by `modifyFn`.
79
79
  */
80
- modify(key: string, modifyFn: (currentValue: T | undefined) => T | undefined): Dictionary<T>;
80
+ modify(key: K, modifyFn: (currentValue: T | undefined) => T | undefined): Dictionary<T, K>;
81
81
  /**
82
82
  * Returns the first value that matches the given predicate, or `undefined` if
83
83
  * no such value exists.
84
84
  */
85
- find<U extends T>(predicate: (value: T, key: string) => value is U): U | undefined;
85
+ find<U extends T>(predicate: (value: T, key: K) => value is U): U | undefined;
86
86
  /**
87
87
  * Returns the first value that matches the given predicate, or `undefined` if
88
88
  * no such value exists.
89
89
  */
90
- find(predicate: (value: T, key: string) => boolean): T | undefined;
90
+ find(predicate: (value: T, key: K) => boolean): T | undefined;
91
91
  /**
92
92
  * Returns the first key that matches the given predicate, or `undefined` if no such key exists.
93
93
  */
94
- findKey(predicate: (value: T, key: string) => boolean): string | undefined;
94
+ findKey(predicate: (value: T, key: K) => boolean): string | undefined;
95
95
  /**
96
96
  * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
97
97
  */
98
- findEntry<U extends T>(predicate: (value: T, key: string) => value is U): [key: string, value: U] | undefined;
98
+ findEntry<U extends T>(predicate: (value: T, key: K) => value is U): [key: K, value: U] | undefined;
99
99
  /**
100
100
  * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
101
101
  */
102
- findEntry(predicate: (value: T, key: string) => boolean): [key: string, value: T] | undefined;
102
+ findEntry(predicate: (value: T, key: K) => boolean): [key: K, value: T] | undefined;
103
103
  /**
104
104
  * Applies the given mapping function to each key-value pair in the dictionary and returns the first non-`undefined` result, or `undefined` if no such result exists.
105
105
  */
106
- mapFirst<U extends AnyNonNullish | null>(mapFn: (value: T, key: string) => U | undefined): U | undefined;
106
+ mapFirst<U extends AnyNonNullish | null>(mapFn: (value: T, key: K) => U | undefined): U | undefined;
107
107
  /**
108
108
  * Applies a function to every key-value pair in the dictionary.
109
109
  */
110
- map<U extends AnyNonNullish | null>(mapFn: (value: T, key: string) => U): Dictionary<U>;
110
+ map<U extends AnyNonNullish | null>(mapFn: (value: T, key: K) => U): Dictionary<U, K>;
111
111
  /**
112
112
  * 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
113
  */
114
114
  reduce<U>(reducer: (acc: U, value: T, key: string) => U, initialValue: U): U;
115
+ /**
116
+ * Converts the dictionary to a plain object.
117
+ *
118
+ * This is automatically called when using `JSON.stringify`.
119
+ */
120
+ toJSON(): Partial<Record<K, T>>;
115
121
  }
@@ -2,7 +2,7 @@
2
2
  * Introduces a Dictionary class that represents an immutable mapping from strings to values.
3
3
  * @module
4
4
  */
5
- import { omitKeys } from "./object.js";
5
+ import { omitKeys, omitUndefinedKeys } from "./object.js";
6
6
  /**
7
7
  * An immutable dictionary mapping strings to values.
8
8
  */
@@ -12,7 +12,7 @@ export class Dictionary {
12
12
  * Creates a new dictionary from an indexed object.
13
13
  */
14
14
  constructor(record) {
15
- this.record = record;
15
+ this.record = omitUndefinedKeys(record);
16
16
  }
17
17
  /**
18
18
  * An empty dictionary.
@@ -53,7 +53,7 @@ export class Dictionary {
53
53
  }
54
54
  /**
55
55
  * Removes the given key from the dictionary.
56
- *
56
+ *hs
57
57
  * If the key does not exist, the same dictionary instance is returned.
58
58
  */
59
59
  remove(key) {
@@ -68,7 +68,7 @@ export class Dictionary {
68
68
  * The number of entries in the dictionary.
69
69
  */
70
70
  get size() {
71
- return Object.keys(this.record).length;
71
+ return this.keys().length;
72
72
  }
73
73
  /**
74
74
  * Returns an array of key-value pairs in the dictionary.
@@ -92,7 +92,7 @@ export class Dictionary {
92
92
  * Calls the given function for each key-value pair in the dictionary.
93
93
  */
94
94
  forEach(fn) {
95
- for (const [key, value] of Object.entries(this.record)) {
95
+ for (const [key, value] of this.entries()) {
96
96
  fn(value, key);
97
97
  }
98
98
  }
@@ -102,7 +102,7 @@ export class Dictionary {
102
102
  * The calls are made sequentially.
103
103
  */
104
104
  async forEachAsync(fn) {
105
- for (const [key, value] of Object.entries(this.record)) {
105
+ for (const [key, value] of this.entries()) {
106
106
  await fn(value, key);
107
107
  }
108
108
  }
@@ -140,7 +140,7 @@ export class Dictionary {
140
140
  * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
141
141
  */
142
142
  findEntry(predicate) {
143
- for (const [key, value] of Object.entries(this.record)) {
143
+ for (const [key, value] of this.entries()) {
144
144
  if (predicate(value, key)) {
145
145
  return [key, value];
146
146
  }
@@ -151,7 +151,7 @@ export class Dictionary {
151
151
  * Applies the given mapping function to each key-value pair in the dictionary and returns the first non-`undefined` result, or `undefined` if no such result exists.
152
152
  */
153
153
  mapFirst(mapFn) {
154
- for (const [key, value] of Object.entries(this.record)) {
154
+ for (const [key, value] of this.entries()) {
155
155
  const mapped = mapFn(value, key);
156
156
  if (mapped !== undefined) {
157
157
  return mapped;
@@ -164,7 +164,7 @@ export class Dictionary {
164
164
  */
165
165
  map(mapFn) {
166
166
  const newRecord = {};
167
- for (const [key, value] of Object.entries(this.record)) {
167
+ for (const [key, value] of this.entries()) {
168
168
  newRecord[key] = mapFn(value, key);
169
169
  }
170
170
  return new Dictionary(newRecord);
@@ -173,6 +173,14 @@ export class Dictionary {
173
173
  * 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.
174
174
  */
175
175
  reduce(reducer, initialValue) {
176
- return Object.entries(this.record).reduce((acc, [key, item]) => reducer(acc, item, key), initialValue);
176
+ return this.entries().reduce((acc, [key, item]) => reducer(acc, item, key), initialValue);
177
+ }
178
+ /**
179
+ * Converts the dictionary to a plain object.
180
+ *
181
+ * This is automatically called when using `JSON.stringify`.
182
+ */
183
+ toJSON() {
184
+ return { ...this.record };
177
185
  }
178
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elyukai/utils",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "A set of JavaScript helper functions.",
5
5
  "files": [
6
6
  "dist",