@byloth/core 2.2.4 → 2.2.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/dist/core.cjs +3 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +765 -5064
- package/dist/core.esm.js.map +1 -1
- package/dist/core.global.js +3 -2
- package/dist/core.global.js.map +1 -1
- package/dist/core.umd.cjs +3 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +10 -10
- package/src/index.ts +2 -1
- package/src/models/aggregators/aggregated-async-iterator.ts +5 -5
- package/src/models/aggregators/aggregated-iterator.ts +12 -12
- package/src/models/aggregators/reduced-iterator.ts +7 -7
- package/src/models/callbacks/callback-chain.ts +3 -3
- package/src/models/callbacks/publisher.ts +1 -1
- package/src/models/collections/array-view.ts +2 -2
- package/src/models/collections/map-view.ts +2 -2
- package/src/models/collections/set-view.ts +3 -3
- package/src/models/collections/types.ts +4 -4
- package/src/models/iterators/smart-async-iterator.ts +1 -1
- package/src/models/iterators/smart-iterator.ts +1 -1
- package/src/models/promises/smart-promise.ts +10 -10
- package/src/models/promises/timed-promise.ts +1 -1
- package/src/utils/index.ts +1 -1
- package/src/utils/iterator.ts +2 -2
- package/src/utils/math.ts +35 -2
- package/src/utils/random.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.6",
|
|
4
4
|
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Core",
|
|
@@ -49,16 +49,16 @@
|
|
|
49
49
|
},
|
|
50
50
|
"types": "src/index.ts",
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@byloth/eslint-config-typescript": "^
|
|
53
|
-
"@eslint/compat": "^2.0.
|
|
54
|
-
"@types/node": "^22.19.
|
|
55
|
-
"@vitest/coverage-v8": "^4.
|
|
56
|
-
"eslint": "^
|
|
52
|
+
"@byloth/eslint-config-typescript": "^4.0.0",
|
|
53
|
+
"@eslint/compat": "^2.0.3",
|
|
54
|
+
"@types/node": "^22.19.15",
|
|
55
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
56
|
+
"eslint": "^10.1.0",
|
|
57
57
|
"husky": "^9.1.7",
|
|
58
|
-
"jsdom": "^
|
|
59
|
-
"typescript": "^
|
|
60
|
-
"vite": "^
|
|
61
|
-
"vitest": "^4.
|
|
58
|
+
"jsdom": "^29.0.1",
|
|
59
|
+
"typescript": "^6.0.2",
|
|
60
|
+
"vite": "^8.0.3",
|
|
61
|
+
"vitest": "^4.1.2"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"dev": "vite",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const VERSION = "2.2.
|
|
1
|
+
export const VERSION = "2.2.6";
|
|
2
2
|
|
|
3
3
|
export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
|
|
4
4
|
|
|
@@ -87,6 +87,7 @@ export type {
|
|
|
87
87
|
|
|
88
88
|
export {
|
|
89
89
|
average,
|
|
90
|
+
clamp,
|
|
90
91
|
capitalize,
|
|
91
92
|
chain,
|
|
92
93
|
count,
|
|
@@ -253,7 +253,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
253
253
|
values.set(key, [index + 1, await predicate(key, element, index)]);
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
return new ReducedIterator(function* ()
|
|
256
|
+
return new ReducedIterator(function* (): Generator<[K, boolean]>
|
|
257
257
|
{
|
|
258
258
|
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
259
259
|
});
|
|
@@ -303,7 +303,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
303
303
|
values.set(key, [index + 1, await predicate(key, element, index)]);
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
-
return new ReducedIterator(function* ()
|
|
306
|
+
return new ReducedIterator(function* (): Generator<[K, boolean]>
|
|
307
307
|
{
|
|
308
308
|
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
309
309
|
});
|
|
@@ -578,7 +578,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
578
578
|
values.set(key, [index + 1, await reducer(key, accumulator, element, index)]);
|
|
579
579
|
}
|
|
580
580
|
|
|
581
|
-
return new ReducedIterator(function* ()
|
|
581
|
+
return new ReducedIterator(function* (): Generator<[K, A]>
|
|
582
582
|
{
|
|
583
583
|
for (const [key, [_, accumulator]] of values) { yield [key, accumulator]; }
|
|
584
584
|
});
|
|
@@ -829,7 +829,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
829
829
|
values.set(key, [index + 1, finding]);
|
|
830
830
|
}
|
|
831
831
|
|
|
832
|
-
return new ReducedIterator(function* ()
|
|
832
|
+
return new ReducedIterator(function* (): Generator<[K, T | undefined]>
|
|
833
833
|
{
|
|
834
834
|
for (const [key, [_, finding]] of values) { yield [key, finding]; }
|
|
835
835
|
});
|
|
@@ -945,7 +945,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
945
945
|
counters.set(key, count + 1);
|
|
946
946
|
}
|
|
947
947
|
|
|
948
|
-
return new ReducedIterator(function* ()
|
|
948
|
+
return new ReducedIterator(function* (): Generator<[K, number]>
|
|
949
949
|
{
|
|
950
950
|
for (const [key, count] of counters) { yield [key, count]; }
|
|
951
951
|
});
|
|
@@ -175,7 +175,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
175
175
|
values.set(key, [index + 1, predicate(key, element, index)]);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
return new ReducedIterator(function* ()
|
|
178
|
+
return new ReducedIterator(function* (): Generator<[K, boolean]>
|
|
179
179
|
{
|
|
180
180
|
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
181
181
|
});
|
|
@@ -224,7 +224,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
224
224
|
values.set(key, [index + 1, predicate(key, element, index)]);
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
return new ReducedIterator(function* ()
|
|
227
|
+
return new ReducedIterator(function* (): Generator<[K, boolean]>
|
|
228
228
|
{
|
|
229
229
|
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
230
230
|
});
|
|
@@ -303,7 +303,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
303
303
|
{
|
|
304
304
|
const elements = this._elements;
|
|
305
305
|
|
|
306
|
-
return new AggregatedIterator(function* ()
|
|
306
|
+
return new AggregatedIterator(function* (): Generator<[K, T]>
|
|
307
307
|
{
|
|
308
308
|
const indexes = new Map<K, number>();
|
|
309
309
|
for (const [key, element] of elements)
|
|
@@ -352,7 +352,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
352
352
|
{
|
|
353
353
|
const elements = this._elements;
|
|
354
354
|
|
|
355
|
-
return new AggregatedIterator(function* ()
|
|
355
|
+
return new AggregatedIterator(function* (): Generator<[K, V]>
|
|
356
356
|
{
|
|
357
357
|
const indexes = new Map<K, number>();
|
|
358
358
|
for (const [key, element] of elements)
|
|
@@ -496,7 +496,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
496
496
|
values.set(key, [index + 1, reducer(key, accumulator, element, index)]);
|
|
497
497
|
}
|
|
498
498
|
|
|
499
|
-
return new ReducedIterator(function* ()
|
|
499
|
+
return new ReducedIterator(function* (): Generator<[K, A]>
|
|
500
500
|
{
|
|
501
501
|
for (const [key, [_, accumulator]] of values) { yield [key, accumulator]; }
|
|
502
502
|
});
|
|
@@ -542,7 +542,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
542
542
|
{
|
|
543
543
|
const elements = this._elements;
|
|
544
544
|
|
|
545
|
-
return new AggregatedIterator(function* ()
|
|
545
|
+
return new AggregatedIterator(function* (): Generator<[K, V]>
|
|
546
546
|
{
|
|
547
547
|
const indexes = new Map<K, number>();
|
|
548
548
|
for (const [key, element] of elements)
|
|
@@ -594,7 +594,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
594
594
|
{
|
|
595
595
|
const elements = this._elements;
|
|
596
596
|
|
|
597
|
-
return new AggregatedIterator(function* ()
|
|
597
|
+
return new AggregatedIterator(function* (): Generator<[K, T]>
|
|
598
598
|
{
|
|
599
599
|
const indexes = new Map<K, number>();
|
|
600
600
|
for (const [key, element] of elements)
|
|
@@ -645,7 +645,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
645
645
|
{
|
|
646
646
|
const elements = this._elements;
|
|
647
647
|
|
|
648
|
-
return new AggregatedIterator(function* ()
|
|
648
|
+
return new AggregatedIterator(function* (): Generator<[K, T]>
|
|
649
649
|
{
|
|
650
650
|
const indexes = new Map<K, number>();
|
|
651
651
|
for (const [key, element] of elements)
|
|
@@ -740,7 +740,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
740
740
|
values.set(key, [index + 1, finding]);
|
|
741
741
|
}
|
|
742
742
|
|
|
743
|
-
return new ReducedIterator(function* ()
|
|
743
|
+
return new ReducedIterator(function* (): Generator<[K, T | undefined]>
|
|
744
744
|
{
|
|
745
745
|
for (const [key, [_, finding]] of values) { yield [key, finding]; }
|
|
746
746
|
});
|
|
@@ -807,7 +807,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
807
807
|
{
|
|
808
808
|
const elements = this._elements;
|
|
809
809
|
|
|
810
|
-
return new AggregatedIterator(function* ()
|
|
810
|
+
return new AggregatedIterator(function* (): Generator<[K, T]>
|
|
811
811
|
{
|
|
812
812
|
const keys = new Map<K, Set<T>>();
|
|
813
813
|
for (const [key, element] of elements)
|
|
@@ -855,7 +855,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
855
855
|
counters.set(key, count + 1);
|
|
856
856
|
}
|
|
857
857
|
|
|
858
|
-
return new ReducedIterator(function* ()
|
|
858
|
+
return new ReducedIterator(function* (): Generator<[K, number]>
|
|
859
859
|
{
|
|
860
860
|
for (const [key, count] of counters) { yield [key, count]; }
|
|
861
861
|
});
|
|
@@ -932,7 +932,7 @@ export default class AggregatedIterator<K extends PropertyKey, T>
|
|
|
932
932
|
{
|
|
933
933
|
const elements = this._elements;
|
|
934
934
|
|
|
935
|
-
return new AggregatedIterator(function* ()
|
|
935
|
+
return new AggregatedIterator(function* (): Generator<[J, T]>
|
|
936
936
|
{
|
|
937
937
|
const indexes = new Map<K, number>();
|
|
938
938
|
for (const [key, element] of elements)
|
|
@@ -289,7 +289,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
289
289
|
{
|
|
290
290
|
const elements = this._elements.enumerate();
|
|
291
291
|
|
|
292
|
-
return new ReducedIterator(function* ()
|
|
292
|
+
return new ReducedIterator(function* (): Generator<[K, T]>
|
|
293
293
|
{
|
|
294
294
|
for (const [index, [key, element]] of elements)
|
|
295
295
|
{
|
|
@@ -335,7 +335,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
335
335
|
{
|
|
336
336
|
const elements = this._elements.enumerate();
|
|
337
337
|
|
|
338
|
-
return new ReducedIterator(function* ()
|
|
338
|
+
return new ReducedIterator(function* (): Generator<[K, V]>
|
|
339
339
|
{
|
|
340
340
|
for (const [index, [key, element]] of elements)
|
|
341
341
|
{
|
|
@@ -472,7 +472,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
472
472
|
{
|
|
473
473
|
const elements = this._elements.enumerate();
|
|
474
474
|
|
|
475
|
-
return new AggregatedIterator(function* ()
|
|
475
|
+
return new AggregatedIterator(function* (): Generator<[K, V]>
|
|
476
476
|
{
|
|
477
477
|
for (const [index, [key, element]] of elements)
|
|
478
478
|
{
|
|
@@ -524,7 +524,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
524
524
|
{
|
|
525
525
|
const elements = this._elements.enumerate();
|
|
526
526
|
|
|
527
|
-
return new ReducedIterator(function* ()
|
|
527
|
+
return new ReducedIterator(function* (): Generator<[K, T]>
|
|
528
528
|
{
|
|
529
529
|
for (const [index, [key, element]] of elements)
|
|
530
530
|
{
|
|
@@ -572,7 +572,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
572
572
|
{
|
|
573
573
|
const elements = this._elements.enumerate();
|
|
574
574
|
|
|
575
|
-
return new ReducedIterator(function* ()
|
|
575
|
+
return new ReducedIterator(function* (): Generator<[K, T]>
|
|
576
576
|
{
|
|
577
577
|
for (const [index, [key, element]] of elements)
|
|
578
578
|
{
|
|
@@ -726,7 +726,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
726
726
|
{
|
|
727
727
|
const elements = this._elements;
|
|
728
728
|
|
|
729
|
-
return new ReducedIterator(function* ()
|
|
729
|
+
return new ReducedIterator(function* (): Generator<[K, T]>
|
|
730
730
|
{
|
|
731
731
|
const values = new Set<T>();
|
|
732
732
|
for (const [key, element] of elements)
|
|
@@ -838,7 +838,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
838
838
|
{
|
|
839
839
|
const elements = this._elements.enumerate();
|
|
840
840
|
|
|
841
|
-
return new AggregatedIterator(function* ()
|
|
841
|
+
return new AggregatedIterator(function* (): Generator<[J, T]>
|
|
842
842
|
{
|
|
843
843
|
for (const [index, [key, element]] of elements)
|
|
844
844
|
{
|
|
@@ -16,9 +16,9 @@ import type { Callback } from "./types.js";
|
|
|
16
16
|
* .add(() => console.log("Doing something else..."))
|
|
17
17
|
* .add(() => console.log("Doing yet another thing..."));
|
|
18
18
|
*
|
|
19
|
-
* unsubscribeAll();
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* unsubscribeAll(); // Doing something...
|
|
20
|
+
* // Doing something else...
|
|
21
|
+
* // Doing yet another thing...
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
24
|
* ---
|
|
@@ -87,7 +87,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
87
87
|
* context.subscribe("player:spawn", () => console.log("Player has spawned."));
|
|
88
88
|
*
|
|
89
89
|
* publisher.publish("player:spawn"); // "Player has spawned."
|
|
90
|
-
* context.publish("player:death");
|
|
90
|
+
* context.publish("player:death"); // * no output *
|
|
91
91
|
* ```
|
|
92
92
|
*
|
|
93
93
|
* ---
|
|
@@ -282,7 +282,7 @@ export default class ArrayView<T> extends Array<T>
|
|
|
282
282
|
* ```ts
|
|
283
283
|
* array.onAdd((value, index) => console.log(`Added ${value} at index ${index}`));
|
|
284
284
|
*
|
|
285
|
-
* array.push(2);
|
|
285
|
+
* array.push(2); // "Added 2 at index 0"
|
|
286
286
|
* array.push(42); // "Added 42 at index 1"
|
|
287
287
|
* ```
|
|
288
288
|
*
|
|
@@ -306,7 +306,7 @@ export default class ArrayView<T> extends Array<T>
|
|
|
306
306
|
* ```ts
|
|
307
307
|
* array.onRemove((value, index) => console.log(`Removed ${value} at index ${index}`));
|
|
308
308
|
*
|
|
309
|
-
* array.pop();
|
|
309
|
+
* array.pop(); // "Removed 8 at index 2"
|
|
310
310
|
* array.shift(); // "Removed 2 at index 0"
|
|
311
311
|
* ```
|
|
312
312
|
*
|
|
@@ -162,7 +162,7 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
162
162
|
* ```ts
|
|
163
163
|
* map.onAdd((key, value) => console.log(`Added ${key}: ${value}`));
|
|
164
164
|
*
|
|
165
|
-
* map.set("key1", 2);
|
|
165
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
166
166
|
* map.set("answer", 42); // "Added answer: 42"
|
|
167
167
|
* ```
|
|
168
168
|
*
|
|
@@ -186,7 +186,7 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
186
186
|
* ```ts
|
|
187
187
|
* map.onRemove((key, value) => console.log(`Removed ${key}: ${value}`));
|
|
188
188
|
*
|
|
189
|
-
* map.delete("key1");
|
|
189
|
+
* map.delete("key1"); // "Removed key1: 2"
|
|
190
190
|
* map.delete("answer"); // "Removed answer: 42"
|
|
191
191
|
* ```
|
|
192
192
|
*
|
|
@@ -106,7 +106,7 @@ export default class SetView<T> extends Set<T>
|
|
|
106
106
|
* @example
|
|
107
107
|
* ```ts
|
|
108
108
|
* const set = new SetView<number>([2, 4, 8]);
|
|
109
|
-
* set.delete(4);
|
|
109
|
+
* set.delete(4); // true
|
|
110
110
|
* set.delete(16); // false
|
|
111
111
|
*
|
|
112
112
|
* console.log(set); // SetView(2) { 2, 8 }
|
|
@@ -156,7 +156,7 @@ export default class SetView<T> extends Set<T>
|
|
|
156
156
|
* ```ts
|
|
157
157
|
* set.onAdd((value) => console.log(`Added ${value}`));
|
|
158
158
|
*
|
|
159
|
-
* set.add(2);
|
|
159
|
+
* set.add(2); // "Added 2"
|
|
160
160
|
* set.add(42); // "Added 42"
|
|
161
161
|
* ```
|
|
162
162
|
*
|
|
@@ -180,7 +180,7 @@ export default class SetView<T> extends Set<T>
|
|
|
180
180
|
* ```ts
|
|
181
181
|
* set.onRemove((value) => console.log(`Removed ${value}`));
|
|
182
182
|
*
|
|
183
|
-
* set.delete(2);
|
|
183
|
+
* set.delete(2); // "Removed 2"
|
|
184
184
|
* set.delete(42); // "Removed 42"
|
|
185
185
|
* ```
|
|
186
186
|
*
|
|
@@ -29,7 +29,7 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
29
29
|
* ```ts
|
|
30
30
|
* map.onAdd((key, value) => console.log(`Added ${key}: ${value}`));
|
|
31
31
|
*
|
|
32
|
-
* map.set("key1", 2);
|
|
32
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
33
33
|
* map.set("answer", 42); // "Added answer: 42"
|
|
34
34
|
* ```
|
|
35
35
|
*
|
|
@@ -50,7 +50,7 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
50
50
|
* ```ts
|
|
51
51
|
* map.onRemove((key, value) => console.log(`Removed ${key}: ${value}`));
|
|
52
52
|
*
|
|
53
|
-
* map.delete("key1");
|
|
53
|
+
* map.delete("key1"); // "Removed key1: 2"
|
|
54
54
|
* map.delete("answer"); // "Removed answer: 42"
|
|
55
55
|
* ```
|
|
56
56
|
*
|
|
@@ -104,7 +104,7 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
104
104
|
* ```ts
|
|
105
105
|
* set.onAdd((value) => console.log(`Added ${value}`));
|
|
106
106
|
*
|
|
107
|
-
* set.add(2);
|
|
107
|
+
* set.add(2); // "Added 2"
|
|
108
108
|
* set.add(42); // "Added 42"
|
|
109
109
|
* ```
|
|
110
110
|
*
|
|
@@ -125,7 +125,7 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
125
125
|
* ```ts
|
|
126
126
|
* set.onRemove((value) => console.log(`Removed ${value}`));
|
|
127
127
|
*
|
|
128
|
-
* set.delete(2);
|
|
128
|
+
* set.delete(2); // "Removed 2"
|
|
129
129
|
* set.delete(42); // "Removed 42"
|
|
130
130
|
* ```
|
|
131
131
|
*
|
|
@@ -696,7 +696,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
696
696
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
697
697
|
* const result = iterator.take(3);
|
|
698
698
|
*
|
|
699
|
-
* console.log(await result.toArray());
|
|
699
|
+
* console.log(await result.toArray()); // [-2, -1, 0]
|
|
700
700
|
* console.log(await iterator.toArray()); // [1, 2]
|
|
701
701
|
* ```
|
|
702
702
|
*
|
|
@@ -580,7 +580,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
|
|
|
580
580
|
* const iterator = new SmartIterator<number>([-2, -1, 0, 1, 2]);
|
|
581
581
|
* const result = iterator.take(3);
|
|
582
582
|
*
|
|
583
|
-
* console.log(result.toArray());
|
|
583
|
+
* console.log(result.toArray()); // [-2, -1, 0]
|
|
584
584
|
* console.log(iterator.toArray()); // [1, 2]
|
|
585
585
|
* ```
|
|
586
586
|
*
|
|
@@ -17,12 +17,12 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
|
|
|
17
17
|
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
18
18
|
* });
|
|
19
19
|
*
|
|
20
|
-
* console.log(promise.isPending);
|
|
20
|
+
* console.log(promise.isPending); // true
|
|
21
21
|
* console.log(promise.isFulfilled); // false
|
|
22
22
|
*
|
|
23
23
|
* console.log(await promise); // "Hello, World!"
|
|
24
24
|
*
|
|
25
|
-
* console.log(promise.isPending);
|
|
25
|
+
* console.log(promise.isPending); // false
|
|
26
26
|
* console.log(promise.isFulfilled); // true
|
|
27
27
|
* ```
|
|
28
28
|
*
|
|
@@ -39,14 +39,14 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
39
39
|
*
|
|
40
40
|
* @example
|
|
41
41
|
* ```ts
|
|
42
|
-
* const
|
|
43
|
-
* const
|
|
42
|
+
* const response = fetch("https://api.example.com/data");
|
|
43
|
+
* const smartResponse = SmartPromise.FromPromise(response);
|
|
44
44
|
*
|
|
45
|
-
* console.log(
|
|
46
|
-
* console.log(
|
|
45
|
+
* console.log(response.isPending); // Throws an error: `isPending` is not a property of `Promise`.
|
|
46
|
+
* console.log(smartResponse.isPending); // true
|
|
47
47
|
*
|
|
48
|
-
* const response = await
|
|
49
|
-
* console.log(
|
|
48
|
+
* const response = await response;
|
|
49
|
+
* console.log(smartResponse.isFulfilled); // true
|
|
50
50
|
* ```
|
|
51
51
|
*
|
|
52
52
|
* ---
|
|
@@ -323,8 +323,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
323
323
|
*
|
|
324
324
|
*
|
|
325
325
|
* promise
|
|
326
|
-
* .then(() => console.log("OK!"))
|
|
327
|
-
* .catch(() => console.log("KO!"))
|
|
326
|
+
* .then(() => console.log("OK!")) // Logs "OK!" if the promise is fulfilled.
|
|
327
|
+
* .catch(() => console.log("KO!")) // Logs "KO!" if the promise is rejected.
|
|
328
328
|
* .finally(() => console.log("Done!")); // Always logs "Done!".
|
|
329
329
|
* ```
|
|
330
330
|
*
|
|
@@ -20,7 +20,7 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
|
20
20
|
* }, 5_000);
|
|
21
21
|
*
|
|
22
22
|
* promise
|
|
23
|
-
* .then((result) => console.log(result))
|
|
23
|
+
* .then((result) => console.log(result)) // "Hello, World!"
|
|
24
24
|
* .catch((error) => console.error(error)); // "Uncaught TimeoutException: The operation has timed out."
|
|
25
25
|
* ```
|
|
26
26
|
*
|
package/src/utils/index.ts
CHANGED
|
@@ -5,5 +5,5 @@ export { delay, nextAnimationFrame, yieldToEventLoop } from "./async.js";
|
|
|
5
5
|
export { dateDifference, dateRange, dateRound, getWeek, TimeUnit, WeekDay } from "./date.js";
|
|
6
6
|
export { loadScript } from "./dom.js";
|
|
7
7
|
export { chain, count, enumerate, range, shuffle, unique, zip } from "./iterator.js";
|
|
8
|
-
export { average, hash, sum } from "./math.js";
|
|
8
|
+
export { average, clamp, hash, sum } from "./math.js";
|
|
9
9
|
export { capitalize } from "./string.js";
|
package/src/utils/iterator.ts
CHANGED
|
@@ -102,7 +102,7 @@ export function count<T>(elements: Iterable<T>): number
|
|
|
102
102
|
*/
|
|
103
103
|
export function enumerate<T>(elements: Iterable<T>): SmartIterator<[number, T]>
|
|
104
104
|
{
|
|
105
|
-
return new SmartIterator<[number, T]>(function* ()
|
|
105
|
+
return new SmartIterator<[number, T]>(function* (): Generator<[number, T]>
|
|
106
106
|
{
|
|
107
107
|
let index = 0;
|
|
108
108
|
for (const element of elements)
|
|
@@ -314,7 +314,7 @@ export function zip<T, U>(first: Iterable<T>, second: Iterable<U>): SmartIterato
|
|
|
314
314
|
const firstIterator = first[Symbol.iterator]();
|
|
315
315
|
const secondIterator = second[Symbol.iterator]();
|
|
316
316
|
|
|
317
|
-
return new SmartIterator<[T, U]>(function* ()
|
|
317
|
+
return new SmartIterator<[T, U]>(function* (): Generator<[T, U]>
|
|
318
318
|
{
|
|
319
319
|
while (true)
|
|
320
320
|
{
|
package/src/utils/math.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { zip } from "./iterator.js";
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```ts
|
|
12
|
-
* average([1, 2, 3, 4, 5]);
|
|
12
|
+
* average([1, 2, 3, 4, 5]); // 3
|
|
13
13
|
* average([6, 8.5, 4], [3, 2, 1]); // 6.5
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
@@ -71,6 +71,39 @@ export function average<T extends number>(values: Iterable<T>, weights?: Iterabl
|
|
|
71
71
|
return _sum / _count;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Clamps a given value between a minimum and a maximum bound.
|
|
76
|
+
*
|
|
77
|
+
* ---
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* clamp(5, 0, 10); // 5
|
|
82
|
+
* clamp(-3, 0, 10); // 0
|
|
83
|
+
* clamp(15, 0, 10); // 10
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* ---
|
|
87
|
+
*
|
|
88
|
+
* @param value The value to clamp.
|
|
89
|
+
* @param min The minimum bound.
|
|
90
|
+
* @param max The maximum bound.
|
|
91
|
+
*
|
|
92
|
+
* @returns The clamped value between the specified bounds.
|
|
93
|
+
*/
|
|
94
|
+
export function clamp(value: number, min: number, max: number): number
|
|
95
|
+
{
|
|
96
|
+
if (min > max)
|
|
97
|
+
{
|
|
98
|
+
throw new ValueException("The minimum bound must be less than or equal to the maximum bound.");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (value < min) { return min; }
|
|
102
|
+
if (value > max) { return max; }
|
|
103
|
+
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
74
107
|
/**
|
|
75
108
|
* An utility function to compute the hash of a given string.
|
|
76
109
|
*
|
|
@@ -83,7 +116,7 @@ export function average<T extends number>(values: Iterable<T>, weights?: Iterabl
|
|
|
83
116
|
* @example
|
|
84
117
|
* ```ts
|
|
85
118
|
* hash("Hello, world!"); // -1880044555
|
|
86
|
-
* hash("How are you?");
|
|
119
|
+
* hash("How are you?"); // 1761539132
|
|
87
120
|
* ```
|
|
88
121
|
*
|
|
89
122
|
* ---
|
package/src/utils/random.ts
CHANGED
|
@@ -338,8 +338,8 @@ export default class Random
|
|
|
338
338
|
*
|
|
339
339
|
* @example
|
|
340
340
|
* ```ts
|
|
341
|
-
* Random.Split(100, 3);
|
|
342
|
-
* Random.Split(10, 4);
|
|
341
|
+
* Random.Split(100, 3); // [28, 41, 31]
|
|
342
|
+
* Random.Split(10, 4); // [3, 1, 4, 2]
|
|
343
343
|
* ```
|
|
344
344
|
*
|
|
345
345
|
* ---
|
|
@@ -368,9 +368,9 @@ export default class Random
|
|
|
368
368
|
*
|
|
369
369
|
* @example
|
|
370
370
|
* ```ts
|
|
371
|
-
* Random.Split([1, 2, 3, 4, 5], 2);
|
|
372
|
-
* Random.Split([1, 2, 3, 4, 5], 2);
|
|
373
|
-
* Random.Split("abcdef", 3);
|
|
371
|
+
* Random.Split([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4, 5]]
|
|
372
|
+
* Random.Split([1, 2, 3, 4, 5], 2); // [[1, 2, 3, 4], [5]]
|
|
373
|
+
* Random.Split("abcdef", 3); // [["a"], ["b", "c", "d"], ["e", "f"]]
|
|
374
374
|
* ```
|
|
375
375
|
*
|
|
376
376
|
* ---
|