@ocavue/utils 1.1.0 → 1.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/dist/index.d.ts +97 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/map-values.test.ts +41 -0
- package/src/map-values.ts +43 -0
- package/src/object-entries.test.ts +33 -0
- package/src/object-entries.ts +58 -0
package/dist/index.d.ts
CHANGED
|
@@ -18,10 +18,10 @@ declare function isSet(value: unknown): value is Set<unknown>;
|
|
|
18
18
|
*
|
|
19
19
|
* Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).
|
|
20
20
|
*/
|
|
21
|
-
declare class DefaultMap<K, V> extends Map<K, V> {
|
|
21
|
+
declare class DefaultMap<K$1, V> extends Map<K$1, V> {
|
|
22
22
|
private readonly defaultFactory;
|
|
23
|
-
constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K, V]>);
|
|
24
|
-
get(key: K): V;
|
|
23
|
+
constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K$1, V]>);
|
|
24
|
+
get(key: K$1): V;
|
|
25
25
|
}
|
|
26
26
|
//#endregion
|
|
27
27
|
//#region src/dom.d.ts
|
|
@@ -110,7 +110,98 @@ declare function isDeepEqual(a: unknown, b: unknown): boolean;
|
|
|
110
110
|
*
|
|
111
111
|
* @public
|
|
112
112
|
*/
|
|
113
|
-
declare const mapGroupBy: <K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Map<K, T[]>;
|
|
113
|
+
declare const mapGroupBy: <K$1, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K$1) => Map<K$1, T[]>;
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/map-values.d.ts
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new object with the same keys as the input object, but with values
|
|
118
|
+
* transformed by the provided callback function. Similar to `Array.prototype.map()`
|
|
119
|
+
* but for object values.
|
|
120
|
+
|
|
121
|
+
* @param object - The object whose values will be transformed.
|
|
122
|
+
* @param callback - A function that transforms each value. Receives the value and
|
|
123
|
+
* its corresponding key as arguments.
|
|
124
|
+
* @returns A new object with the same keys but transformed values.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const prices = { apple: 1, banana: 2, orange: 3 }
|
|
129
|
+
* const doubled = mapValues(prices, (price) => price * 2)
|
|
130
|
+
* // Result: { apple: 2, banana: 4, orange: 6 }
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const users = { john: 25, jane: 30, bob: 35 }
|
|
136
|
+
* const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)
|
|
137
|
+
* // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const data = { a: '1', b: '2', c: '3' }
|
|
143
|
+
* const numbers = mapValues(data, (str) => parseInt(str, 10))
|
|
144
|
+
* // Result: { a: 1, b: 2, c: 3 }
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
declare function mapValues<ValueIn, ValueOut>(object: Record<string, ValueIn>, callback: (value: ValueIn, key: string) => ValueOut): Record<string, ValueOut>;
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/object-entries.d.ts
|
|
152
|
+
/**
|
|
153
|
+
* A TypeScript utility type that represents the entries of an object as a union of tuple types.
|
|
154
|
+
* Each tuple contains a key-value pair where the key and value types are precisely typed
|
|
155
|
+
* according to the input object type.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* type MyObject = { a: 1; b: 'B' }
|
|
160
|
+
* type MyEntries = ObjectEntries<MyObject>
|
|
161
|
+
* // ^ ["a", 1] | ["b", "B"]
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @public
|
|
165
|
+
*/
|
|
166
|
+
type ObjectEntries<T extends Record<string, any>> = { [K in keyof T]: [K, T[K]] }[keyof T];
|
|
167
|
+
/**
|
|
168
|
+
* A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys
|
|
169
|
+
* and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this
|
|
170
|
+
* function returns an array of tuples where each tuple is precisely typed according to the
|
|
171
|
+
* input object's structure.
|
|
172
|
+
*
|
|
173
|
+
* This is particularly useful when working with objects that have known, fixed property types
|
|
174
|
+
* and you want to maintain type safety when iterating over entries.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const myObject = { a: 1, b: 'hello', c: true } as const
|
|
179
|
+
* const entries = objectEntries(myObject)
|
|
180
|
+
* // Type: (["a", 1] | ["b", "hello"] | ["c", true])[]
|
|
181
|
+
*
|
|
182
|
+
* for (const [key, value] of entries) {
|
|
183
|
+
* // key is typed as "a" | "b" | "c"
|
|
184
|
+
* // value is typed as 1 | "hello" | true
|
|
185
|
+
* console.log(`${key}: ${value}`)
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* interface User {
|
|
192
|
+
* name: string
|
|
193
|
+
* age: number
|
|
194
|
+
* active: boolean
|
|
195
|
+
* }
|
|
196
|
+
*
|
|
197
|
+
* const user: User = { name: 'Alice', age: 30, active: true }
|
|
198
|
+
* const entries = objectEntries(user)
|
|
199
|
+
* // Type: (["name", string] | ["age", number] | ["active", boolean])[]
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @public
|
|
203
|
+
*/
|
|
204
|
+
declare const objectEntries: <T extends Record<string, any>>(obj: T) => ObjectEntries<T>[];
|
|
114
205
|
//#endregion
|
|
115
206
|
//#region src/object-group-by.d.ts
|
|
116
207
|
/**
|
|
@@ -118,7 +209,7 @@ declare const mapGroupBy: <K, T>(items: Iterable<T>, keySelector: (item: T, inde
|
|
|
118
209
|
*
|
|
119
210
|
* @public
|
|
120
211
|
*/
|
|
121
|
-
declare const objectGroupBy: <K extends PropertyKey, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Partial<Record<K, T[]>>;
|
|
212
|
+
declare const objectGroupBy: <K$1 extends PropertyKey, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K$1) => Partial<Record<K$1, T[]>>;
|
|
122
213
|
//#endregion
|
|
123
214
|
//#region src/once.d.ts
|
|
124
215
|
/**
|
|
@@ -142,5 +233,5 @@ declare function once<T>(fn: () => T): () => T;
|
|
|
142
233
|
*/
|
|
143
234
|
declare function sleep(ms: number): Promise<void>;
|
|
144
235
|
//#endregion
|
|
145
|
-
export { DefaultMap, formatBytes, getDocument, getDocumentElement, getId, getWindow, isDeepEqual, isDocument, isDocumentFragment, isElement, isElementLike, isHTMLElement, isMap, isMathMLElement, isNodeLike, isObject, isSVGElement, isSet, isShadowRoot, isTextNode, isWindowLike, mapGroupBy, objectGroupBy, once, sleep };
|
|
236
|
+
export { DefaultMap, type ObjectEntries, formatBytes, getDocument, getDocumentElement, getId, getWindow, isDeepEqual, isDocument, isDocumentFragment, isElement, isElementLike, isHTMLElement, isMap, isMathMLElement, isNodeLike, isObject, isSVGElement, isSet, isShadowRoot, isTextNode, isWindowLike, mapGroupBy, mapValues, objectEntries, objectGroupBy, once, sleep };
|
|
146
237
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":[],"mappings":";;AAGA;AASA;AAOgB,iBAhBA,QAAA,CAgBmC,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IAdvC,MAcuC,CAAA,MAAA,GAAA,MAAA,GAAA,MAAA,EAAA,OAAA,CAAA;;;;ACdtC,iBDOG,KAAA,CCPO,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IDOyB,GCPzB,CAAA,OAAA,EAAA,OAAA,CAAA;;;;AAG8C,iBDWrD,KAAA,CCXqD,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IDWrB,GCXqB,CAAA,OAAA,CAAA;;;;ADLrE;AASA;AAOA;;cCda,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/map-values.ts","../src/object-entries.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":[],"mappings":";;AAGA;AASA;AAOgB,iBAhBA,QAAA,CAgBmC,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IAdvC,MAcuC,CAAA,MAAA,GAAA,MAAA,GAAA,MAAA,EAAA,OAAA,CAAA;;;;ACdtC,iBDOG,KAAA,CCPO,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IDOyB,GCPzB,CAAA,OAAA,EAAA,OAAA,CAAA;;;;AAG8C,iBDWrD,KAAA,CCXqD,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IDWrB,GCXqB,CAAA,OAAA,CAAA;;;;ADLrE;AASA;AAOA;;cCda,2BAAyB,IAAI,KAAG;;EAAhC,WAAA,CAAA,cAAU,EAAA,GAAA,GAGa,CAHb,EAAA,QAAA,CAAA,EAG2B,QAH3B,CAAA,SAAA,CAG8C,GAH9C,EAGiD,CAHjD,CAAA,CAAA;EAAmB,GAAA,CAAA,GAAA,EAQtB,GARsB,CAAA,EAQlB,CARkB;;;;;ADF1C;A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YQ6BX,WR7BW,EAAA,CAAA,CAAA,CAAA,KAAA,EQ8BzC,QR9ByC,CQ8BhC,CR9BgC,CAAA,EAAA,WAAA,EAAA,CAAA,IAAA,EQ+B5B,CR/B4B,EAAA,KAAA,EAAA,MAAA,EAAA,GQ+BP,GR/BO,EAAA,GQgC7C,ORhC6C,CQgCrC,MRhCqC,CQgC9B,GRhC8B,EQgC3B,CRhC2B,EAAA,CAAA,CAAA;;;;ADLlD;AASA;AAOA;;;;ACdA;;;;;;AAGkD,iBSKlC,ITLkC,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,GAAA,GSKhB,CTLgB,CAAA,EAAA,GAAA,GSKN,CTLM;;;;ADLlD;AASA;AAOgB,iBWhBA,KAAA,CXgBmC,EAAA,EAAA,MAAA,CAAA,EWhBhB,OXgBgB,CAAA,IAAA,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -252,6 +252,88 @@ function mapGroupByNative(items, keySelector) {
|
|
|
252
252
|
*/
|
|
253
253
|
const mapGroupBy = !!Map.groupBy ? mapGroupByNative : mapGroupByPolyfill;
|
|
254
254
|
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/map-values.ts
|
|
257
|
+
/**
|
|
258
|
+
* Creates a new object with the same keys as the input object, but with values
|
|
259
|
+
* transformed by the provided callback function. Similar to `Array.prototype.map()`
|
|
260
|
+
* but for object values.
|
|
261
|
+
|
|
262
|
+
* @param object - The object whose values will be transformed.
|
|
263
|
+
* @param callback - A function that transforms each value. Receives the value and
|
|
264
|
+
* its corresponding key as arguments.
|
|
265
|
+
* @returns A new object with the same keys but transformed values.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* const prices = { apple: 1, banana: 2, orange: 3 }
|
|
270
|
+
* const doubled = mapValues(prices, (price) => price * 2)
|
|
271
|
+
* // Result: { apple: 2, banana: 4, orange: 6 }
|
|
272
|
+
* ```
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```typescript
|
|
276
|
+
* const users = { john: 25, jane: 30, bob: 35 }
|
|
277
|
+
* const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)
|
|
278
|
+
* // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const data = { a: '1', b: '2', c: '3' }
|
|
284
|
+
* const numbers = mapValues(data, (str) => parseInt(str, 10))
|
|
285
|
+
* // Result: { a: 1, b: 2, c: 3 }
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @public
|
|
289
|
+
*/
|
|
290
|
+
function mapValues(object, callback) {
|
|
291
|
+
let result = {};
|
|
292
|
+
for (const [key, value] of Object.entries(object)) result[key] = callback(value, key);
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/object-entries.ts
|
|
298
|
+
/**
|
|
299
|
+
* A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys
|
|
300
|
+
* and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this
|
|
301
|
+
* function returns an array of tuples where each tuple is precisely typed according to the
|
|
302
|
+
* input object's structure.
|
|
303
|
+
*
|
|
304
|
+
* This is particularly useful when working with objects that have known, fixed property types
|
|
305
|
+
* and you want to maintain type safety when iterating over entries.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```typescript
|
|
309
|
+
* const myObject = { a: 1, b: 'hello', c: true } as const
|
|
310
|
+
* const entries = objectEntries(myObject)
|
|
311
|
+
* // Type: (["a", 1] | ["b", "hello"] | ["c", true])[]
|
|
312
|
+
*
|
|
313
|
+
* for (const [key, value] of entries) {
|
|
314
|
+
* // key is typed as "a" | "b" | "c"
|
|
315
|
+
* // value is typed as 1 | "hello" | true
|
|
316
|
+
* console.log(`${key}: ${value}`)
|
|
317
|
+
* }
|
|
318
|
+
* ```
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* interface User {
|
|
323
|
+
* name: string
|
|
324
|
+
* age: number
|
|
325
|
+
* active: boolean
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* const user: User = { name: 'Alice', age: 30, active: true }
|
|
329
|
+
* const entries = objectEntries(user)
|
|
330
|
+
* // Type: (["name", string] | ["age", number] | ["active", boolean])[]
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @public
|
|
334
|
+
*/
|
|
335
|
+
const objectEntries = Object.entries;
|
|
336
|
+
|
|
255
337
|
//#endregion
|
|
256
338
|
//#region src/object-group-by.ts
|
|
257
339
|
/**
|
|
@@ -320,5 +402,5 @@ function sleep(ms) {
|
|
|
320
402
|
}
|
|
321
403
|
|
|
322
404
|
//#endregion
|
|
323
|
-
export { DefaultMap, formatBytes, getDocument, getDocumentElement, getId, getWindow, isDeepEqual, isDocument, isDocumentFragment, isElement, isElementLike, isHTMLElement, isMap, isMathMLElement, isNodeLike, isObject, isSVGElement, isSet, isShadowRoot, isTextNode, isWindowLike, mapGroupBy, objectGroupBy, once, sleep };
|
|
405
|
+
export { DefaultMap, formatBytes, getDocument, getDocumentElement, getId, getWindow, isDeepEqual, isDocument, isDocumentFragment, isElement, isElementLike, isHTMLElement, isMap, isMathMLElement, isNodeLike, isObject, isSVGElement, isSet, isShadowRoot, isTextNode, isWindowLike, mapGroupBy, mapValues, objectEntries, objectGroupBy, once, sleep };
|
|
324
406
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["NodeType.ELEMENT_NODE","NodeType.TEXT_NODE","NodeType.DOCUMENT_NODE","NodeType.DOCUMENT_FRAGMENT_NODE","mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]>","result: Partial<Record<K, T[]>>","objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>>","result: T"],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom-node-type.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":["/**\n * Checks if the given value is an object.\n */\nexport function isObject(\n value: unknown,\n): value is Record<string | symbol | number, unknown> {\n return value != null && typeof value === 'object'\n}\n\n/**\n * Checks if the given value is a Map.\n */\nexport function isMap(value: unknown): value is Map<unknown, unknown> {\n return value instanceof Map\n}\n\n/**\n * Checks if the given value is a Set.\n */\nexport function isSet(value: unknown): value is Set<unknown> {\n return value instanceof Set\n}\n","/**\n * A map that automatically creates values for missing keys using a factory function.\n *\n * Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n private readonly defaultFactory: () => V\n\n constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K, V]>) {\n super(iterable)\n this.defaultFactory = defaultFactory\n }\n\n override get(key: K): V {\n if (this.has(key)) {\n return super.get(key)!\n }\n const value = this.defaultFactory()\n this.set(key, value)\n return value\n }\n}\n","// prettier-ignore\nexport const ELEMENT_NODE = 1 satisfies typeof Node.ELEMENT_NODE;\n// prettier-ignore\nexport const ATTRIBUTE_NODE = 2 satisfies typeof Node.ATTRIBUTE_NODE;\n// prettier-ignore\nexport const TEXT_NODE = 3 satisfies typeof Node.TEXT_NODE;\n// prettier-ignore\nexport const CDATA_SECTION_NODE = 4 satisfies typeof Node.CDATA_SECTION_NODE;\n// prettier-ignore\nexport const ENTITY_REFERENCE_NODE = 5 satisfies typeof Node.ENTITY_REFERENCE_NODE;\n// prettier-ignore\nexport const ENTITY_NODE = 6 satisfies typeof Node.ENTITY_NODE;\n// prettier-ignore\nexport const PROCESSING_INSTRUCTION_NODE = 7 satisfies typeof Node.PROCESSING_INSTRUCTION_NODE;\n// prettier-ignore\nexport const COMMENT_NODE = 8 satisfies typeof Node.COMMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_NODE = 9 satisfies typeof Node.DOCUMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_TYPE_NODE = 10 satisfies typeof Node.DOCUMENT_TYPE_NODE;\n// prettier-ignore\nexport const DOCUMENT_FRAGMENT_NODE = 11 satisfies typeof Node.DOCUMENT_FRAGMENT_NODE;\n// prettier-ignore\nexport const NOTATION_NODE = 12 satisfies typeof Node.NOTATION_NODE;\n","import { isObject } from './checker'\nimport * as NodeType from './dom-node-type'\n\n/**\n * Checks if the given DOM node is an Element.\n */\nexport function isElement(node: Node): node is Element {\n return node.nodeType === NodeType.ELEMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a Text node.\n */\nexport function isTextNode(node: Node): node is Text {\n return node.nodeType === NodeType.TEXT_NODE\n}\n\n/**\n * Checks if the given DOM node is an HTMLElement.\n */\nexport function isHTMLElement(node: Node): node is HTMLElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/1999/xhtml'\n}\n\n/**\n * Checks if the given DOM node is an SVGElement.\n */\nexport function isSVGElement(node: Node): node is SVGElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/2000/svg'\n}\n\n/**\n * Checks if the given DOM node is an MathMLElement.\n */\nexport function isMathMLElement(node: Node): node is MathMLElement {\n return (\n isElement(node) &&\n node.namespaceURI === 'http://www.w3.org/1998/Math/MathML'\n )\n}\n\n/**\n * Checks if the given DOM node is a Document.\n */\nexport function isDocument(node: Node): node is Document {\n return node.nodeType === NodeType.DOCUMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a DocumentFragment.\n */\nexport function isDocumentFragment(node: Node): node is DocumentFragment {\n return node.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a ShadowRoot.\n */\nexport function isShadowRoot(node: Node): node is ShadowRoot {\n return isDocumentFragment(node) && 'host' in node && isElementLike(node.host)\n}\n\n/**\n * Checks if an unknown value is likely a DOM node.\n */\nexport function isNodeLike(value: unknown): value is Node {\n return isObject(value) && value.nodeType !== undefined\n}\n\n/**\n * Checks if an unknown value is likely a DOM element.\n */\nexport function isElementLike(value: unknown): value is Element {\n return (\n isObject(value) &&\n value.nodeType === NodeType.ELEMENT_NODE &&\n typeof value.nodeName === 'string'\n )\n}\n\n/**\n * Checks if the given value is likely a Window object.\n */\nexport function isWindowLike(value: unknown): value is Window {\n return isObject(value) && value.window === value\n}\n\n/**\n * Gets the window object for the given target or the global window object if no\n * target is provided.\n */\nexport function getWindow(\n target?: Node | ShadowRoot | Document | null,\n): Window & typeof globalThis {\n if (target) {\n if (isShadowRoot(target)) {\n return getWindow(target.host)\n }\n if (isDocument(target)) {\n return target.defaultView || window\n }\n if (isElement(target)) {\n return target.ownerDocument?.defaultView || window\n }\n }\n return window\n}\n\n/**\n * Gets the document for the given target or the global document if no target is\n * provided.\n */\nexport function getDocument(\n target?: Element | Window | Node | Document | null,\n): Document {\n if (target) {\n if (isWindowLike(target)) {\n return target.document\n }\n if (isDocument(target)) {\n return target\n }\n return target.ownerDocument || document\n }\n return document\n}\n\n/**\n * Gets a reference to the root node of the document based on the given target.\n */\nexport function getDocumentElement(\n target?: Element | Node | Window | Document | null,\n): HTMLElement {\n return getDocument(target).documentElement\n}\n","/**\n * Formats a number of bytes into a human-readable string.\n * @param bytes - The number of bytes to format.\n * @returns A string representing the number of bytes in a human-readable format.\n */\nexport function formatBytes(bytes: number): string {\n const units = ['B', 'KB', 'MB', 'GB']\n let unitIndex = 0\n let num = bytes\n while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {\n num /= 1024\n unitIndex++\n }\n const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1\n return `${num.toFixed(fraction)}${units[unitIndex]}`\n}\n","let id = 0\n\n/**\n * Generates a unique positive integer.\n */\nexport function getId(): number {\n id = (id % Number.MAX_SAFE_INTEGER) + 1\n return id\n}\n","import { isMap, isSet } from './checker'\n\n/**\n * Whether two values are deeply equal.\n */\nexport function isDeepEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true\n }\n\n // Handle null and undefined early\n if (a == null || b == null) {\n return false\n }\n\n const aType = typeof a\n const bType = typeof b\n if (aType !== bType) {\n return false\n }\n\n if (aType === 'number' && Number.isNaN(a) && Number.isNaN(b)) {\n return true\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n const size = a.length\n for (let i = 0; i < size; i++) {\n if (!isDeepEqual(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n if (isSet(a)) {\n if (!isSet(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const value of a) {\n if (!b.has(value)) {\n return false\n }\n }\n return true\n }\n\n if (isMap(a)) {\n if (!isMap(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const key of a.keys()) {\n if (!b.has(key)) {\n return false\n }\n if (!isDeepEqual(a.get(key), b.get(key))) {\n return false\n }\n }\n return true\n }\n\n if (typeof a === 'object' && typeof b === 'object') {\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n if (aKeys.length !== bKeys.length) {\n return false\n }\n for (const key of aKeys) {\n if (\n !isDeepEqual(\n (a as Record<string, unknown>)[key],\n (b as Record<string, unknown>)[key],\n )\n ) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n","/**\n * @internal\n */\nexport function mapGroupByPolyfill<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n const map = new Map<K, T[]>()\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = map.get(key)\n if (group) {\n group.push(item)\n } else {\n map.set(key, [item])\n }\n index++\n }\n return map\n}\n\n/**\n * @internal\n */\nexport function mapGroupByNative<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n return Map.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Map.groupBy` static method.\n *\n * @public\n */\nexport const mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]> = !!Map.groupBy ? mapGroupByNative : mapGroupByPolyfill\n","/**\n * @internal\n */\nexport function objectGroupByPolyfill<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n const result: Partial<Record<K, T[]>> = {}\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = result[key]\n if (group) {\n group.push(item)\n } else {\n result[key] = [item]\n }\n index++\n }\n return result\n}\n\n/**\n * @internal\n */\nexport function objectGroupByNative<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n return Object.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Object.groupBy` static method.\n *\n * @public\n */\nexport const objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>> = !!Object.groupBy\n ? objectGroupByNative\n : objectGroupByPolyfill\n","/**\n * Creates a function that will only execute the provided function once.\n * Subsequent calls will return the cached result from the first execution.\n *\n * @param fn The function to execute once\n * @returns A function that will only execute the provided function once\n * @example\n * ```ts\n * const getValue = once(() => expensiveOperation())\n * getValue() // executes expensiveOperation\n * getValue() // returns cached result\n * ```\n */\nexport function once<T>(fn: () => T): () => T {\n let called = false\n let result: T\n return () => {\n if (!called) {\n result = fn()\n called = true\n // @ts-expect-error - micro-optimization for memory management\n fn = undefined\n }\n return result\n }\n}\n","/**\n * Sleep for a given number of milliseconds.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n"],"mappings":";;;;AAGA,SAAgB,SACd,OACoD;AACpD,QAAO,SAAS,QAAQ,OAA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K,CAAC;AAEtB;;AAEF,QAAO;;;;;AAMT,SAAgB,iBACd,OACA,aACa;AACb,QAAO,IAAI,QAAQ,OAAO,YAAY;;;;;;;AAQxC,MAAaI,aAGM,CAAC,CAAC,IAAI,UAAU,mBAAmB;;;;;;;ACrCtD,SAAgB,sBACd,OACA,aACyB;CACzB,MAAMC,SAAkC,EAAE;CAC1C,IAAI,QAAQ;AACZ,MAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,MAAM,YAAY,MAAM,MAAM;EACpC,MAAM,QAAQ,OAAO;AACrB,MAAI,MACF,OAAM,KAAK,KAAK;MAEhB,QAAO,OAAO,CAAC,KAAK;AAEtB;;AAEF,QAAO;;;;;AAMT,SAAgB,oBACd,OACA,aACyB;AACzB,QAAO,OAAO,QAAQ,OAAO,YAAY;;;;;;;AAQ3C,MAAaC,gBAGkB,CAAC,CAAC,OAAO,UACpC,sBACA;;;;;;;;;;;;;;;;;AC7BJ,SAAgB,KAAQ,IAAsB;CAC5C,IAAI,SAAS;CACb,IAAIC;AACJ,cAAa;AACX,MAAI,CAAC,QAAQ;AACX,YAAS,IAAI;AACb,YAAS;AAET,QAAK;;AAEP,SAAO;;;;;;;;;ACpBX,SAAgB,MAAM,IAA2B;AAC/C,QAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["NodeType.ELEMENT_NODE","NodeType.TEXT_NODE","NodeType.DOCUMENT_NODE","NodeType.DOCUMENT_FRAGMENT_NODE","mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]>","objectEntries: <T extends Record<string, any>>(\n obj: T,\n) => ObjectEntries<T>[]","result: Partial<Record<K, T[]>>","objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>>","result: T"],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom-node-type.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/map-values.ts","../src/object-entries.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":["/**\n * Checks if the given value is an object.\n */\nexport function isObject(\n value: unknown,\n): value is Record<string | symbol | number, unknown> {\n return value != null && typeof value === 'object'\n}\n\n/**\n * Checks if the given value is a Map.\n */\nexport function isMap(value: unknown): value is Map<unknown, unknown> {\n return value instanceof Map\n}\n\n/**\n * Checks if the given value is a Set.\n */\nexport function isSet(value: unknown): value is Set<unknown> {\n return value instanceof Set\n}\n","/**\n * A map that automatically creates values for missing keys using a factory function.\n *\n * Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n private readonly defaultFactory: () => V\n\n constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K, V]>) {\n super(iterable)\n this.defaultFactory = defaultFactory\n }\n\n override get(key: K): V {\n if (this.has(key)) {\n return super.get(key)!\n }\n const value = this.defaultFactory()\n this.set(key, value)\n return value\n }\n}\n","// prettier-ignore\nexport const ELEMENT_NODE = 1 satisfies typeof Node.ELEMENT_NODE;\n// prettier-ignore\nexport const ATTRIBUTE_NODE = 2 satisfies typeof Node.ATTRIBUTE_NODE;\n// prettier-ignore\nexport const TEXT_NODE = 3 satisfies typeof Node.TEXT_NODE;\n// prettier-ignore\nexport const CDATA_SECTION_NODE = 4 satisfies typeof Node.CDATA_SECTION_NODE;\n// prettier-ignore\nexport const ENTITY_REFERENCE_NODE = 5 satisfies typeof Node.ENTITY_REFERENCE_NODE;\n// prettier-ignore\nexport const ENTITY_NODE = 6 satisfies typeof Node.ENTITY_NODE;\n// prettier-ignore\nexport const PROCESSING_INSTRUCTION_NODE = 7 satisfies typeof Node.PROCESSING_INSTRUCTION_NODE;\n// prettier-ignore\nexport const COMMENT_NODE = 8 satisfies typeof Node.COMMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_NODE = 9 satisfies typeof Node.DOCUMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_TYPE_NODE = 10 satisfies typeof Node.DOCUMENT_TYPE_NODE;\n// prettier-ignore\nexport const DOCUMENT_FRAGMENT_NODE = 11 satisfies typeof Node.DOCUMENT_FRAGMENT_NODE;\n// prettier-ignore\nexport const NOTATION_NODE = 12 satisfies typeof Node.NOTATION_NODE;\n","import { isObject } from './checker'\nimport * as NodeType from './dom-node-type'\n\n/**\n * Checks if the given DOM node is an Element.\n */\nexport function isElement(node: Node): node is Element {\n return node.nodeType === NodeType.ELEMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a Text node.\n */\nexport function isTextNode(node: Node): node is Text {\n return node.nodeType === NodeType.TEXT_NODE\n}\n\n/**\n * Checks if the given DOM node is an HTMLElement.\n */\nexport function isHTMLElement(node: Node): node is HTMLElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/1999/xhtml'\n}\n\n/**\n * Checks if the given DOM node is an SVGElement.\n */\nexport function isSVGElement(node: Node): node is SVGElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/2000/svg'\n}\n\n/**\n * Checks if the given DOM node is an MathMLElement.\n */\nexport function isMathMLElement(node: Node): node is MathMLElement {\n return (\n isElement(node) &&\n node.namespaceURI === 'http://www.w3.org/1998/Math/MathML'\n )\n}\n\n/**\n * Checks if the given DOM node is a Document.\n */\nexport function isDocument(node: Node): node is Document {\n return node.nodeType === NodeType.DOCUMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a DocumentFragment.\n */\nexport function isDocumentFragment(node: Node): node is DocumentFragment {\n return node.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a ShadowRoot.\n */\nexport function isShadowRoot(node: Node): node is ShadowRoot {\n return isDocumentFragment(node) && 'host' in node && isElementLike(node.host)\n}\n\n/**\n * Checks if an unknown value is likely a DOM node.\n */\nexport function isNodeLike(value: unknown): value is Node {\n return isObject(value) && value.nodeType !== undefined\n}\n\n/**\n * Checks if an unknown value is likely a DOM element.\n */\nexport function isElementLike(value: unknown): value is Element {\n return (\n isObject(value) &&\n value.nodeType === NodeType.ELEMENT_NODE &&\n typeof value.nodeName === 'string'\n )\n}\n\n/**\n * Checks if the given value is likely a Window object.\n */\nexport function isWindowLike(value: unknown): value is Window {\n return isObject(value) && value.window === value\n}\n\n/**\n * Gets the window object for the given target or the global window object if no\n * target is provided.\n */\nexport function getWindow(\n target?: Node | ShadowRoot | Document | null,\n): Window & typeof globalThis {\n if (target) {\n if (isShadowRoot(target)) {\n return getWindow(target.host)\n }\n if (isDocument(target)) {\n return target.defaultView || window\n }\n if (isElement(target)) {\n return target.ownerDocument?.defaultView || window\n }\n }\n return window\n}\n\n/**\n * Gets the document for the given target or the global document if no target is\n * provided.\n */\nexport function getDocument(\n target?: Element | Window | Node | Document | null,\n): Document {\n if (target) {\n if (isWindowLike(target)) {\n return target.document\n }\n if (isDocument(target)) {\n return target\n }\n return target.ownerDocument || document\n }\n return document\n}\n\n/**\n * Gets a reference to the root node of the document based on the given target.\n */\nexport function getDocumentElement(\n target?: Element | Node | Window | Document | null,\n): HTMLElement {\n return getDocument(target).documentElement\n}\n","/**\n * Formats a number of bytes into a human-readable string.\n * @param bytes - The number of bytes to format.\n * @returns A string representing the number of bytes in a human-readable format.\n */\nexport function formatBytes(bytes: number): string {\n const units = ['B', 'KB', 'MB', 'GB']\n let unitIndex = 0\n let num = bytes\n while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {\n num /= 1024\n unitIndex++\n }\n const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1\n return `${num.toFixed(fraction)}${units[unitIndex]}`\n}\n","let id = 0\n\n/**\n * Generates a unique positive integer.\n */\nexport function getId(): number {\n id = (id % Number.MAX_SAFE_INTEGER) + 1\n return id\n}\n","import { isMap, isSet } from './checker'\n\n/**\n * Whether two values are deeply equal.\n */\nexport function isDeepEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true\n }\n\n // Handle null and undefined early\n if (a == null || b == null) {\n return false\n }\n\n const aType = typeof a\n const bType = typeof b\n if (aType !== bType) {\n return false\n }\n\n if (aType === 'number' && Number.isNaN(a) && Number.isNaN(b)) {\n return true\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n const size = a.length\n for (let i = 0; i < size; i++) {\n if (!isDeepEqual(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n if (isSet(a)) {\n if (!isSet(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const value of a) {\n if (!b.has(value)) {\n return false\n }\n }\n return true\n }\n\n if (isMap(a)) {\n if (!isMap(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const key of a.keys()) {\n if (!b.has(key)) {\n return false\n }\n if (!isDeepEqual(a.get(key), b.get(key))) {\n return false\n }\n }\n return true\n }\n\n if (typeof a === 'object' && typeof b === 'object') {\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n if (aKeys.length !== bKeys.length) {\n return false\n }\n for (const key of aKeys) {\n if (\n !isDeepEqual(\n (a as Record<string, unknown>)[key],\n (b as Record<string, unknown>)[key],\n )\n ) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n","/**\n * @internal\n */\nexport function mapGroupByPolyfill<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n const map = new Map<K, T[]>()\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = map.get(key)\n if (group) {\n group.push(item)\n } else {\n map.set(key, [item])\n }\n index++\n }\n return map\n}\n\n/**\n * @internal\n */\nexport function mapGroupByNative<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n return Map.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Map.groupBy` static method.\n *\n * @public\n */\nexport const mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]> = !!Map.groupBy ? mapGroupByNative : mapGroupByPolyfill\n","/**\n * Creates a new object with the same keys as the input object, but with values\n * transformed by the provided callback function. Similar to `Array.prototype.map()`\n * but for object values.\n\n * @param object - The object whose values will be transformed.\n * @param callback - A function that transforms each value. Receives the value and\n * its corresponding key as arguments.\n * @returns A new object with the same keys but transformed values.\n *\n * @example\n * ```typescript\n * const prices = { apple: 1, banana: 2, orange: 3 }\n * const doubled = mapValues(prices, (price) => price * 2)\n * // Result: { apple: 2, banana: 4, orange: 6 }\n * ```\n *\n * @example\n * ```typescript\n * const users = { john: 25, jane: 30, bob: 35 }\n * const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)\n * // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }\n * ```\n *\n * @example\n * ```typescript\n * const data = { a: '1', b: '2', c: '3' }\n * const numbers = mapValues(data, (str) => parseInt(str, 10))\n * // Result: { a: 1, b: 2, c: 3 }\n * ```\n *\n * @public\n */\nexport function mapValues<ValueIn, ValueOut>(\n object: Record<string, ValueIn>,\n callback: (value: ValueIn, key: string) => ValueOut,\n): Record<string, ValueOut> {\n let result = {} as Record<string, ValueOut>\n for (const [key, value] of Object.entries(object)) {\n result[key] = callback(value, key)\n }\n return result\n}\n","/**\n * A TypeScript utility type that represents the entries of an object as a union of tuple types.\n * Each tuple contains a key-value pair where the key and value types are precisely typed\n * according to the input object type.\n *\n * @example\n * ```typescript\n * type MyObject = { a: 1; b: 'B' }\n * type MyEntries = ObjectEntries<MyObject>\n * // ^ [\"a\", 1] | [\"b\", \"B\"]\n * ```\n *\n * @public\n */\nexport type ObjectEntries<T extends Record<string, any>> = {\n [K in keyof T]: [K, T[K]]\n}[keyof T]\n\n/**\n * A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys\n * and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this\n * function returns an array of tuples where each tuple is precisely typed according to the\n * input object's structure.\n *\n * This is particularly useful when working with objects that have known, fixed property types\n * and you want to maintain type safety when iterating over entries.\n *\n * @example\n * ```typescript\n * const myObject = { a: 1, b: 'hello', c: true } as const\n * const entries = objectEntries(myObject)\n * // Type: ([\"a\", 1] | [\"b\", \"hello\"] | [\"c\", true])[]\n *\n * for (const [key, value] of entries) {\n * // key is typed as \"a\" | \"b\" | \"c\"\n * // value is typed as 1 | \"hello\" | true\n * console.log(`${key}: ${value}`)\n * }\n * ```\n *\n * @example\n * ```typescript\n * interface User {\n * name: string\n * age: number\n * active: boolean\n * }\n *\n * const user: User = { name: 'Alice', age: 30, active: true }\n * const entries = objectEntries(user)\n * // Type: ([\"name\", string] | [\"age\", number] | [\"active\", boolean])[]\n * ```\n *\n * @public\n */\nexport const objectEntries: <T extends Record<string, any>>(\n obj: T,\n) => ObjectEntries<T>[] = Object.entries\n","/**\n * @internal\n */\nexport function objectGroupByPolyfill<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n const result: Partial<Record<K, T[]>> = {}\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = result[key]\n if (group) {\n group.push(item)\n } else {\n result[key] = [item]\n }\n index++\n }\n return result\n}\n\n/**\n * @internal\n */\nexport function objectGroupByNative<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n return Object.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Object.groupBy` static method.\n *\n * @public\n */\nexport const objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>> = !!Object.groupBy\n ? objectGroupByNative\n : objectGroupByPolyfill\n","/**\n * Creates a function that will only execute the provided function once.\n * Subsequent calls will return the cached result from the first execution.\n *\n * @param fn The function to execute once\n * @returns A function that will only execute the provided function once\n * @example\n * ```ts\n * const getValue = once(() => expensiveOperation())\n * getValue() // executes expensiveOperation\n * getValue() // returns cached result\n * ```\n */\nexport function once<T>(fn: () => T): () => T {\n let called = false\n let result: T\n return () => {\n if (!called) {\n result = fn()\n called = true\n // @ts-expect-error - micro-optimization for memory management\n fn = undefined\n }\n return result\n }\n}\n","/**\n * Sleep for a given number of milliseconds.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n"],"mappings":";;;;AAGA,SAAgB,SACd,OACoD;AACpD,QAAO,SAAS,QAAQ,OAAO,UAAU;;;;;AAM3C,SAAgB,MAAM,OAAgD;AACpE,QAAO,iBAAiB;;;;;AAM1B,SAAgB,MAAM,OAAuC;AAC3D,QAAO,iBAAiB;;;;;;;;;;ACf1B,IAAa,aAAb,cAAsC,IAAU;CAG9C,YAAY,gBAAyB,UAAsC;AACzE,QAAM,SAAS;AACf,OAAK,iBAAiB;;CAGxB,AAAS,IAAI,KAAW;AACtB,MAAI,KAAK,IAAI,IAAI,CACf,QAAO,MAAM,IAAI,IAAI;EAEvB,MAAM,QAAQ,KAAK,gBAAgB;AACnC,OAAK,IAAI,KAAK,MAAM;AACpB,SAAO;;;;;;AClBX,MAAa,eAAe;AAI5B,MAAa,YAAY;AAYzB,MAAa,gBAAgB;AAI7B,MAAa,yBAAyB;;;;;;;ACftC,SAAgB,UAAU,MAA6B;AACrD,QAAO,KAAK,aAAaA;;;;;AAM3B,SAAgB,WAAW,MAA0B;AACnD,QAAO,KAAK,aAAaC;;;;;AAM3B,SAAgB,cAAc,MAAiC;AAC7D,QAAO,UAAU,KAAK,IAAI,KAAK,iBAAiB;;;;;AAMlD,SAAgB,aAAa,MAAgC;AAC3D,QAAO,UAAU,KAAK,IAAI,KAAK,iBAAiB;;;;;AAMlD,SAAgB,gBAAgB,MAAmC;AACjE,QACE,UAAU,KAAK,IACf,KAAK,iBAAiB;;;;;AAO1B,SAAgB,WAAW,MAA8B;AACvD,QAAO,KAAK,aAAaC;;;;;AAM3B,SAAgB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,SAAS;CACb,IAAIC;AACJ,cAAa;AACX,MAAI,CAAC,QAAQ;AACX,YAAS,IAAI;AACb,YAAS;AAET,QAAK;;AAEP,SAAO;;;;;;;;;ACpBX,SAAgB,MAAM,IAA2B;AAC/C,QAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ export { formatBytes } from './format-bytes'
|
|
|
5
5
|
export { getId } from './get-id'
|
|
6
6
|
export { isDeepEqual } from './is-deep-equal'
|
|
7
7
|
export { mapGroupBy } from './map-group-by'
|
|
8
|
+
export { mapValues } from './map-values'
|
|
9
|
+
export { objectEntries, type ObjectEntries } from './object-entries'
|
|
8
10
|
export { objectGroupBy } from './object-group-by'
|
|
9
11
|
export { once } from './once'
|
|
10
12
|
export { sleep } from './sleep'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import { mapValues } from './map-values'
|
|
6
|
+
|
|
7
|
+
describe('mapValues', () => {
|
|
8
|
+
it('transforms values with callback function', () => {
|
|
9
|
+
const prices = { apple: 1, banana: 2, orange: 3 }
|
|
10
|
+
const doubled = mapValues(prices, (price) => price * 2)
|
|
11
|
+
|
|
12
|
+
expect(doubled).toEqual({ apple: 2, banana: 4, orange: 6 })
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('provides key to callback function', () => {
|
|
16
|
+
const users = { john: 25, jane: 30 }
|
|
17
|
+
const greetings = mapValues(
|
|
18
|
+
users,
|
|
19
|
+
(age, name) => `${name} is ${age} years old`,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
expect(greetings).toEqual({
|
|
23
|
+
john: 'john is 25 years old',
|
|
24
|
+
jane: 'jane is 30 years old',
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('handles empty objects', () => {
|
|
29
|
+
const empty = {}
|
|
30
|
+
const result = mapValues(empty, (value) => value)
|
|
31
|
+
|
|
32
|
+
expect(result).toEqual({})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('transforms value types', () => {
|
|
36
|
+
const data = { a: '1', b: '2', c: '3' }
|
|
37
|
+
const numbers = mapValues(data, (str) => Number.parseInt(str, 10))
|
|
38
|
+
|
|
39
|
+
expect(numbers).toEqual({ a: 1, b: 2, c: 3 })
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object with the same keys as the input object, but with values
|
|
3
|
+
* transformed by the provided callback function. Similar to `Array.prototype.map()`
|
|
4
|
+
* but for object values.
|
|
5
|
+
|
|
6
|
+
* @param object - The object whose values will be transformed.
|
|
7
|
+
* @param callback - A function that transforms each value. Receives the value and
|
|
8
|
+
* its corresponding key as arguments.
|
|
9
|
+
* @returns A new object with the same keys but transformed values.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const prices = { apple: 1, banana: 2, orange: 3 }
|
|
14
|
+
* const doubled = mapValues(prices, (price) => price * 2)
|
|
15
|
+
* // Result: { apple: 2, banana: 4, orange: 6 }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const users = { john: 25, jane: 30, bob: 35 }
|
|
21
|
+
* const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)
|
|
22
|
+
* // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const data = { a: '1', b: '2', c: '3' }
|
|
28
|
+
* const numbers = mapValues(data, (str) => parseInt(str, 10))
|
|
29
|
+
* // Result: { a: 1, b: 2, c: 3 }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export function mapValues<ValueIn, ValueOut>(
|
|
35
|
+
object: Record<string, ValueIn>,
|
|
36
|
+
callback: (value: ValueIn, key: string) => ValueOut,
|
|
37
|
+
): Record<string, ValueOut> {
|
|
38
|
+
let result = {} as Record<string, ValueOut>
|
|
39
|
+
for (const [key, value] of Object.entries(object)) {
|
|
40
|
+
result[key] = callback(value, key)
|
|
41
|
+
}
|
|
42
|
+
return result
|
|
43
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import { objectEntries } from './object-entries'
|
|
6
|
+
|
|
7
|
+
describe('objectEntries', () => {
|
|
8
|
+
it('returns entries for an empty object', () => {
|
|
9
|
+
const obj = {}
|
|
10
|
+
const entries = objectEntries(obj)
|
|
11
|
+
expect(entries).toEqual([])
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns entries for objects with mixed types', () => {
|
|
15
|
+
const obj = { name: 'Alice', age: 30, active: true }
|
|
16
|
+
const entries = objectEntries(obj)
|
|
17
|
+
expect(entries).toEqual([
|
|
18
|
+
['name', 'Alice'],
|
|
19
|
+
['age', 30],
|
|
20
|
+
['active', true],
|
|
21
|
+
])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('preserves type safety with const objects', () => {
|
|
25
|
+
const obj = { a: 1, b: 'hello', c: true } as const
|
|
26
|
+
const entries = objectEntries(obj)
|
|
27
|
+
|
|
28
|
+
expect(entries).toHaveLength(3)
|
|
29
|
+
expect(entries).toContainEqual(['a', 1])
|
|
30
|
+
expect(entries).toContainEqual(['b', 'hello'])
|
|
31
|
+
expect(entries).toContainEqual(['c', true])
|
|
32
|
+
})
|
|
33
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A TypeScript utility type that represents the entries of an object as a union of tuple types.
|
|
3
|
+
* Each tuple contains a key-value pair where the key and value types are precisely typed
|
|
4
|
+
* according to the input object type.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* type MyObject = { a: 1; b: 'B' }
|
|
9
|
+
* type MyEntries = ObjectEntries<MyObject>
|
|
10
|
+
* // ^ ["a", 1] | ["b", "B"]
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export type ObjectEntries<T extends Record<string, any>> = {
|
|
16
|
+
[K in keyof T]: [K, T[K]]
|
|
17
|
+
}[keyof T]
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys
|
|
21
|
+
* and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this
|
|
22
|
+
* function returns an array of tuples where each tuple is precisely typed according to the
|
|
23
|
+
* input object's structure.
|
|
24
|
+
*
|
|
25
|
+
* This is particularly useful when working with objects that have known, fixed property types
|
|
26
|
+
* and you want to maintain type safety when iterating over entries.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const myObject = { a: 1, b: 'hello', c: true } as const
|
|
31
|
+
* const entries = objectEntries(myObject)
|
|
32
|
+
* // Type: (["a", 1] | ["b", "hello"] | ["c", true])[]
|
|
33
|
+
*
|
|
34
|
+
* for (const [key, value] of entries) {
|
|
35
|
+
* // key is typed as "a" | "b" | "c"
|
|
36
|
+
* // value is typed as 1 | "hello" | true
|
|
37
|
+
* console.log(`${key}: ${value}`)
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* interface User {
|
|
44
|
+
* name: string
|
|
45
|
+
* age: number
|
|
46
|
+
* active: boolean
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* const user: User = { name: 'Alice', age: 30, active: true }
|
|
50
|
+
* const entries = objectEntries(user)
|
|
51
|
+
* // Type: (["name", string] | ["age", number] | ["active", boolean])[]
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export const objectEntries: <T extends Record<string, any>>(
|
|
57
|
+
obj: T,
|
|
58
|
+
) => ObjectEntries<T>[] = Object.entries
|