@develia/commons 0.4.24 → 0.4.26
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/array.d.ts.map +1 -0
- package/dist/array.js.map +1 -0
- package/dist/async-pool-executor.d.ts.map +1 -0
- package/dist/async-pool-executor.js.map +1 -0
- package/dist/browser/form-utilities.d.ts.map +1 -0
- package/dist/browser/form-utilities.js.map +1 -0
- package/dist/browser/geo-location.d.ts.map +1 -0
- package/dist/browser/geo-location.js.map +1 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/native-bridge.d.ts.map +1 -0
- package/dist/browser/native-bridge.js.map +1 -0
- package/dist/cancellable-promise.d.ts.map +1 -0
- package/dist/cancellable-promise.js.map +1 -0
- package/dist/clean-url-search-params.d.ts.map +1 -0
- package/dist/clean-url-search-params.js.map +1 -0
- package/dist/collections/cache-dictionary.d.ts.map +1 -0
- package/dist/collections/cache-dictionary.js.map +1 -0
- package/dist/collections/default-map.d.ts.map +1 -0
- package/dist/collections/default-map.js.map +1 -0
- package/dist/collections/default-weak-map.d.ts.map +1 -0
- package/dist/collections/default-weak-map.js.map +1 -0
- package/dist/{types/collections → collections}/from.d.ts +15 -6
- package/dist/collections/from.d.ts.map +1 -0
- package/dist/{esm/collections → collections}/from.js +4 -10
- package/dist/collections/from.js.map +1 -0
- package/dist/collections/index.d.ts.map +1 -0
- package/dist/collections/index.js.map +1 -0
- package/dist/debouncer.d.ts.map +1 -0
- package/dist/debouncer.js.map +1 -0
- package/dist/draft.d.ts.map +1 -0
- package/dist/draft.js.map +1 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/functions.d.ts.map +1 -0
- package/dist/functions.js.map +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/key-value-pair.d.ts.map +1 -0
- package/dist/key-value-pair.js.map +1 -0
- package/dist/keyed-debouncer.d.ts.map +1 -0
- package/dist/keyed-debouncer.js.map +1 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js.map +1 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js.map +1 -0
- package/dist/order.d.ts.map +1 -0
- package/dist/order.js.map +1 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js.map +1 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js.map +1 -0
- package/dist/range.d.ts.map +1 -0
- package/dist/range.js.map +1 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js.map +1 -0
- package/dist/stopwatch.d.ts.map +1 -0
- package/dist/stopwatch.js.map +1 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/string.js.map +1 -0
- package/dist/timer.d.ts.map +1 -0
- package/dist/timer.js.map +1 -0
- package/dist/timespan.d.ts.map +1 -0
- package/dist/timespan.js.map +1 -0
- package/dist/triggers.d.ts.map +1 -0
- package/dist/triggers.js.map +1 -0
- package/dist/type-code.d.ts.map +1 -0
- package/dist/type-code.js.map +1 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js.map +1 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js.map +1 -0
- package/dist/{types/utilities.d.ts → utilities.d.ts} +4 -3
- package/dist/utilities.d.ts.map +1 -0
- package/dist/{web/utilities.js → utilities.js} +4 -3
- package/dist/utilities.js.map +1 -0
- package/package.json +13 -62
- package/src/collections/from.ts +21 -11
- package/src/utilities.ts +5 -4
- package/dist/cjs/array.js +0 -106
- package/dist/cjs/array.js.map +0 -1
- package/dist/cjs/async-pool-executor.js +0 -79
- package/dist/cjs/async-pool-executor.js.map +0 -1
- package/dist/cjs/browser/form-utilities.js +0 -162
- package/dist/cjs/browser/form-utilities.js.map +0 -1
- package/dist/cjs/browser/geo-location.js +0 -78
- package/dist/cjs/browser/geo-location.js.map +0 -1
- package/dist/cjs/browser/index.js +0 -99
- package/dist/cjs/browser/index.js.map +0 -1
- package/dist/cjs/browser/native-bridge.js +0 -81
- package/dist/cjs/browser/native-bridge.js.map +0 -1
- package/dist/cjs/cancellable-promise.js +0 -81
- package/dist/cjs/cancellable-promise.js.map +0 -1
- package/dist/cjs/clean-url-search-params.js +0 -34
- package/dist/cjs/clean-url-search-params.js.map +0 -1
- package/dist/cjs/collections/cache-dictionary.js +0 -102
- package/dist/cjs/collections/cache-dictionary.js.map +0 -1
- package/dist/cjs/collections/default-map.js +0 -45
- package/dist/cjs/collections/default-map.js.map +0 -1
- package/dist/cjs/collections/default-weak-map.js +0 -46
- package/dist/cjs/collections/default-weak-map.js.map +0 -1
- package/dist/cjs/collections/from.js +0 -778
- package/dist/cjs/collections/from.js.map +0 -1
- package/dist/cjs/collections/index.js +0 -15
- package/dist/cjs/collections/index.js.map +0 -1
- package/dist/cjs/debouncer.js +0 -121
- package/dist/cjs/debouncer.js.map +0 -1
- package/dist/cjs/draft.js +0 -225
- package/dist/cjs/draft.js.map +0 -1
- package/dist/cjs/event-bus.js +0 -71
- package/dist/cjs/event-bus.js.map +0 -1
- package/dist/cjs/functions.js +0 -262
- package/dist/cjs/functions.js.map +0 -1
- package/dist/cjs/index.js +0 -84
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/key-value-pair.js +0 -22
- package/dist/cjs/key-value-pair.js.map +0 -1
- package/dist/cjs/keyed-debouncer.js +0 -144
- package/dist/cjs/keyed-debouncer.js.map +0 -1
- package/dist/cjs/lazy.js +0 -29
- package/dist/cjs/lazy.js.map +0 -1
- package/dist/cjs/math.js +0 -37
- package/dist/cjs/math.js.map +0 -1
- package/dist/cjs/order.js +0 -9
- package/dist/cjs/order.js.map +0 -1
- package/dist/cjs/parse.js +0 -174
- package/dist/cjs/parse.js.map +0 -1
- package/dist/cjs/patch.js +0 -52
- package/dist/cjs/patch.js.map +0 -1
- package/dist/cjs/range.js +0 -154
- package/dist/cjs/range.js.map +0 -1
- package/dist/cjs/result.js +0 -3
- package/dist/cjs/result.js.map +0 -1
- package/dist/cjs/stopwatch.js +0 -46
- package/dist/cjs/stopwatch.js.map +0 -1
- package/dist/cjs/string.js +0 -154
- package/dist/cjs/string.js.map +0 -1
- package/dist/cjs/timer.js +0 -57
- package/dist/cjs/timer.js.map +0 -1
- package/dist/cjs/timespan.js +0 -367
- package/dist/cjs/timespan.js.map +0 -1
- package/dist/cjs/triggers.js +0 -103
- package/dist/cjs/triggers.js.map +0 -1
- package/dist/cjs/type-code.js +0 -18
- package/dist/cjs/type-code.js.map +0 -1
- package/dist/cjs/type.js +0 -296
- package/dist/cjs/type.js.map +0 -1
- package/dist/cjs/types.js +0 -3
- package/dist/cjs/types.js.map +0 -1
- package/dist/cjs/utilities.js +0 -621
- package/dist/cjs/utilities.js.map +0 -1
- package/dist/esm/array.js.map +0 -1
- package/dist/esm/async-pool-executor.js.map +0 -1
- package/dist/esm/browser/form-utilities.js.map +0 -1
- package/dist/esm/browser/geo-location.js.map +0 -1
- package/dist/esm/browser/index.js.map +0 -1
- package/dist/esm/browser/native-bridge.js.map +0 -1
- package/dist/esm/cancellable-promise.js.map +0 -1
- package/dist/esm/clean-url-search-params.js.map +0 -1
- package/dist/esm/collections/cache-dictionary.js.map +0 -1
- package/dist/esm/collections/default-map.js.map +0 -1
- package/dist/esm/collections/default-weak-map.js.map +0 -1
- package/dist/esm/collections/from.js.map +0 -1
- package/dist/esm/collections/index.js.map +0 -1
- package/dist/esm/debouncer.js.map +0 -1
- package/dist/esm/draft.js.map +0 -1
- package/dist/esm/event-bus.js.map +0 -1
- package/dist/esm/functions.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/key-value-pair.js.map +0 -1
- package/dist/esm/keyed-debouncer.js.map +0 -1
- package/dist/esm/lazy.js.map +0 -1
- package/dist/esm/math.js.map +0 -1
- package/dist/esm/order.js.map +0 -1
- package/dist/esm/parse.js.map +0 -1
- package/dist/esm/patch.js.map +0 -1
- package/dist/esm/range.js.map +0 -1
- package/dist/esm/result.js.map +0 -1
- package/dist/esm/stopwatch.js.map +0 -1
- package/dist/esm/string.js.map +0 -1
- package/dist/esm/timer.js.map +0 -1
- package/dist/esm/timespan.js.map +0 -1
- package/dist/esm/triggers.js.map +0 -1
- package/dist/esm/type-code.js.map +0 -1
- package/dist/esm/type.js.map +0 -1
- package/dist/esm/types.js.map +0 -1
- package/dist/esm/utilities.js +0 -590
- package/dist/esm/utilities.js.map +0 -1
- package/dist/types/array.d.ts.map +0 -1
- package/dist/types/async-pool-executor.d.ts.map +0 -1
- package/dist/types/browser/form-utilities.d.ts.map +0 -1
- package/dist/types/browser/geo-location.d.ts.map +0 -1
- package/dist/types/browser/index.d.ts.map +0 -1
- package/dist/types/browser/native-bridge.d.ts.map +0 -1
- package/dist/types/cancellable-promise.d.ts.map +0 -1
- package/dist/types/clean-url-search-params.d.ts.map +0 -1
- package/dist/types/collections/cache-dictionary.d.ts.map +0 -1
- package/dist/types/collections/default-map.d.ts.map +0 -1
- package/dist/types/collections/default-weak-map.d.ts.map +0 -1
- package/dist/types/collections/from.d.ts.map +0 -1
- package/dist/types/collections/index.d.ts.map +0 -1
- package/dist/types/debouncer.d.ts.map +0 -1
- package/dist/types/draft.d.ts.map +0 -1
- package/dist/types/event-bus.d.ts.map +0 -1
- package/dist/types/functions.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/key-value-pair.d.ts.map +0 -1
- package/dist/types/keyed-debouncer.d.ts.map +0 -1
- package/dist/types/lazy.d.ts.map +0 -1
- package/dist/types/math.d.ts.map +0 -1
- package/dist/types/order.d.ts.map +0 -1
- package/dist/types/parse.d.ts.map +0 -1
- package/dist/types/patch.d.ts.map +0 -1
- package/dist/types/range.d.ts.map +0 -1
- package/dist/types/result.d.ts.map +0 -1
- package/dist/types/stopwatch.d.ts.map +0 -1
- package/dist/types/string.d.ts.map +0 -1
- package/dist/types/timer.d.ts.map +0 -1
- package/dist/types/timespan.d.ts.map +0 -1
- package/dist/types/triggers.d.ts.map +0 -1
- package/dist/types/type-code.d.ts.map +0 -1
- package/dist/types/type.d.ts.map +0 -1
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/utilities.d.ts.map +0 -1
- package/dist/web/array.js +0 -98
- package/dist/web/array.js.map +0 -1
- package/dist/web/async-pool-executor.js +0 -76
- package/dist/web/async-pool-executor.js.map +0 -1
- package/dist/web/browser/form-utilities.js +0 -155
- package/dist/web/browser/form-utilities.js.map +0 -1
- package/dist/web/browser/geo-location.js +0 -75
- package/dist/web/browser/geo-location.js.map +0 -1
- package/dist/web/browser/index.js +0 -75
- package/dist/web/browser/index.js.map +0 -1
- package/dist/web/browser/native-bridge.js +0 -78
- package/dist/web/browser/native-bridge.js.map +0 -1
- package/dist/web/cancellable-promise.js +0 -78
- package/dist/web/cancellable-promise.js.map +0 -1
- package/dist/web/clean-url-search-params.js +0 -31
- package/dist/web/clean-url-search-params.js.map +0 -1
- package/dist/web/collections/cache-dictionary.js +0 -99
- package/dist/web/collections/cache-dictionary.js.map +0 -1
- package/dist/web/collections/default-map.js +0 -42
- package/dist/web/collections/default-map.js.map +0 -1
- package/dist/web/collections/default-weak-map.js +0 -43
- package/dist/web/collections/default-weak-map.js.map +0 -1
- package/dist/web/collections/from.js +0 -770
- package/dist/web/collections/from.js.map +0 -1
- package/dist/web/collections/index.js +0 -5
- package/dist/web/collections/index.js.map +0 -1
- package/dist/web/debouncer.js +0 -118
- package/dist/web/debouncer.js.map +0 -1
- package/dist/web/draft.js +0 -222
- package/dist/web/draft.js.map +0 -1
- package/dist/web/event-bus.js +0 -66
- package/dist/web/event-bus.js.map +0 -1
- package/dist/web/functions.js +0 -251
- package/dist/web/functions.js.map +0 -1
- package/dist/web/index.js +0 -40
- package/dist/web/index.js.map +0 -1
- package/dist/web/key-value-pair.js +0 -19
- package/dist/web/key-value-pair.js.map +0 -1
- package/dist/web/keyed-debouncer.js +0 -109
- package/dist/web/keyed-debouncer.js.map +0 -1
- package/dist/web/lazy.js +0 -26
- package/dist/web/lazy.js.map +0 -1
- package/dist/web/math.js +0 -33
- package/dist/web/math.js.map +0 -1
- package/dist/web/order.js +0 -7
- package/dist/web/order.js.map +0 -1
- package/dist/web/parse.js +0 -169
- package/dist/web/parse.js.map +0 -1
- package/dist/web/patch.js +0 -48
- package/dist/web/patch.js.map +0 -1
- package/dist/web/range.js +0 -151
- package/dist/web/range.js.map +0 -1
- package/dist/web/result.js +0 -2
- package/dist/web/result.js.map +0 -1
- package/dist/web/stopwatch.js +0 -40
- package/dist/web/stopwatch.js.map +0 -1
- package/dist/web/string.js +0 -142
- package/dist/web/string.js.map +0 -1
- package/dist/web/timer.js +0 -54
- package/dist/web/timer.js.map +0 -1
- package/dist/web/timespan.js +0 -365
- package/dist/web/timespan.js.map +0 -1
- package/dist/web/triggers.js +0 -99
- package/dist/web/triggers.js.map +0 -1
- package/dist/web/type-code.js +0 -16
- package/dist/web/type-code.js.map +0 -1
- package/dist/web/type.js +0 -292
- package/dist/web/type.js.map +0 -1
- package/dist/web/types.js +0 -2
- package/dist/web/types.js.map +0 -1
- package/dist/web/utilities.js.map +0 -1
- /package/dist/{types/array.d.ts → array.d.ts} +0 -0
- /package/dist/{esm/array.js → array.js} +0 -0
- /package/dist/{types/async-pool-executor.d.ts → async-pool-executor.d.ts} +0 -0
- /package/dist/{esm/async-pool-executor.js → async-pool-executor.js} +0 -0
- /package/dist/{types/browser → browser}/form-utilities.d.ts +0 -0
- /package/dist/{esm/browser → browser}/form-utilities.js +0 -0
- /package/dist/{types/browser → browser}/geo-location.d.ts +0 -0
- /package/dist/{esm/browser → browser}/geo-location.js +0 -0
- /package/dist/{types/browser → browser}/index.d.ts +0 -0
- /package/dist/{esm/browser → browser}/index.js +0 -0
- /package/dist/{types/browser → browser}/native-bridge.d.ts +0 -0
- /package/dist/{esm/browser → browser}/native-bridge.js +0 -0
- /package/dist/{types/cancellable-promise.d.ts → cancellable-promise.d.ts} +0 -0
- /package/dist/{esm/cancellable-promise.js → cancellable-promise.js} +0 -0
- /package/dist/{types/clean-url-search-params.d.ts → clean-url-search-params.d.ts} +0 -0
- /package/dist/{esm/clean-url-search-params.js → clean-url-search-params.js} +0 -0
- /package/dist/{types/collections → collections}/cache-dictionary.d.ts +0 -0
- /package/dist/{esm/collections → collections}/cache-dictionary.js +0 -0
- /package/dist/{types/collections → collections}/default-map.d.ts +0 -0
- /package/dist/{esm/collections → collections}/default-map.js +0 -0
- /package/dist/{types/collections → collections}/default-weak-map.d.ts +0 -0
- /package/dist/{esm/collections → collections}/default-weak-map.js +0 -0
- /package/dist/{types/collections → collections}/index.d.ts +0 -0
- /package/dist/{esm/collections → collections}/index.js +0 -0
- /package/dist/{types/debouncer.d.ts → debouncer.d.ts} +0 -0
- /package/dist/{esm/debouncer.js → debouncer.js} +0 -0
- /package/dist/{types/draft.d.ts → draft.d.ts} +0 -0
- /package/dist/{esm/draft.js → draft.js} +0 -0
- /package/dist/{types/event-bus.d.ts → event-bus.d.ts} +0 -0
- /package/dist/{esm/event-bus.js → event-bus.js} +0 -0
- /package/dist/{types/functions.d.ts → functions.d.ts} +0 -0
- /package/dist/{esm/functions.js → functions.js} +0 -0
- /package/dist/{types/index.d.ts → index.d.ts} +0 -0
- /package/dist/{esm/index.js → index.js} +0 -0
- /package/dist/{types/key-value-pair.d.ts → key-value-pair.d.ts} +0 -0
- /package/dist/{esm/key-value-pair.js → key-value-pair.js} +0 -0
- /package/dist/{types/keyed-debouncer.d.ts → keyed-debouncer.d.ts} +0 -0
- /package/dist/{esm/keyed-debouncer.js → keyed-debouncer.js} +0 -0
- /package/dist/{types/lazy.d.ts → lazy.d.ts} +0 -0
- /package/dist/{esm/lazy.js → lazy.js} +0 -0
- /package/dist/{types/math.d.ts → math.d.ts} +0 -0
- /package/dist/{esm/math.js → math.js} +0 -0
- /package/dist/{types/order.d.ts → order.d.ts} +0 -0
- /package/dist/{esm/order.js → order.js} +0 -0
- /package/dist/{types/parse.d.ts → parse.d.ts} +0 -0
- /package/dist/{esm/parse.js → parse.js} +0 -0
- /package/dist/{types/patch.d.ts → patch.d.ts} +0 -0
- /package/dist/{esm/patch.js → patch.js} +0 -0
- /package/dist/{types/range.d.ts → range.d.ts} +0 -0
- /package/dist/{esm/range.js → range.js} +0 -0
- /package/dist/{types/result.d.ts → result.d.ts} +0 -0
- /package/dist/{esm/result.js → result.js} +0 -0
- /package/dist/{types/stopwatch.d.ts → stopwatch.d.ts} +0 -0
- /package/dist/{esm/stopwatch.js → stopwatch.js} +0 -0
- /package/dist/{types/string.d.ts → string.d.ts} +0 -0
- /package/dist/{esm/string.js → string.js} +0 -0
- /package/dist/{types/timer.d.ts → timer.d.ts} +0 -0
- /package/dist/{esm/timer.js → timer.js} +0 -0
- /package/dist/{types/timespan.d.ts → timespan.d.ts} +0 -0
- /package/dist/{esm/timespan.js → timespan.js} +0 -0
- /package/dist/{types/triggers.d.ts → triggers.d.ts} +0 -0
- /package/dist/{esm/triggers.js → triggers.js} +0 -0
- /package/dist/{types/type-code.d.ts → type-code.d.ts} +0 -0
- /package/dist/{esm/type-code.js → type-code.js} +0 -0
- /package/dist/{types/type.d.ts → type.d.ts} +0 -0
- /package/dist/{esm/type.js → type.js} +0 -0
- /package/dist/{types/types.d.ts → types.d.ts} +0 -0
- /package/dist/{esm/types.js → types.js} +0 -0
package/dist/web/functions.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Return the received value unchanged.
|
|
3
|
-
* Useful as a default mapper, placeholder callback, or neutral transform in a pipeline.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* identity(5); // 5
|
|
8
|
-
*
|
|
9
|
-
* const value = {name: "commons"};
|
|
10
|
-
* identity(value) === value; // true
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
export function identity(value) {
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Create a function that always returns the same value.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* const getAnswer = constant(42);
|
|
22
|
-
*
|
|
23
|
-
* getAnswer(); // 42
|
|
24
|
-
* getAnswer(); // 42
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export function constant(value) {
|
|
28
|
-
return () => value;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Do nothing and return undefined.
|
|
32
|
-
* Useful as a safe default callback.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* const onComplete = options.onComplete ?? noop;
|
|
37
|
-
* onComplete();
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function noop() {
|
|
41
|
-
// Intentionally empty.
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Create a function that can be executed only once.
|
|
45
|
-
* Later calls return the result from the first execution without calling the original function again.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```ts
|
|
49
|
-
* const initialize = once(() => ({connected: true}));
|
|
50
|
-
*
|
|
51
|
-
* const first = initialize();
|
|
52
|
-
* const second = initialize();
|
|
53
|
-
* first === second; // true
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export function once(fn) {
|
|
57
|
-
let called = false;
|
|
58
|
-
let result;
|
|
59
|
-
return function (...args) {
|
|
60
|
-
if (!called) {
|
|
61
|
-
called = true;
|
|
62
|
-
result = fn.apply(this, args);
|
|
63
|
-
}
|
|
64
|
-
return result;
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Cache the result of a function and reuse it for calls with the same resolved key.
|
|
69
|
-
* By default, the first argument is used as the cache key.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* const double = memoize((value: number) => value * 2);
|
|
74
|
-
*
|
|
75
|
-
* double(4); // 8, computed
|
|
76
|
-
* double(4); // 8, read from cache
|
|
77
|
-
* double.has(4); // true
|
|
78
|
-
* ```
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* const sum = memoize(
|
|
83
|
-
* (left: number, right: number) => left + right,
|
|
84
|
-
* (left, right) => `${left}:${right}`,
|
|
85
|
-
* );
|
|
86
|
-
*
|
|
87
|
-
* sum(2, 3); // 5
|
|
88
|
-
* sum.delete(2, 3); // true
|
|
89
|
-
* sum.clear();
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
export function memoize(fn, resolver, cache = new Map()) {
|
|
93
|
-
const resolveKey = (...args) => {
|
|
94
|
-
if (resolver)
|
|
95
|
-
return resolver(...args);
|
|
96
|
-
return args[0];
|
|
97
|
-
};
|
|
98
|
-
const memoized = function (...args) {
|
|
99
|
-
const key = resolveKey(...args);
|
|
100
|
-
if (cache.has(key)) {
|
|
101
|
-
return cache.get(key);
|
|
102
|
-
}
|
|
103
|
-
const value = fn.apply(this, args);
|
|
104
|
-
cache.set(key, value);
|
|
105
|
-
return value;
|
|
106
|
-
};
|
|
107
|
-
Object.defineProperty(memoized, "cache", {
|
|
108
|
-
value: cache,
|
|
109
|
-
enumerable: false,
|
|
110
|
-
});
|
|
111
|
-
memoized.clear = () => cache.clear();
|
|
112
|
-
memoized.delete = (...args) => cache.delete(resolveKey(...args));
|
|
113
|
-
memoized.has = (...args) => cache.has(resolveKey(...args));
|
|
114
|
-
return memoized;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Limit how often a function can run during a time window.
|
|
118
|
-
* Calls made while the function is throttled can optionally be replayed once at the end with the latest arguments.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* ```ts
|
|
122
|
-
* const onScroll = throttle(() => {
|
|
123
|
-
* console.log(window.scrollY);
|
|
124
|
-
* }, 100);
|
|
125
|
-
*
|
|
126
|
-
* window.addEventListener("scroll", onScroll);
|
|
127
|
-
* ```
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* const refresh = throttle(loadData, 500, {leading: false});
|
|
132
|
-
*
|
|
133
|
-
* refresh(); // scheduled instead of executed immediately
|
|
134
|
-
* refresh.flush(); // execute the scheduled call now
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
export function throttle(fn, wait, options = {}) {
|
|
138
|
-
const delay = normalizeDelay(wait);
|
|
139
|
-
const leading = options.leading ?? true;
|
|
140
|
-
const trailing = options.trailing ?? true;
|
|
141
|
-
let timer;
|
|
142
|
-
let lastArgs;
|
|
143
|
-
let lastThis;
|
|
144
|
-
let lastInvokeTime = 0;
|
|
145
|
-
let hasInvoked = false;
|
|
146
|
-
let result;
|
|
147
|
-
const invoke = (time) => {
|
|
148
|
-
const args = lastArgs;
|
|
149
|
-
const context = lastThis;
|
|
150
|
-
lastArgs = undefined;
|
|
151
|
-
lastThis = undefined;
|
|
152
|
-
hasInvoked = true;
|
|
153
|
-
lastInvokeTime = time;
|
|
154
|
-
result = fn.apply(context, args);
|
|
155
|
-
return result;
|
|
156
|
-
};
|
|
157
|
-
const expire = () => {
|
|
158
|
-
timer = undefined;
|
|
159
|
-
if (trailing && lastArgs) {
|
|
160
|
-
invoke(Date.now());
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
lastArgs = undefined;
|
|
164
|
-
lastThis = undefined;
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
const throttled = function (...args) {
|
|
168
|
-
const now = Date.now();
|
|
169
|
-
lastArgs = args;
|
|
170
|
-
lastThis = this;
|
|
171
|
-
if (!hasInvoked) {
|
|
172
|
-
if (leading) {
|
|
173
|
-
return invoke(now);
|
|
174
|
-
}
|
|
175
|
-
hasInvoked = true;
|
|
176
|
-
lastInvokeTime = now;
|
|
177
|
-
}
|
|
178
|
-
const remaining = delay - (now - lastInvokeTime);
|
|
179
|
-
if (remaining <= 0 || remaining > delay) {
|
|
180
|
-
if (timer !== undefined) {
|
|
181
|
-
clearTimeout(timer);
|
|
182
|
-
timer = undefined;
|
|
183
|
-
}
|
|
184
|
-
return invoke(now);
|
|
185
|
-
}
|
|
186
|
-
if (timer === undefined && trailing) {
|
|
187
|
-
timer = setTimeout(expire, remaining);
|
|
188
|
-
}
|
|
189
|
-
return result;
|
|
190
|
-
};
|
|
191
|
-
throttled.cancel = () => {
|
|
192
|
-
if (timer !== undefined) {
|
|
193
|
-
clearTimeout(timer);
|
|
194
|
-
}
|
|
195
|
-
timer = undefined;
|
|
196
|
-
lastArgs = undefined;
|
|
197
|
-
lastThis = undefined;
|
|
198
|
-
lastInvokeTime = 0;
|
|
199
|
-
hasInvoked = false;
|
|
200
|
-
};
|
|
201
|
-
throttled.flush = () => {
|
|
202
|
-
if (timer !== undefined) {
|
|
203
|
-
clearTimeout(timer);
|
|
204
|
-
timer = undefined;
|
|
205
|
-
if (lastArgs && trailing) {
|
|
206
|
-
return invoke(Date.now());
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return result;
|
|
210
|
-
};
|
|
211
|
-
throttled.pending = () => timer !== undefined;
|
|
212
|
-
return throttled;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Create a function with one or more arguments already provided.
|
|
216
|
-
* Later arguments are appended after the preset arguments.
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* ```ts
|
|
220
|
-
* const sum = (a: number, b: number, c: number) => a + b + c;
|
|
221
|
-
* const addFive = partial(sum, 2, 3);
|
|
222
|
-
*
|
|
223
|
-
* addFive(4); // 9
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
export function partial(fn, ...presetArgs) {
|
|
227
|
-
return function (...laterArgs) {
|
|
228
|
-
return fn.apply(this, [...presetArgs, ...laterArgs]);
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
export function pipe(...fns) {
|
|
232
|
-
return createFlow(fns);
|
|
233
|
-
}
|
|
234
|
-
export function compose(...fns) {
|
|
235
|
-
return createFlow(fns.slice().reverse());
|
|
236
|
-
}
|
|
237
|
-
function createFlow(fns) {
|
|
238
|
-
if (fns.length === 0)
|
|
239
|
-
return identity;
|
|
240
|
-
return function (...args) {
|
|
241
|
-
let result = fns[0].apply(this, args);
|
|
242
|
-
for (let i = 1; i < fns.length; i++) {
|
|
243
|
-
result = fns[i].call(this, result);
|
|
244
|
-
}
|
|
245
|
-
return result;
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
function normalizeDelay(wait) {
|
|
249
|
-
return Number.isFinite(wait) ? Math.max(0, wait) : 0;
|
|
250
|
-
}
|
|
251
|
-
//# sourceMappingURL=functions.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AAgEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAChC,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAChC,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,IAAI;IAChB,uBAAuB;AAC3B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,IAAI,CAAwB,EAAK;IAC7C,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,IAAI,MAAqB,CAAC;IAE1B,OAAO,UAAsC,GAAG,IAAmB;QAC/D,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,MAAM,GAAG,IAAI,CAAC;YACd,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAM,CAAC;AACX,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,OAAO,CACnB,EAAK,EACL,QAAwC,EACxC,QAA+B,IAAI,GAAG,EAAoB;IAE1D,MAAM,UAAU,GAAG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any, ...args: any[]) => any;\ntype TimerHandle = ReturnType<typeof setTimeout>;\n\n/** Options that control when a throttled function is executed. */\nexport interface ThrottleOptions {\n /** Execute immediately on the first call in a throttle window. Defaults to true. */\n leading?: boolean;\n /** Execute once more at the end of the throttle window using the latest arguments. Defaults to true. */\n trailing?: boolean;\n}\n\n/**\n * A delayed function returned by {@link throttle}.\n *\n * @example\n * ```ts\n * const refresh = throttle(loadData, 250);\n * refresh();\n *\n * if (refresh.pending()) {\n * refresh.flush();\n * }\n * ```\n */\nexport type DeferredFunction<F extends AnyFunction> = ((\n this: ThisParameterType<F>,\n ...args: Parameters<F>\n) => ReturnType<F> | undefined) & {\n /** Cancel the scheduled call and forget the latest arguments. */\n cancel(): void;\n /** Execute the scheduled trailing call immediately, when one exists. */\n flush(): ReturnType<F> | undefined;\n /** Return true while a trailing call is scheduled. */\n pending(): boolean;\n};\n\n/**\n * A memoized function with helpers for inspecting and invalidating its cache.\n *\n * @example\n * ```ts\n * const getUser = memoize((id: string) => fetchUser(id));\n *\n * getUser(\"42\");\n * getUser.has(\"42\"); // true\n * getUser.delete(\"42\");\n * ```\n */\nexport type MemoizedFunction<F extends AnyFunction, K> = ((\n this: ThisParameterType<F>,\n ...args: Parameters<F>\n) => ReturnType<F>) & {\n /** The backing cache used by the memoized function. */\n readonly cache: Map<K, ReturnType<F>>;\n /** Remove every cached value. */\n clear(): void;\n /** Remove the cached value for the key resolved from the provided arguments. */\n delete(...args: Parameters<F>): boolean;\n /** Return true when a cached value exists for the key resolved from the provided arguments. */\n has(...args: Parameters<F>): boolean;\n};\n\ntype Unary<T, R> = (arg: T) => R;\n\n/**\n * Return the received value unchanged.\n * Useful as a default mapper, placeholder callback, or neutral transform in a pipeline.\n *\n * @example\n * ```ts\n * identity(5); // 5\n *\n * const value = {name: \"commons\"};\n * identity(value) === value; // true\n * ```\n */\nexport function identity<T>(value: T): T {\n return value;\n}\n\n/**\n * Create a function that always returns the same value.\n *\n * @example\n * ```ts\n * const getAnswer = constant(42);\n *\n * getAnswer(); // 42\n * getAnswer(); // 42\n * ```\n */\nexport function constant<T>(value: T): () => T {\n return () => value;\n}\n\n/**\n * Do nothing and return undefined.\n * Useful as a safe default callback.\n *\n * @example\n * ```ts\n * const onComplete = options.onComplete ?? noop;\n * onComplete();\n * ```\n */\nexport function noop(): void {\n // Intentionally empty.\n}\n\n/**\n * Create a function that can be executed only once.\n * Later calls return the result from the first execution without calling the original function again.\n *\n * @example\n * ```ts\n * const initialize = once(() => ({connected: true}));\n *\n * const first = initialize();\n * const second = initialize();\n * first === second; // true\n * ```\n */\nexport function once<F extends AnyFunction>(fn: F): F {\n let called = false;\n let result: ReturnType<F>;\n\n return function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> {\n if (!called) {\n called = true;\n result = fn.apply(this, args);\n }\n return result;\n } as F;\n}\n\n/**\n * Cache the result of a function and reuse it for calls with the same resolved key.\n * By default, the first argument is used as the cache key.\n *\n * @example\n * ```ts\n * const double = memoize((value: number) => value * 2);\n *\n * double(4); // 8, computed\n * double(4); // 8, read from cache\n * double.has(4); // true\n * ```\n *\n * @example\n * ```ts\n * const sum = memoize(\n * (left: number, right: number) => left + right,\n * (left, right) => `${left}:${right}`,\n * );\n *\n * sum(2, 3); // 5\n * sum.delete(2, 3); // true\n * sum.clear();\n * ```\n */\nexport function memoize<F extends AnyFunction, K = Parameters<F>[0]>(\n fn: F,\n resolver?: (...args: Parameters<F>) => K,\n cache: Map<K, ReturnType<F>> = new Map<K, ReturnType<F>>()\n): MemoizedFunction<F, K> {\n const resolveKey = (...args: Parameters<F>): K => {\n if (resolver) return resolver(...args);\n return args[0] as K;\n };\n\n const memoized = function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> {\n const key = resolveKey(...args);\n if (cache.has(key)) {\n return cache.get(key)!;\n }\n\n const value = fn.apply(this, args);\n cache.set(key, value);\n return value;\n } as MemoizedFunction<F, K>;\n\n Object.defineProperty(memoized, \"cache\", {\n value: cache,\n enumerable: false,\n });\n\n memoized.clear = () => cache.clear();\n memoized.delete = (...args: Parameters<F>) => cache.delete(resolveKey(...args));\n memoized.has = (...args: Parameters<F>) => cache.has(resolveKey(...args));\n\n return memoized;\n}\n\n/**\n * Limit how often a function can run during a time window.\n * Calls made while the function is throttled can optionally be replayed once at the end with the latest arguments.\n *\n * @example\n * ```ts\n * const onScroll = throttle(() => {\n * console.log(window.scrollY);\n * }, 100);\n *\n * window.addEventListener(\"scroll\", onScroll);\n * ```\n *\n * @example\n * ```ts\n * const refresh = throttle(loadData, 500, {leading: false});\n *\n * refresh(); // scheduled instead of executed immediately\n * refresh.flush(); // execute the scheduled call now\n * ```\n */\nexport function throttle<F extends AnyFunction>(\n fn: F,\n wait: number,\n options: ThrottleOptions = {}\n): DeferredFunction<F> {\n const delay = normalizeDelay(wait);\n const leading = options.leading ?? true;\n const trailing = options.trailing ?? true;\n\n let timer: TimerHandle | undefined;\n let lastArgs: Parameters<F> | undefined;\n let lastThis: ThisParameterType<F> | undefined;\n let lastInvokeTime = 0;\n let hasInvoked = false;\n let result: ReturnType<F> | undefined;\n\n const invoke = (time: number): ReturnType<F> => {\n const args = lastArgs!;\n const context = lastThis!;\n lastArgs = undefined;\n lastThis = undefined;\n hasInvoked = true;\n lastInvokeTime = time;\n result = fn.apply(context, args);\n return result as ReturnType<F>;\n };\n\n const expire = (): void => {\n timer = undefined;\n if (trailing && lastArgs) {\n invoke(Date.now());\n } else {\n lastArgs = undefined;\n lastThis = undefined;\n }\n };\n\n const throttled = function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> | undefined {\n const now = Date.now();\n lastArgs = args;\n lastThis = this;\n\n if (!hasInvoked) {\n if (leading) {\n return invoke(now);\n }\n hasInvoked = true;\n lastInvokeTime = now;\n }\n\n const remaining = delay - (now - lastInvokeTime);\n if (remaining <= 0 || remaining > delay) {\n if (timer !== undefined) {\n clearTimeout(timer);\n timer = undefined;\n }\n return invoke(now);\n }\n\n if (timer === undefined && trailing) {\n timer = setTimeout(expire, remaining);\n }\n\n return result;\n } as DeferredFunction<F>;\n\n throttled.cancel = (): void => {\n if (timer !== undefined) {\n clearTimeout(timer);\n }\n timer = undefined;\n lastArgs = undefined;\n lastThis = undefined;\n lastInvokeTime = 0;\n hasInvoked = false;\n };\n\n throttled.flush = (): ReturnType<F> | undefined => {\n if (timer !== undefined) {\n clearTimeout(timer);\n timer = undefined;\n if (lastArgs && trailing) {\n return invoke(Date.now());\n }\n }\n return result;\n };\n\n throttled.pending = (): boolean => timer !== undefined;\n\n return throttled;\n}\n\n/**\n * Create a function with one or more arguments already provided.\n * Later arguments are appended after the preset arguments.\n *\n * @example\n * ```ts\n * const sum = (a: number, b: number, c: number) => a + b + c;\n * const addFive = partial(sum, 2, 3);\n *\n * addFive(4); // 9\n * ```\n */\nexport function partial<F extends AnyFunction>(\n fn: F,\n ...presetArgs: unknown[]\n): (...laterArgs: unknown[]) => ReturnType<F> {\n return function (this: ThisParameterType<F>, ...laterArgs: unknown[]): ReturnType<F> {\n return fn.apply(this, [...presetArgs, ...laterArgs] as Parameters<F>);\n };\n}\n\n/**\n * Compose unary functions from left to right.\n * The first function receives the original arguments, and each next function receives the previous result.\n *\n * @example\n * ```ts\n * const transform = pipe(\n * (value: number) => value + 1,\n * (value: number) => value * 2,\n * );\n *\n * transform(2); // 6\n * ```\n */\nexport function pipe<A, B>(ab: Unary<A, B>): Unary<A, B>;\nexport function pipe<A, B, C>(ab: Unary<A, B>, bc: Unary<B, C>): Unary<A, C>;\nexport function pipe<A, B, C, D>(ab: Unary<A, B>, bc: Unary<B, C>, cd: Unary<C, D>): Unary<A, D>;\nexport function pipe<A, B, C, D, E>(ab: Unary<A, B>, bc: Unary<B, C>, cd: Unary<C, D>, de: Unary<D, E>): Unary<A, E>;\nexport function pipe(...fns: AnyFunction[]): AnyFunction {\n return createFlow(fns);\n}\n\n/**\n * Compose unary functions from right to left.\n * The rightmost function receives the original arguments, and each function to its left receives the previous result.\n *\n * @example\n * ```ts\n * const transform = compose(\n * (value: number) => value + 1,\n * (value: number) => value * 2,\n * );\n *\n * transform(2); // 5\n * ```\n */\nexport function compose<A, B>(ab: Unary<A, B>): Unary<A, B>;\nexport function compose<A, B, C>(bc: Unary<B, C>, ab: Unary<A, B>): Unary<A, C>;\nexport function compose<A, B, C, D>(cd: Unary<C, D>, bc: Unary<B, C>, ab: Unary<A, B>): Unary<A, D>;\nexport function compose<A, B, C, D, E>(de: Unary<D, E>, cd: Unary<C, D>, bc: Unary<B, C>, ab: Unary<A, B>): Unary<A, E>;\nexport function compose(...fns: AnyFunction[]): AnyFunction {\n return createFlow(fns.slice().reverse());\n}\n\nfunction createFlow(fns: AnyFunction[]): AnyFunction {\n if (fns.length === 0) return identity;\n\n return function (this: any, ...args: any[]): unknown {\n let result = fns[0].apply(this, args);\n for (let i = 1; i < fns.length; i++) {\n result = fns[i].call(this, result);\n }\n return result;\n };\n}\n\nfunction normalizeDelay(wait: number): number {\n return Number.isFinite(wait) ? Math.max(0, wait) : 0;\n}"]}
|
package/dist/web/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
// ─── Types ───────────────────────────────────────────
|
|
3
|
-
export * from './types.js';
|
|
4
|
-
export { default as Type } from './type.js';
|
|
5
|
-
export { default as TypeCode } from './type-code.js';
|
|
6
|
-
// ─── Utilities ───────────────────────────────────────
|
|
7
|
-
export * from './utilities.js';
|
|
8
|
-
export * from './functions.js';
|
|
9
|
-
export * from './string.js';
|
|
10
|
-
export * from './math.js';
|
|
11
|
-
export * from './parse.js';
|
|
12
|
-
export { default as CancellablePromise } from "./cancellable-promise.js";
|
|
13
|
-
// ─── Data Structures ────────────────────────────────
|
|
14
|
-
export { default as KeyValuePair } from './key-value-pair.js';
|
|
15
|
-
export { default as Order } from './order.js';
|
|
16
|
-
// ─── Async & Timing ─────────────────────────────────
|
|
17
|
-
export { default as Timer } from './timer.js';
|
|
18
|
-
export { default as TimeSpan } from './timespan.js';
|
|
19
|
-
export { default as Stopwatch } from './stopwatch.js';
|
|
20
|
-
export { TimeTrigger, CountTrigger } from './triggers.js';
|
|
21
|
-
export { default as AsyncPoolExecutor } from './async-pool-executor.js';
|
|
22
|
-
export { default as Debouncer } from './debouncer.js';
|
|
23
|
-
export { default as KeyedDebouncer } from './keyed-debouncer.js';
|
|
24
|
-
// ─── Patterns ────────────────────────────────────────
|
|
25
|
-
export { default as Lazy } from './lazy.js';
|
|
26
|
-
export { default as from } from './collections/from.js';
|
|
27
|
-
export { EventBus, Event } from './event-bus.js';
|
|
28
|
-
export { default as Range } from './range.js';
|
|
29
|
-
// ─── Draft ────────────────────────────────────────
|
|
30
|
-
export { default as draft } from './draft.js';
|
|
31
|
-
// ─── Web ─────────────────────────────────────────────
|
|
32
|
-
export { default as CleanURLSearchParams } from './clean-url-search-params.js';
|
|
33
|
-
/** @deprecated */
|
|
34
|
-
export { ajaxSubmit, ajaxSubmission, submitForm, formToObject } from './browser/index.js';
|
|
35
|
-
// ─── Deprecated ──────────────────────────────────────
|
|
36
|
-
/** @deprecated */
|
|
37
|
-
export * from './array.js';
|
|
38
|
-
/** @deprecated */
|
|
39
|
-
export * from './collections/index.js';
|
|
40
|
-
//# sourceMappingURL=index.js.map
|
package/dist/web/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,wDAAwD;AACxD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAEnD,wDAAwD;AACxD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAA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nB,cAAc,wBAAwB,CAAC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport type {Result} from './result.js';\nexport {TimeTrigger, CountTrigger} from './triggers.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\nexport {default as Debouncer} from './debouncer.js';\nexport type {DebouncedTask, DebouncerOptions} from './debouncer.js';\nexport {default as KeyedDebouncer} from './keyed-debouncer.js';\nexport type {KeyedDebouncerOptions} from './keyed-debouncer.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a pair of key and value.
|
|
3
|
-
*
|
|
4
|
-
* @template TKey The type of the key.
|
|
5
|
-
* @template TValue The type of the value.
|
|
6
|
-
*/
|
|
7
|
-
export default class KeyValuePair {
|
|
8
|
-
get value() {
|
|
9
|
-
return this._value;
|
|
10
|
-
}
|
|
11
|
-
get key() {
|
|
12
|
-
return this._key;
|
|
13
|
-
}
|
|
14
|
-
constructor(key, value) {
|
|
15
|
-
this._key = key;
|
|
16
|
-
this._value = value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=key-value-pair.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"key-value-pair.js","sourceRoot":"","sources":["../../src/key-value-pair.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IAC7B,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,GAAG;QACH,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAKD,YAAY,GAAS,EAAE,KAAa;QAChC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;CAGJ","sourcesContent":["/**\n * Represents a pair of key and value.\n *\n * @template TKey The type of the key.\n * @template TValue The type of the value.\n */\nexport default class KeyValuePair<TKey, TValue> {\n get value(): TValue {\n return this._value;\n }\n\n get key(): TKey {\n return this._key;\n }\n\n private readonly _key: TKey;\n private readonly _value: TValue;\n\n constructor(key: TKey, value: TValue) {\n this._key = key;\n this._value = value;\n }\n\n\n}"]}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _KeyedDebouncer_instances, _KeyedDebouncer_debouncers, _KeyedDebouncer_delayMs, _KeyedDebouncer_onSchedule, _KeyedDebouncer_onStart, _KeyedDebouncer_onSuccess, _KeyedDebouncer_onCancel, _KeyedDebouncer_onError, _KeyedDebouncer_onIdle, _KeyedDebouncer_create;
|
|
13
|
-
import Debouncer, { assertValidDelay } from "./debouncer.js";
|
|
14
|
-
import { noop } from "./functions.js";
|
|
15
|
-
/**
|
|
16
|
-
* An independent {@link Debouncer} per key.
|
|
17
|
-
* Debouncers only exist for keys with pending work: each one is created when
|
|
18
|
-
* its first task is scheduled and removed as soon as it becomes idle.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* const debouncer = new KeyedDebouncer<string>({delayMs: 1500});
|
|
23
|
-
*
|
|
24
|
-
* debouncer.schedule(chatId, async (signal) => {
|
|
25
|
-
* const pending = await store.getPending(chatId);
|
|
26
|
-
* const reply = await callAI(pending, {signal});
|
|
27
|
-
* if (signal.aborted) return;
|
|
28
|
-
* await sendReply(chatId, reply);
|
|
29
|
-
* });
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
class KeyedDebouncer {
|
|
33
|
-
constructor(options) {
|
|
34
|
-
_KeyedDebouncer_instances.add(this);
|
|
35
|
-
_KeyedDebouncer_debouncers.set(this, new Map());
|
|
36
|
-
_KeyedDebouncer_delayMs.set(this, void 0);
|
|
37
|
-
_KeyedDebouncer_onSchedule.set(this, void 0);
|
|
38
|
-
_KeyedDebouncer_onStart.set(this, void 0);
|
|
39
|
-
_KeyedDebouncer_onSuccess.set(this, void 0);
|
|
40
|
-
_KeyedDebouncer_onCancel.set(this, void 0);
|
|
41
|
-
_KeyedDebouncer_onError.set(this, void 0);
|
|
42
|
-
_KeyedDebouncer_onIdle.set(this, void 0);
|
|
43
|
-
assertValidDelay(options.delayMs);
|
|
44
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_delayMs, options.delayMs, "f");
|
|
45
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onSchedule, options.onSchedule ?? noop, "f");
|
|
46
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onStart, options.onStart ?? noop, "f");
|
|
47
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onSuccess, options.onSuccess ?? noop, "f");
|
|
48
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onCancel, options.onCancel ?? noop, "f");
|
|
49
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onError, options.onError ?? console.error, "f");
|
|
50
|
-
__classPrivateFieldSet(this, _KeyedDebouncer_onIdle, options.onIdle ?? noop, "f");
|
|
51
|
-
}
|
|
52
|
-
/** Number of keys with a task waiting or running. */
|
|
53
|
-
get size() {
|
|
54
|
-
return __classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").size;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Schedule `task` for `key`, cancelling any previous task of that key.
|
|
58
|
-
*
|
|
59
|
-
* @param key - The key whose task is replaced.
|
|
60
|
-
* @param task - The task to run once the delay elapses.
|
|
61
|
-
* @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.
|
|
62
|
-
*/
|
|
63
|
-
schedule(key, task, delayMs) {
|
|
64
|
-
const existing = __classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").get(key);
|
|
65
|
-
if (existing !== undefined) {
|
|
66
|
-
existing.schedule(task, delayMs);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
// Validate first so an invalid delay never registers the key.
|
|
70
|
-
assertValidDelay(delayMs ?? __classPrivateFieldGet(this, _KeyedDebouncer_delayMs, "f"));
|
|
71
|
-
const debouncer = __classPrivateFieldGet(this, _KeyedDebouncer_instances, "m", _KeyedDebouncer_create).call(this, key);
|
|
72
|
-
__classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").set(key, debouncer);
|
|
73
|
-
debouncer.schedule(task, delayMs);
|
|
74
|
-
}
|
|
75
|
-
/** Cancel the task of `key`, if any. */
|
|
76
|
-
cancel(key) {
|
|
77
|
-
__classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").get(key)?.cancel();
|
|
78
|
-
}
|
|
79
|
-
/** Cancel every task. Useful when shutting the process down. */
|
|
80
|
-
cancelAll() {
|
|
81
|
-
// Copy first: cancelling removes the debouncer from the map.
|
|
82
|
-
for (const debouncer of [...__classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").values()]) {
|
|
83
|
-
debouncer.cancel();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/** Whether a task is waiting or running for `key`. */
|
|
87
|
-
isPending(key) {
|
|
88
|
-
return __classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").has(key);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
_KeyedDebouncer_debouncers = new WeakMap(), _KeyedDebouncer_delayMs = new WeakMap(), _KeyedDebouncer_onSchedule = new WeakMap(), _KeyedDebouncer_onStart = new WeakMap(), _KeyedDebouncer_onSuccess = new WeakMap(), _KeyedDebouncer_onCancel = new WeakMap(), _KeyedDebouncer_onError = new WeakMap(), _KeyedDebouncer_onIdle = new WeakMap(), _KeyedDebouncer_instances = new WeakSet(), _KeyedDebouncer_create = function _KeyedDebouncer_create(key) {
|
|
92
|
-
const debouncer = new Debouncer({
|
|
93
|
-
delayMs: __classPrivateFieldGet(this, _KeyedDebouncer_delayMs, "f"),
|
|
94
|
-
onSchedule: () => __classPrivateFieldGet(this, _KeyedDebouncer_onSchedule, "f").call(this, key),
|
|
95
|
-
onStart: () => __classPrivateFieldGet(this, _KeyedDebouncer_onStart, "f").call(this, key),
|
|
96
|
-
onSuccess: () => __classPrivateFieldGet(this, _KeyedDebouncer_onSuccess, "f").call(this, key),
|
|
97
|
-
onCancel: () => __classPrivateFieldGet(this, _KeyedDebouncer_onCancel, "f").call(this, key),
|
|
98
|
-
onError: (error) => __classPrivateFieldGet(this, _KeyedDebouncer_onError, "f").call(this, error, key),
|
|
99
|
-
onIdle: () => {
|
|
100
|
-
if (__classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").get(key) === debouncer) {
|
|
101
|
-
__classPrivateFieldGet(this, _KeyedDebouncer_debouncers, "f").delete(key);
|
|
102
|
-
__classPrivateFieldGet(this, _KeyedDebouncer_onIdle, "f").call(this, key);
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
return debouncer;
|
|
107
|
-
};
|
|
108
|
-
export default KeyedDebouncer;
|
|
109
|
-
//# sourceMappingURL=keyed-debouncer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-debouncer.js","sourceRoot":"","sources":["../../src/keyed-debouncer.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,SAAS,EAAE,EAAC,gBAAgB,EAAqB,MAAM,gBAAgB,CAAC;AAC/E,OAAO,EAAC,IAAI,EAAC,MAAM,gBAAgB,CAAC;AAoBpC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAqB,cAAc;IAU/B,YAAY,OAAiC;;QATpC,qCAAc,IAAI,GAAG,EAAgB,EAAC;QACtC,0CAAiB;QACjB,6CAA8B;QAC9B,0CAA2B;QAC3B,4CAA6B;QAC7B,2CAA4B;QAC5B,0CAA2C;QAC3C,yCAA0B;QAG/B,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAClC,uBAAA,IAAI,2BAAY,OAAO,CAAC,O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B,CAAC;eAjFgB,cAAc","sourcesContent":["import Debouncer, {assertValidDelay, type DebouncedTask} from \"./debouncer.js\";\nimport {noop} from \"./functions.js\";\n\n/** Options accepted by {@link KeyedDebouncer}. Every callback receives the affected key. */\nexport interface KeyedDebouncerOptions<K> {\n /** Default wait, in milliseconds, before a task is executed. */\n delayMs: number;\n /** Called every time a task is scheduled for `key`, after any previous task of that key has been cancelled. */\n onSchedule?: (key: K) => void;\n /** Called when the wait elapses and the task of `key` is about to run. */\n onStart?: (key: K) => void;\n /** Called when the task of `key` completes without throwing and without being cancelled. */\n onSuccess?: (key: K) => void;\n /** Called when a live task of `key` is cancelled, either explicitly or by being replaced. */\n onCancel?: (key: K) => void;\n /** Receives task errors. Cancellations never reach this callback. Defaults to `console.error`. */\n onError?: (error: unknown, key: K) => void;\n /** Called when `key` has no more pending work, after it has been removed from the debouncer. */\n onIdle?: (key: K) => void;\n}\n\n/**\n * An independent {@link Debouncer} per key.\n * Debouncers only exist for keys with pending work: each one is created when\n * its first task is scheduled and removed as soon as it becomes idle.\n *\n * @example\n * ```ts\n * const debouncer = new KeyedDebouncer<string>({delayMs: 1500});\n *\n * debouncer.schedule(chatId, async (signal) => {\n * const pending = await store.getPending(chatId);\n * const reply = await callAI(pending, {signal});\n * if (signal.aborted) return;\n * await sendReply(chatId, reply);\n * });\n * ```\n */\nexport default class KeyedDebouncer<K = string> {\n readonly #debouncers = new Map<K, Debouncer>();\n readonly #delayMs: number;\n readonly #onSchedule: (key: K) => void;\n readonly #onStart: (key: K) => void;\n readonly #onSuccess: (key: K) => void;\n readonly #onCancel: (key: K) => void;\n readonly #onError: (error: unknown, key: K) => void;\n readonly #onIdle: (key: K) => void;\n\n constructor(options: KeyedDebouncerOptions<K>) {\n assertValidDelay(options.delayMs);\n this.#delayMs = options.delayMs;\n this.#onSchedule = options.onSchedule ?? noop;\n this.#onStart = options.onStart ?? noop;\n this.#onSuccess = options.onSuccess ?? noop;\n this.#onCancel = options.onCancel ?? noop;\n this.#onError = options.onError ?? console.error;\n this.#onIdle = options.onIdle ?? noop;\n }\n\n /** Number of keys with a task waiting or running. */\n get size(): number {\n return this.#debouncers.size;\n }\n\n /**\n * Schedule `task` for `key`, cancelling any previous task of that key.\n *\n * @param key - The key whose task is replaced.\n * @param task - The task to run once the delay elapses.\n * @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.\n */\n schedule(key: K, task: DebouncedTask, delayMs?: number): void {\n const existing = this.#debouncers.get(key);\n if (existing !== undefined) {\n existing.schedule(task, delayMs);\n return;\n }\n\n // Validate first so an invalid delay never registers the key.\n assertValidDelay(delayMs ?? this.#delayMs);\n const debouncer = this.#create(key);\n this.#debouncers.set(key, debouncer);\n debouncer.schedule(task, delayMs);\n }\n\n /** Cancel the task of `key`, if any. */\n cancel(key: K): void {\n this.#debouncers.get(key)?.cancel();\n }\n\n /** Cancel every task. Useful when shutting the process down. */\n cancelAll(): void {\n // Copy first: cancelling removes the debouncer from the map.\n for (const debouncer of [...this.#debouncers.values()]) {\n debouncer.cancel();\n }\n }\n\n /** Whether a task is waiting or running for `key`. */\n isPending(key: K): boolean {\n return this.#debouncers.has(key);\n }\n\n #create(key: K): Debouncer {\n const debouncer: Debouncer = new Debouncer({\n delayMs: this.#delayMs,\n onSchedule: () => this.#onSchedule(key),\n onStart: () => this.#onStart(key),\n onSuccess: () => this.#onSuccess(key),\n onCancel: () => this.#onCancel(key),\n onError: (error) => this.#onError(error, key),\n onIdle: () => {\n if (this.#debouncers.get(key) === debouncer) {\n this.#debouncers.delete(key);\n this.#onIdle(key);\n }\n },\n });\n return debouncer;\n }\n}\n"]}
|
package/dist/web/lazy.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a lazy value that is created only when it is accessed for the first time.
|
|
3
|
-
* @template T The type of the lazy value.
|
|
4
|
-
*/
|
|
5
|
-
export default class Lazy {
|
|
6
|
-
constructor(getter) {
|
|
7
|
-
this._valueCreated = false;
|
|
8
|
-
this._value = null;
|
|
9
|
-
this._factoryMethod = getter;
|
|
10
|
-
}
|
|
11
|
-
get valueCreated() {
|
|
12
|
-
return this._valueCreated;
|
|
13
|
-
}
|
|
14
|
-
get value() {
|
|
15
|
-
if (!this._valueCreated) {
|
|
16
|
-
this._value = this._factoryMethod();
|
|
17
|
-
this._valueCreated = true;
|
|
18
|
-
}
|
|
19
|
-
return this._value;
|
|
20
|
-
}
|
|
21
|
-
reset() {
|
|
22
|
-
this._valueCreated = false;
|
|
23
|
-
this._value = null;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=lazy.js.map
|
package/dist/web/lazy.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../../src/lazy.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,IAAI;IAKrB,YAAY,MAAmB;QAJvB,kBAAa,GAAG,KAAK,CAAC;QACtB,WAAM,GAAgB,IAAI,CAAC;QAI/B,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC;IACjC,CAAC;IAED,IAAI,YAAY;QACZ,OAAO,IAAI,CAAC,aAAa,CAAC;IAC9B,CAAC;IAED,IAAW,KAAK;QACZ,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YACpC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC9B,CAAC;QACD,OAAO,IAAI,CAAC,MAAW,CAAC;IAC5B,CAAC;IAED,KAAK;QACD,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,CAAC;CACJ","sourcesContent":["import type {Nullable, Supplier} from \"./types.js\";\n\n\n/**\n * Represents a lazy value that is created only when it is accessed for the first time.\n * @template T The type of the lazy value.\n */\nexport default class Lazy<T> {\n private _valueCreated = false;\n private _value: Nullable<T> = null;\n private readonly _factoryMethod: Supplier<T>;\n\n constructor(getter: Supplier<T>) {\n this._factoryMethod = getter;\n }\n\n get valueCreated(): boolean {\n return this._valueCreated;\n }\n\n public get value(): T {\n if (!this._valueCreated) {\n this._value = this._factoryMethod();\n this._valueCreated = true;\n }\n return this._value as T;\n }\n\n reset(): void {\n this._valueCreated = false;\n this._value = null;\n }\n}"]}
|
package/dist/web/math.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Linearly interpolates a number between two ranges.
|
|
3
|
-
*
|
|
4
|
-
* @param {number} n - The number to interpolate.
|
|
5
|
-
* @param {number} inMin - The minimum value of the input range.
|
|
6
|
-
* @param {number} inMax - The maximum value of the input range.
|
|
7
|
-
* @param {number} outMin - The minimum value of the output range.
|
|
8
|
-
* @param {number} outMax - The maximum value of the output range.
|
|
9
|
-
*
|
|
10
|
-
* @return {number} - The interpolated value.
|
|
11
|
-
*/
|
|
12
|
-
export function lerp(n, inMin, inMax, outMin, outMax) {
|
|
13
|
-
if (inMax === inMin) {
|
|
14
|
-
throw new Error("Input range cannot be zero");
|
|
15
|
-
}
|
|
16
|
-
return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Clamps a number between a minimum and maximum value.
|
|
20
|
-
*
|
|
21
|
-
* @param {number} n - The value to be clamped.
|
|
22
|
-
* @param {number} min - The minimum value.
|
|
23
|
-
* @param {number} max - The maximum value.
|
|
24
|
-
* @return {number} The clamped value.
|
|
25
|
-
*/
|
|
26
|
-
export function clamp(n, min, max) {
|
|
27
|
-
if (n <= min)
|
|
28
|
-
return min;
|
|
29
|
-
if (n >= max)
|
|
30
|
-
return max;
|
|
31
|
-
return n;
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=math.js.map
|
package/dist/web/math.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"math.js","sourceRoot":"","sources":["../../src/math.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,IAAI,CAAC,CAAS,EAAE,KAAa,EAAE,KAAa,EAAE,MAAc,EAAE,MAAc;IACxF,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAA;AACvE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,KAAK,CAAC,CAAS,EAAE,GAAW,EAAE,GAAW;IACrD,IAAI,CAAC,IAAI,GAAG;QACR,OAAO,GAAG,CAAC;IACf,IAAI,CAAC,IAAI,GAAG;QACR,OAAO,GAAG,CAAC;IACf,OAAO,CAAC,CAAA;AACZ,CAAC","sourcesContent":["/**\n * Linearly interpolates a number between two ranges.\n *\n * @param {number} n - The number to interpolate.\n * @param {number} inMin - The minimum value of the input range.\n * @param {number} inMax - The maximum value of the input range.\n * @param {number} outMin - The minimum value of the output range.\n * @param {number} outMax - The maximum value of the output range.\n *\n * @return {number} - The interpolated value.\n */\nexport function lerp(n: number, inMin: number, inMax: number, outMin: number, outMax: number): number {\n if (inMax === inMin) {\n throw new Error(\"Input range cannot be zero\");\n }\n return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin))\n}\n\n/**\n * Clamps a number between a minimum and maximum value.\n *\n * @param {number} n - The value to be clamped.\n * @param {number} min - The minimum value.\n * @param {number} max - The maximum value.\n * @return {number} The clamped value.\n */\nexport function clamp(n: number, min: number, max: number): number {\n if (n <= min)\n return min;\n if (n >= max)\n return max;\n return n\n}\n"]}
|
package/dist/web/order.js
DELETED
package/dist/web/order.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","sourceRoot":"","sources":["../../src/order.ts"],"names":[],"mappings":"AAAA,IAAK,KAGJ;AAHD,WAAK,KAAK;IACN,0BAAiB,CAAA;IACjB,4BAAmB,CAAA;AACvB,CAAC,EAHI,KAAK,KAAL,KAAK,QAGT;AAED,eAAe,KAAK,CAAC","sourcesContent":["enum Order {\n Ascending = \"asc\",\n Descending = \"desc\"\n}\n\nexport default Order;"]}
|