@ocavue/utils 1.1.0 → 1.3.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 +139 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +85 -229
- package/dist/index.js.map +1 -1
- package/package.json +16 -13
- package/src/__snapshots__/e2e.test.ts.snap +247 -0
- package/src/default-map.test.ts +290 -1
- package/src/default-map.ts +76 -0
- package/src/e2e.test.ts +36 -0
- package/src/get-id.test.ts +20 -1
- package/src/get-id.ts +15 -1
- package/src/index.ts +3 -1
- package/src/map-group-by.ts +3 -1
- 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/src/object-group-by.ts +3 -1
- package/src/once-stub-1.ts +13 -0
- package/src/once-stub-2.ts +3 -0
- package/src/once.test.ts +29 -0
package/dist/index.d.ts
CHANGED
|
@@ -18,10 +18,52 @@ 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
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A weak map that automatically creates values for missing keys using a factory function.
|
|
28
|
+
*
|
|
29
|
+
* Similar to DefaultMap but uses WeakMap as the base, allowing garbage collection of keys.
|
|
30
|
+
*/
|
|
31
|
+
declare class DefaultWeakMap<K$1 extends WeakKey, V> extends WeakMap<K$1, V> {
|
|
32
|
+
private readonly defaultFactory;
|
|
33
|
+
constructor(defaultFactory: () => V, entries?: readonly (readonly [K$1, V])[] | null);
|
|
34
|
+
get(key: K$1): V;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A map that counts occurrences of keys.
|
|
38
|
+
*
|
|
39
|
+
* Similar to Python's [Counter](https://docs.python.org/3.13/library/collections.html#collections.Counter).
|
|
40
|
+
*/
|
|
41
|
+
declare class Counter<K$1> extends DefaultMap<K$1, number> {
|
|
42
|
+
constructor(iterable?: Iterable<readonly [K$1, number]>);
|
|
43
|
+
/**
|
|
44
|
+
* Increments the count for a key by a given amount (default 1).
|
|
45
|
+
*/
|
|
46
|
+
increment(key: K$1, amount?: number): void;
|
|
47
|
+
/**
|
|
48
|
+
* Decrements the count for a key by a given amount (default 1).
|
|
49
|
+
*/
|
|
50
|
+
decrement(key: K$1, amount?: number): void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A weak map that counts occurrences of object keys.
|
|
54
|
+
*
|
|
55
|
+
* Similar to Counter but uses WeakMap as the base, allowing garbage collection of keys.
|
|
56
|
+
*/
|
|
57
|
+
declare class WeakCounter<K$1 extends WeakKey> extends DefaultWeakMap<K$1, number> {
|
|
58
|
+
constructor(entries?: readonly (readonly [K$1, number])[] | null);
|
|
59
|
+
/**
|
|
60
|
+
* Increments the count for a key by a given amount (default 1).
|
|
61
|
+
*/
|
|
62
|
+
increment(key: K$1, amount?: number): void;
|
|
63
|
+
/**
|
|
64
|
+
* Decrements the count for a key by a given amount (default 1).
|
|
65
|
+
*/
|
|
66
|
+
decrement(key: K$1, amount?: number): void;
|
|
25
67
|
}
|
|
26
68
|
//#endregion
|
|
27
69
|
//#region src/dom.d.ts
|
|
@@ -110,7 +152,98 @@ declare function isDeepEqual(a: unknown, b: unknown): boolean;
|
|
|
110
152
|
*
|
|
111
153
|
* @public
|
|
112
154
|
*/
|
|
113
|
-
declare const mapGroupBy: <K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Map<K, T[]>;
|
|
155
|
+
declare const mapGroupBy: <K$1, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K$1) => Map<K$1, T[]>;
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/map-values.d.ts
|
|
158
|
+
/**
|
|
159
|
+
* Creates a new object with the same keys as the input object, but with values
|
|
160
|
+
* transformed by the provided callback function. Similar to `Array.prototype.map()`
|
|
161
|
+
* but for object values.
|
|
162
|
+
|
|
163
|
+
* @param object - The object whose values will be transformed.
|
|
164
|
+
* @param callback - A function that transforms each value. Receives the value and
|
|
165
|
+
* its corresponding key as arguments.
|
|
166
|
+
* @returns A new object with the same keys but transformed values.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const prices = { apple: 1, banana: 2, orange: 3 }
|
|
171
|
+
* const doubled = mapValues(prices, (price) => price * 2)
|
|
172
|
+
* // Result: { apple: 2, banana: 4, orange: 6 }
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const users = { john: 25, jane: 30, bob: 35 }
|
|
178
|
+
* const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)
|
|
179
|
+
* // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const data = { a: '1', b: '2', c: '3' }
|
|
185
|
+
* const numbers = mapValues(data, (str) => parseInt(str, 10))
|
|
186
|
+
* // Result: { a: 1, b: 2, c: 3 }
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @public
|
|
190
|
+
*/
|
|
191
|
+
declare function mapValues<ValueIn, ValueOut>(object: Record<string, ValueIn>, callback: (value: ValueIn, key: string) => ValueOut): Record<string, ValueOut>;
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/object-entries.d.ts
|
|
194
|
+
/**
|
|
195
|
+
* A TypeScript utility type that represents the entries of an object as a union of tuple types.
|
|
196
|
+
* Each tuple contains a key-value pair where the key and value types are precisely typed
|
|
197
|
+
* according to the input object type.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* type MyObject = { a: 1; b: 'B' }
|
|
202
|
+
* type MyEntries = ObjectEntries<MyObject>
|
|
203
|
+
* // ^ ["a", 1] | ["b", "B"]
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @public
|
|
207
|
+
*/
|
|
208
|
+
type ObjectEntries<T extends Record<string, any>> = { [K in keyof T]: [K, T[K]] }[keyof T];
|
|
209
|
+
/**
|
|
210
|
+
* A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys
|
|
211
|
+
* and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this
|
|
212
|
+
* function returns an array of tuples where each tuple is precisely typed according to the
|
|
213
|
+
* input object's structure.
|
|
214
|
+
*
|
|
215
|
+
* This is particularly useful when working with objects that have known, fixed property types
|
|
216
|
+
* and you want to maintain type safety when iterating over entries.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* const myObject = { a: 1, b: 'hello', c: true } as const
|
|
221
|
+
* const entries = objectEntries(myObject)
|
|
222
|
+
* // Type: (["a", 1] | ["b", "hello"] | ["c", true])[]
|
|
223
|
+
*
|
|
224
|
+
* for (const [key, value] of entries) {
|
|
225
|
+
* // key is typed as "a" | "b" | "c"
|
|
226
|
+
* // value is typed as 1 | "hello" | true
|
|
227
|
+
* console.log(`${key}: ${value}`)
|
|
228
|
+
* }
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* interface User {
|
|
234
|
+
* name: string
|
|
235
|
+
* age: number
|
|
236
|
+
* active: boolean
|
|
237
|
+
* }
|
|
238
|
+
*
|
|
239
|
+
* const user: User = { name: 'Alice', age: 30, active: true }
|
|
240
|
+
* const entries = objectEntries(user)
|
|
241
|
+
* // Type: (["name", string] | ["age", number] | ["active", boolean])[]
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* @public
|
|
245
|
+
*/
|
|
246
|
+
declare const objectEntries: <T extends Record<string, any>>(obj: T) => ObjectEntries<T>[];
|
|
114
247
|
//#endregion
|
|
115
248
|
//#region src/object-group-by.d.ts
|
|
116
249
|
/**
|
|
@@ -118,7 +251,7 @@ declare const mapGroupBy: <K, T>(items: Iterable<T>, keySelector: (item: T, inde
|
|
|
118
251
|
*
|
|
119
252
|
* @public
|
|
120
253
|
*/
|
|
121
|
-
declare const objectGroupBy: <K extends PropertyKey, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Partial<Record<K, T[]>>;
|
|
254
|
+
declare const objectGroupBy: <K$1 extends PropertyKey, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K$1) => Partial<Record<K$1, T[]>>;
|
|
122
255
|
//#endregion
|
|
123
256
|
//#region src/once.d.ts
|
|
124
257
|
/**
|
|
@@ -142,5 +275,5 @@ declare function once<T>(fn: () => T): () => T;
|
|
|
142
275
|
*/
|
|
143
276
|
declare function sleep(ms: number): Promise<void>;
|
|
144
277
|
//#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 };
|
|
278
|
+
export { Counter, DefaultMap, DefaultWeakMap, type ObjectEntries, WeakCounter, 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
279
|
//# 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,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CAAA,GAAA,QAAI,MOEV,CPFU,GAAA,COEL,CPFK,EOEF,CPFE,COEA,CPFA,CAAA,CAAA,EARc,CAAA,MOW9B,CPX8B,CAAA;;AAuBtC;;;;;;;;;;;AA0BA;;;;;;;;AAyBA;;;;;;;;;;;ACzEA;AAOA;AAOA;AAOA;AAOA;AAUA;AAOgB,cMIH,aNJqB,EAAA,CAAO,UMIF,MNJiB,CAAA,MAAgB,EAAA,GAAA,CAAA,CAAA,CAAA,GAAA,EMKjE,CNLiE,EAAA,GMMnE,aNNmE,CMMrD,CNNqD,CAAA,EAAA;;;;;;;;AD3CtB,cQ+BrC,aR/BqC,EAAA,CAAA,YQ+BX,WR/BW,EAAA,CAAA,CAAA,CAAA,KAAA,EQgCzC,QRhCyC,CQgChC,CRhCgC,CAAA,EAAA,WAAA,EAAA,CAAA,IAAA,EQiC5B,CRjC4B,EAAA,KAAA,EAAA,MAAA,EAAA,GQiCP,GRjCO,EAAA,GQkC7C,ORlC6C,CQkCrC,MRlCqC,CQkC9B,GRlC8B,EQkC3B,CRlC2B,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
|
@@ -1,122 +1,84 @@
|
|
|
1
|
-
//#region src/checker.ts
|
|
2
|
-
/**
|
|
3
|
-
* Checks if the given value is an object.
|
|
4
|
-
*/
|
|
5
1
|
function isObject(value) {
|
|
6
|
-
return
|
|
2
|
+
return typeof value == "object" && !!value;
|
|
7
3
|
}
|
|
8
|
-
/**
|
|
9
|
-
* Checks if the given value is a Map.
|
|
10
|
-
*/
|
|
11
4
|
function isMap(value) {
|
|
12
5
|
return value instanceof Map;
|
|
13
6
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Checks if the given value is a Set.
|
|
16
|
-
*/
|
|
17
7
|
function isSet(value) {
|
|
18
8
|
return value instanceof Set;
|
|
19
9
|
}
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/default-map.ts
|
|
23
|
-
/**
|
|
24
|
-
* A map that automatically creates values for missing keys using a factory function.
|
|
25
|
-
*
|
|
26
|
-
* Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).
|
|
27
|
-
*/
|
|
28
10
|
var DefaultMap = class extends Map {
|
|
29
11
|
constructor(defaultFactory, iterable) {
|
|
30
|
-
super(iterable);
|
|
31
|
-
this.defaultFactory = defaultFactory;
|
|
12
|
+
super(iterable), this.defaultFactory = defaultFactory;
|
|
32
13
|
}
|
|
33
14
|
get(key) {
|
|
34
15
|
if (this.has(key)) return super.get(key);
|
|
35
|
-
|
|
36
|
-
this.set(key, value);
|
|
37
|
-
|
|
16
|
+
let value = this.defaultFactory();
|
|
17
|
+
return this.set(key, value), value;
|
|
18
|
+
}
|
|
19
|
+
}, DefaultWeakMap = class extends WeakMap {
|
|
20
|
+
constructor(defaultFactory, entries) {
|
|
21
|
+
super(entries), this.defaultFactory = defaultFactory;
|
|
22
|
+
}
|
|
23
|
+
get(key) {
|
|
24
|
+
if (this.has(key)) return super.get(key);
|
|
25
|
+
let value = this.defaultFactory();
|
|
26
|
+
return this.set(key, value), value;
|
|
27
|
+
}
|
|
28
|
+
}, Counter = class extends DefaultMap {
|
|
29
|
+
constructor(iterable) {
|
|
30
|
+
super(() => 0, iterable);
|
|
31
|
+
}
|
|
32
|
+
increment(key, amount = 1) {
|
|
33
|
+
this.set(key, this.get(key) + amount);
|
|
34
|
+
}
|
|
35
|
+
decrement(key, amount = 1) {
|
|
36
|
+
this.set(key, this.get(key) - amount);
|
|
37
|
+
}
|
|
38
|
+
}, WeakCounter = class extends DefaultWeakMap {
|
|
39
|
+
constructor(entries) {
|
|
40
|
+
super(() => 0, entries);
|
|
41
|
+
}
|
|
42
|
+
increment(key, amount = 1) {
|
|
43
|
+
this.set(key, this.get(key) + amount);
|
|
44
|
+
}
|
|
45
|
+
decrement(key, amount = 1) {
|
|
46
|
+
this.set(key, this.get(key) - amount);
|
|
38
47
|
}
|
|
39
48
|
};
|
|
40
|
-
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region src/dom-node-type.ts
|
|
43
|
-
const ELEMENT_NODE = 1;
|
|
44
|
-
const TEXT_NODE = 3;
|
|
45
|
-
const DOCUMENT_NODE = 9;
|
|
46
|
-
const DOCUMENT_FRAGMENT_NODE = 11;
|
|
47
|
-
|
|
48
|
-
//#endregion
|
|
49
|
-
//#region src/dom.ts
|
|
50
|
-
/**
|
|
51
|
-
* Checks if the given DOM node is an Element.
|
|
52
|
-
*/
|
|
53
49
|
function isElement(node) {
|
|
54
|
-
return node.nodeType ===
|
|
50
|
+
return node.nodeType === 1;
|
|
55
51
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Checks if the given DOM node is a Text node.
|
|
58
|
-
*/
|
|
59
52
|
function isTextNode(node) {
|
|
60
|
-
return node.nodeType ===
|
|
53
|
+
return node.nodeType === 3;
|
|
61
54
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Checks if the given DOM node is an HTMLElement.
|
|
64
|
-
*/
|
|
65
55
|
function isHTMLElement(node) {
|
|
66
56
|
return isElement(node) && node.namespaceURI === "http://www.w3.org/1999/xhtml";
|
|
67
57
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Checks if the given DOM node is an SVGElement.
|
|
70
|
-
*/
|
|
71
58
|
function isSVGElement(node) {
|
|
72
59
|
return isElement(node) && node.namespaceURI === "http://www.w3.org/2000/svg";
|
|
73
60
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Checks if the given DOM node is an MathMLElement.
|
|
76
|
-
*/
|
|
77
61
|
function isMathMLElement(node) {
|
|
78
62
|
return isElement(node) && node.namespaceURI === "http://www.w3.org/1998/Math/MathML";
|
|
79
63
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Checks if the given DOM node is a Document.
|
|
82
|
-
*/
|
|
83
64
|
function isDocument(node) {
|
|
84
|
-
return node.nodeType ===
|
|
65
|
+
return node.nodeType === 9;
|
|
85
66
|
}
|
|
86
|
-
/**
|
|
87
|
-
* Checks if the given DOM node is a DocumentFragment.
|
|
88
|
-
*/
|
|
89
67
|
function isDocumentFragment(node) {
|
|
90
|
-
return node.nodeType ===
|
|
68
|
+
return node.nodeType === 11;
|
|
91
69
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Checks if the given DOM node is a ShadowRoot.
|
|
94
|
-
*/
|
|
95
70
|
function isShadowRoot(node) {
|
|
96
71
|
return isDocumentFragment(node) && "host" in node && isElementLike(node.host);
|
|
97
72
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Checks if an unknown value is likely a DOM node.
|
|
100
|
-
*/
|
|
101
73
|
function isNodeLike(value) {
|
|
102
74
|
return isObject(value) && value.nodeType !== void 0;
|
|
103
75
|
}
|
|
104
|
-
/**
|
|
105
|
-
* Checks if an unknown value is likely a DOM element.
|
|
106
|
-
*/
|
|
107
76
|
function isElementLike(value) {
|
|
108
|
-
return isObject(value) && value.nodeType ===
|
|
77
|
+
return isObject(value) && value.nodeType === 1 && typeof value.nodeName == "string";
|
|
109
78
|
}
|
|
110
|
-
/**
|
|
111
|
-
* Checks if the given value is likely a Window object.
|
|
112
|
-
*/
|
|
113
79
|
function isWindowLike(value) {
|
|
114
80
|
return isObject(value) && value.window === value;
|
|
115
81
|
}
|
|
116
|
-
/**
|
|
117
|
-
* Gets the window object for the given target or the global window object if no
|
|
118
|
-
* target is provided.
|
|
119
|
-
*/
|
|
120
82
|
function getWindow(target) {
|
|
121
83
|
if (target) {
|
|
122
84
|
if (isShadowRoot(target)) return getWindow(target.host);
|
|
@@ -125,200 +87,94 @@ function getWindow(target) {
|
|
|
125
87
|
}
|
|
126
88
|
return window;
|
|
127
89
|
}
|
|
128
|
-
/**
|
|
129
|
-
* Gets the document for the given target or the global document if no target is
|
|
130
|
-
* provided.
|
|
131
|
-
*/
|
|
132
90
|
function getDocument(target) {
|
|
133
|
-
|
|
134
|
-
if (isWindowLike(target)) return target.document;
|
|
135
|
-
if (isDocument(target)) return target;
|
|
136
|
-
return target.ownerDocument || document;
|
|
137
|
-
}
|
|
138
|
-
return document;
|
|
91
|
+
return target ? isWindowLike(target) ? target.document : isDocument(target) ? target : target.ownerDocument || document : document;
|
|
139
92
|
}
|
|
140
|
-
/**
|
|
141
|
-
* Gets a reference to the root node of the document based on the given target.
|
|
142
|
-
*/
|
|
143
93
|
function getDocumentElement(target) {
|
|
144
94
|
return getDocument(target).documentElement;
|
|
145
95
|
}
|
|
146
|
-
|
|
147
|
-
//#endregion
|
|
148
|
-
//#region src/format-bytes.ts
|
|
149
|
-
/**
|
|
150
|
-
* Formats a number of bytes into a human-readable string.
|
|
151
|
-
* @param bytes - The number of bytes to format.
|
|
152
|
-
* @returns A string representing the number of bytes in a human-readable format.
|
|
153
|
-
*/
|
|
154
96
|
function formatBytes(bytes) {
|
|
155
|
-
|
|
97
|
+
let units = [
|
|
156
98
|
"B",
|
|
157
99
|
"KB",
|
|
158
100
|
"MB",
|
|
159
101
|
"GB"
|
|
160
|
-
];
|
|
161
|
-
|
|
162
|
-
let
|
|
163
|
-
while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {
|
|
164
|
-
num /= 1024;
|
|
165
|
-
unitIndex++;
|
|
166
|
-
}
|
|
167
|
-
const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1;
|
|
102
|
+
], unitIndex = 0, num = bytes;
|
|
103
|
+
for (; Math.abs(num) >= 1024 && unitIndex < units.length - 1;) num /= 1024, unitIndex++;
|
|
104
|
+
let fraction = unitIndex === 0 && num % 1 == 0 ? 0 : 1;
|
|
168
105
|
return `${num.toFixed(fraction)}${units[unitIndex]}`;
|
|
169
106
|
}
|
|
170
|
-
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region src/get-id.ts
|
|
173
|
-
let id = 0;
|
|
174
|
-
/**
|
|
175
|
-
* Generates a unique positive integer.
|
|
176
|
-
*/
|
|
107
|
+
let id = 0, maxSafeInteger = 2 ** 53 - 1;
|
|
177
108
|
function getId() {
|
|
178
|
-
id
|
|
179
|
-
return id;
|
|
109
|
+
return id++, id >= maxSafeInteger && (id = 1), id;
|
|
180
110
|
}
|
|
181
|
-
|
|
182
|
-
//#endregion
|
|
183
|
-
//#region src/is-deep-equal.ts
|
|
184
|
-
/**
|
|
185
|
-
* Whether two values are deeply equal.
|
|
186
|
-
*/
|
|
187
111
|
function isDeepEqual(a, b) {
|
|
188
|
-
if (a === b) return
|
|
189
|
-
if (a == null || b == null) return
|
|
190
|
-
|
|
191
|
-
if (aType !== typeof b) return
|
|
192
|
-
if (aType === "number" && Number.isNaN(a) && Number.isNaN(b)) return
|
|
112
|
+
if (a === b) return !0;
|
|
113
|
+
if (a == null || b == null) return !1;
|
|
114
|
+
let aType = typeof a;
|
|
115
|
+
if (aType !== typeof b) return !1;
|
|
116
|
+
if (aType === "number" && Number.isNaN(a) && Number.isNaN(b)) return !0;
|
|
193
117
|
if (Array.isArray(a)) {
|
|
194
|
-
if (!Array.isArray(b)) return
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return true;
|
|
118
|
+
if (!Array.isArray(b) || a.length !== b.length) return !1;
|
|
119
|
+
let size = a.length;
|
|
120
|
+
for (let i = 0; i < size; i++) if (!isDeepEqual(a[i], b[i])) return !1;
|
|
121
|
+
return !0;
|
|
199
122
|
}
|
|
200
123
|
if (isSet(a)) {
|
|
201
|
-
if (!isSet(b)) return
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return true;
|
|
124
|
+
if (!isSet(b) || a.size !== b.size) return !1;
|
|
125
|
+
for (let value of a) if (!b.has(value)) return !1;
|
|
126
|
+
return !0;
|
|
205
127
|
}
|
|
206
128
|
if (isMap(a)) {
|
|
207
|
-
if (!isMap(b)) return
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (!b.has(key)) return false;
|
|
211
|
-
if (!isDeepEqual(a.get(key), b.get(key))) return false;
|
|
212
|
-
}
|
|
213
|
-
return true;
|
|
129
|
+
if (!isMap(b) || a.size !== b.size) return !1;
|
|
130
|
+
for (let key of a.keys()) if (!b.has(key) || !isDeepEqual(a.get(key), b.get(key))) return !1;
|
|
131
|
+
return !0;
|
|
214
132
|
}
|
|
215
|
-
if (typeof a
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return true;
|
|
133
|
+
if (typeof a == "object" && typeof b == "object") {
|
|
134
|
+
let aKeys = Object.keys(a), bKeys = Object.keys(b);
|
|
135
|
+
if (aKeys.length !== bKeys.length) return !1;
|
|
136
|
+
for (let key of aKeys) if (!isDeepEqual(a[key], b[key])) return !1;
|
|
137
|
+
return !0;
|
|
221
138
|
}
|
|
222
|
-
return
|
|
139
|
+
return !1;
|
|
223
140
|
}
|
|
224
|
-
|
|
225
|
-
//#endregion
|
|
226
|
-
//#region src/map-group-by.ts
|
|
227
|
-
/**
|
|
228
|
-
* @internal
|
|
229
|
-
*/
|
|
230
141
|
function mapGroupByPolyfill(items, keySelector) {
|
|
231
|
-
|
|
232
|
-
let
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const group = map.get(key);
|
|
236
|
-
if (group) group.push(item);
|
|
237
|
-
else map.set(key, [item]);
|
|
238
|
-
index++;
|
|
142
|
+
let map = /* @__PURE__ */ new Map(), index = 0;
|
|
143
|
+
for (let item of items) {
|
|
144
|
+
let key = keySelector(item, index), group = map.get(key);
|
|
145
|
+
group ? group.push(item) : map.set(key, [item]), index++;
|
|
239
146
|
}
|
|
240
147
|
return map;
|
|
241
148
|
}
|
|
242
|
-
/**
|
|
243
|
-
* @internal
|
|
244
|
-
*/
|
|
245
149
|
function mapGroupByNative(items, keySelector) {
|
|
246
150
|
return Map.groupBy(items, keySelector);
|
|
247
151
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
//#endregion
|
|
256
|
-
//#region src/object-group-by.ts
|
|
257
|
-
/**
|
|
258
|
-
* @internal
|
|
259
|
-
*/
|
|
152
|
+
const mapGroupBy = /* @__PURE__ */ (() => !!Map.groupBy)() ? mapGroupByNative : mapGroupByPolyfill;
|
|
153
|
+
function mapValues(object, callback) {
|
|
154
|
+
let result = {};
|
|
155
|
+
for (let [key, value] of Object.entries(object)) result[key] = callback(value, key);
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
const objectEntries = Object.entries;
|
|
260
159
|
function objectGroupByPolyfill(items, keySelector) {
|
|
261
|
-
|
|
262
|
-
let
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
const group = result[key];
|
|
266
|
-
if (group) group.push(item);
|
|
267
|
-
else result[key] = [item];
|
|
268
|
-
index++;
|
|
160
|
+
let result = {}, index = 0;
|
|
161
|
+
for (let item of items) {
|
|
162
|
+
let key = keySelector(item, index), group = result[key];
|
|
163
|
+
group ? group.push(item) : result[key] = [item], index++;
|
|
269
164
|
}
|
|
270
165
|
return result;
|
|
271
166
|
}
|
|
272
|
-
/**
|
|
273
|
-
* @internal
|
|
274
|
-
*/
|
|
275
167
|
function objectGroupByNative(items, keySelector) {
|
|
276
168
|
return Object.groupBy(items, keySelector);
|
|
277
169
|
}
|
|
278
|
-
|
|
279
|
-
* A polyfill for the `Object.groupBy` static method.
|
|
280
|
-
*
|
|
281
|
-
* @public
|
|
282
|
-
*/
|
|
283
|
-
const objectGroupBy = !!Object.groupBy ? objectGroupByNative : objectGroupByPolyfill;
|
|
284
|
-
|
|
285
|
-
//#endregion
|
|
286
|
-
//#region src/once.ts
|
|
287
|
-
/**
|
|
288
|
-
* Creates a function that will only execute the provided function once.
|
|
289
|
-
* Subsequent calls will return the cached result from the first execution.
|
|
290
|
-
*
|
|
291
|
-
* @param fn The function to execute once
|
|
292
|
-
* @returns A function that will only execute the provided function once
|
|
293
|
-
* @example
|
|
294
|
-
* ```ts
|
|
295
|
-
* const getValue = once(() => expensiveOperation())
|
|
296
|
-
* getValue() // executes expensiveOperation
|
|
297
|
-
* getValue() // returns cached result
|
|
298
|
-
* ```
|
|
299
|
-
*/
|
|
170
|
+
const objectGroupBy = /* @__PURE__ */ (() => !!Object.groupBy)() ? objectGroupByNative : objectGroupByPolyfill;
|
|
300
171
|
function once(fn) {
|
|
301
|
-
let called =
|
|
302
|
-
|
|
303
|
-
return () => {
|
|
304
|
-
if (!called) {
|
|
305
|
-
result = fn();
|
|
306
|
-
called = true;
|
|
307
|
-
fn = void 0;
|
|
308
|
-
}
|
|
309
|
-
return result;
|
|
310
|
-
};
|
|
172
|
+
let called = !1, result;
|
|
173
|
+
return () => (called || (result = fn(), called = !0, fn = void 0), result);
|
|
311
174
|
}
|
|
312
|
-
|
|
313
|
-
//#endregion
|
|
314
|
-
//#region src/sleep.ts
|
|
315
|
-
/**
|
|
316
|
-
* Sleep for a given number of milliseconds.
|
|
317
|
-
*/
|
|
318
175
|
function sleep(ms) {
|
|
319
176
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
320
177
|
}
|
|
178
|
+
export { Counter, DefaultMap, DefaultWeakMap, WeakCounter, 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 };
|
|
321
179
|
|
|
322
|
-
//#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 };
|
|
324
180
|
//# sourceMappingURL=index.js.map
|