@develia/commons 0.4.15 → 0.4.18
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/cjs/array.js +38 -2
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +26 -11
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +35 -34
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +1 -1
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/native-bridge.js +3 -1
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js +34 -13
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +26 -3
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +3 -3
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/collections/from.js +3 -3
- package/dist/cjs/collections/from.js.map +1 -1
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +46 -11
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -1
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/functions.js +348 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +3 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +10 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/range.js +154 -0
- package/dist/cjs/range.js.map +1 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +8 -5
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +1 -1
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +21 -8
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +20 -19
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +175 -39
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +36 -2
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +26 -11
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +35 -34
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +1 -1
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/native-bridge.js +3 -1
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js +34 -13
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +26 -3
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +3 -3
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/collections/from.js +3 -3
- package/dist/esm/collections/from.js.map +1 -1
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +46 -11
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -1
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/functions.js +336 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +3 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +10 -1
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/range.js +151 -0
- package/dist/esm/range.js.map +1 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +8 -5
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +1 -1
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +21 -8
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +20 -19
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +174 -39
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +29 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +2 -2
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/cancellable-promise.d.ts.map +1 -1
- package/dist/types/clean-url-search-params.d.ts +3 -1
- package/dist/types/clean-url-search-params.d.ts.map +1 -1
- package/dist/types/collections/from.d.ts +1 -1
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/functions.d.ts +231 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/parse.d.ts.map +1 -1
- package/dist/types/range.d.ts +72 -0
- package/dist/types/range.d.ts.map +1 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +1 -1
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type.d.ts +2 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +21 -3
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +36 -2
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +26 -11
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +35 -34
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +1 -1
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/native-bridge.js +3 -1
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js +34 -13
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +26 -3
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +3 -3
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/collections/from.js +3 -3
- package/dist/web/collections/from.js.map +1 -1
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +46 -11
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -1
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/functions.js +336 -0
- package/dist/web/functions.js.map +1 -0
- package/dist/web/index.js +2 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +3 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +10 -1
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js.map +1 -1
- package/dist/web/range.js +151 -0
- package/dist/web/range.js.map +1 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +8 -5
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +1 -1
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +21 -8
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +20 -19
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +174 -39
- package/dist/web/utilities.js.map +1 -1
- package/package.json +66 -60
- package/src/array.ts +116 -68
- package/src/async-pool-executor.ts +107 -85
- package/src/browser/form-utilities.ts +32 -31
- package/src/browser/geo-location.ts +113 -113
- package/src/browser/native-bridge.ts +118 -116
- package/src/cancellable-promise.ts +102 -76
- package/src/clean-url-search-params.ts +36 -11
- package/src/collections/cache-dictionary.ts +115 -115
- package/src/collections/default-map.ts +48 -48
- package/src/collections/default-weak-map.ts +51 -51
- package/src/collections/from.ts +859 -859
- package/src/collections/index.ts +3 -3
- package/src/draft.ts +55 -16
- package/src/event-bus.ts +83 -83
- package/src/functions.ts +496 -0
- package/src/index.ts +44 -42
- package/src/key-value-pair.ts +24 -24
- package/src/lazy.ts +32 -32
- package/src/math.ts +33 -30
- package/src/order.ts +5 -5
- package/src/parse.ts +251 -240
- package/src/patch.ts +79 -79
- package/src/range.ts +165 -0
- package/src/stopwatch.ts +43 -43
- package/src/string.ts +8 -5
- package/src/timer.ts +67 -67
- package/src/timespan.ts +412 -398
- package/src/type-code.ts +15 -15
- package/src/type.ts +372 -371
- package/src/types.ts +30 -30
- package/src/utilities.ts +653 -480
package/src/functions.ts
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
type AnyFunction = (this: any, ...args: any[]) => any;
|
|
2
|
+
type TimerHandle = ReturnType<typeof setTimeout>;
|
|
3
|
+
|
|
4
|
+
/** Options that control when a debounced function is executed. */
|
|
5
|
+
export interface DebounceOptions {
|
|
6
|
+
/** Execute on the first call in a debounce window. Defaults to false. */
|
|
7
|
+
leading?: boolean;
|
|
8
|
+
/** Execute after the debounce window using the latest arguments. Defaults to true. */
|
|
9
|
+
trailing?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Options that control when a throttled function is executed. */
|
|
13
|
+
export interface ThrottleOptions {
|
|
14
|
+
/** Execute immediately on the first call in a throttle window. Defaults to true. */
|
|
15
|
+
leading?: boolean;
|
|
16
|
+
/** Execute once more at the end of the throttle window using the latest arguments. Defaults to true. */
|
|
17
|
+
trailing?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A delayed function returned by {@link debounce} or {@link throttle}.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const saveLater = debounce(save, 250);
|
|
26
|
+
* saveLater();
|
|
27
|
+
*
|
|
28
|
+
* if (saveLater.pending()) {
|
|
29
|
+
* saveLater.flush();
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type DeferredFunction<F extends AnyFunction> = ((
|
|
34
|
+
this: ThisParameterType<F>,
|
|
35
|
+
...args: Parameters<F>
|
|
36
|
+
) => ReturnType<F> | undefined) & {
|
|
37
|
+
/** Cancel the scheduled call and forget the latest arguments. */
|
|
38
|
+
cancel(): void;
|
|
39
|
+
/** Execute the scheduled trailing call immediately, when one exists. */
|
|
40
|
+
flush(): ReturnType<F> | undefined;
|
|
41
|
+
/** Return true while a trailing call is scheduled. */
|
|
42
|
+
pending(): boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A memoized function with helpers for inspecting and invalidating its cache.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const getUser = memoize((id: string) => fetchUser(id));
|
|
51
|
+
*
|
|
52
|
+
* getUser("42");
|
|
53
|
+
* getUser.has("42"); // true
|
|
54
|
+
* getUser.delete("42");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export type MemoizedFunction<F extends AnyFunction, K> = ((
|
|
58
|
+
this: ThisParameterType<F>,
|
|
59
|
+
...args: Parameters<F>
|
|
60
|
+
) => ReturnType<F>) & {
|
|
61
|
+
/** The backing cache used by the memoized function. */
|
|
62
|
+
readonly cache: Map<K, ReturnType<F>>;
|
|
63
|
+
/** Remove every cached value. */
|
|
64
|
+
clear(): void;
|
|
65
|
+
/** Remove the cached value for the key resolved from the provided arguments. */
|
|
66
|
+
delete(...args: Parameters<F>): boolean;
|
|
67
|
+
/** Return true when a cached value exists for the key resolved from the provided arguments. */
|
|
68
|
+
has(...args: Parameters<F>): boolean;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type Unary<T, R> = (arg: T) => R;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Return the received value unchanged.
|
|
75
|
+
* Useful as a default mapper, placeholder callback, or neutral transform in a pipeline.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* identity(5); // 5
|
|
80
|
+
*
|
|
81
|
+
* const value = {name: "commons"};
|
|
82
|
+
* identity(value) === value; // true
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function identity<T>(value: T): T {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Create a function that always returns the same value.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const getAnswer = constant(42);
|
|
95
|
+
*
|
|
96
|
+
* getAnswer(); // 42
|
|
97
|
+
* getAnswer(); // 42
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export function constant<T>(value: T): () => T {
|
|
101
|
+
return () => value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Do nothing and return undefined.
|
|
106
|
+
* Useful as a safe default callback.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const onComplete = options.onComplete ?? noop;
|
|
111
|
+
* onComplete();
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function noop(): void {
|
|
115
|
+
// Intentionally empty.
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create a function that can be executed only once.
|
|
120
|
+
* Later calls return the result from the first execution without calling the original function again.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const initialize = once(() => ({connected: true}));
|
|
125
|
+
*
|
|
126
|
+
* const first = initialize();
|
|
127
|
+
* const second = initialize();
|
|
128
|
+
* first === second; // true
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function once<F extends AnyFunction>(fn: F): F {
|
|
132
|
+
let called = false;
|
|
133
|
+
let result: ReturnType<F>;
|
|
134
|
+
|
|
135
|
+
return function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> {
|
|
136
|
+
if (!called) {
|
|
137
|
+
called = true;
|
|
138
|
+
result = fn.apply(this, args);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
} as F;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Cache the result of a function and reuse it for calls with the same resolved key.
|
|
146
|
+
* By default, the first argument is used as the cache key.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const double = memoize((value: number) => value * 2);
|
|
151
|
+
*
|
|
152
|
+
* double(4); // 8, computed
|
|
153
|
+
* double(4); // 8, read from cache
|
|
154
|
+
* double.has(4); // true
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* const sum = memoize(
|
|
160
|
+
* (left: number, right: number) => left + right,
|
|
161
|
+
* (left, right) => `${left}:${right}`,
|
|
162
|
+
* );
|
|
163
|
+
*
|
|
164
|
+
* sum(2, 3); // 5
|
|
165
|
+
* sum.delete(2, 3); // true
|
|
166
|
+
* sum.clear();
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export function memoize<F extends AnyFunction, K = Parameters<F>[0]>(
|
|
170
|
+
fn: F,
|
|
171
|
+
resolver?: (...args: Parameters<F>) => K,
|
|
172
|
+
cache: Map<K, ReturnType<F>> = new Map<K, ReturnType<F>>()
|
|
173
|
+
): MemoizedFunction<F, K> {
|
|
174
|
+
const resolveKey = (...args: Parameters<F>): K => {
|
|
175
|
+
if (resolver) return resolver(...args);
|
|
176
|
+
return args[0] as K;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const memoized = function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> {
|
|
180
|
+
const key = resolveKey(...args);
|
|
181
|
+
if (cache.has(key)) {
|
|
182
|
+
return cache.get(key)!;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const value = fn.apply(this, args);
|
|
186
|
+
cache.set(key, value);
|
|
187
|
+
return value;
|
|
188
|
+
} as MemoizedFunction<F, K>;
|
|
189
|
+
|
|
190
|
+
Object.defineProperty(memoized, "cache", {
|
|
191
|
+
value: cache,
|
|
192
|
+
enumerable: false,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
memoized.clear = () => cache.clear();
|
|
196
|
+
memoized.delete = (...args: Parameters<F>) => cache.delete(resolveKey(...args));
|
|
197
|
+
memoized.has = (...args: Parameters<F>) => cache.has(resolveKey(...args));
|
|
198
|
+
|
|
199
|
+
return memoized;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Delay a function until no calls have been made for the configured wait time.
|
|
204
|
+
* The latest arguments and this-context are used for the trailing execution.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* const search = debounce((query: string) => {
|
|
209
|
+
* console.log(query);
|
|
210
|
+
* }, 300);
|
|
211
|
+
*
|
|
212
|
+
* search("com");
|
|
213
|
+
* search("commons"); // only this value is printed after 300 ms
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* const track = debounce(sendAnalytics, 1000, {leading: true, trailing: false});
|
|
219
|
+
*
|
|
220
|
+
* track("open"); // executed immediately
|
|
221
|
+
* track.cancel(); // cancel the pending debounce window
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export function debounce<F extends AnyFunction>(
|
|
225
|
+
fn: F,
|
|
226
|
+
wait: number,
|
|
227
|
+
options: DebounceOptions = {}
|
|
228
|
+
): DeferredFunction<F> {
|
|
229
|
+
const delay = normalizeDelay(wait);
|
|
230
|
+
const leading = options.leading ?? false;
|
|
231
|
+
const trailing = options.trailing ?? true;
|
|
232
|
+
|
|
233
|
+
let timer: TimerHandle | undefined;
|
|
234
|
+
let lastArgs: Parameters<F> | undefined;
|
|
235
|
+
let lastThis: ThisParameterType<F> | undefined;
|
|
236
|
+
let result: ReturnType<F> | undefined;
|
|
237
|
+
|
|
238
|
+
const invoke = (): ReturnType<F> => {
|
|
239
|
+
const args = lastArgs!;
|
|
240
|
+
const context = lastThis!;
|
|
241
|
+
lastArgs = undefined;
|
|
242
|
+
lastThis = undefined;
|
|
243
|
+
result = fn.apply(context, args);
|
|
244
|
+
return result as ReturnType<F>;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const expire = (): void => {
|
|
248
|
+
timer = undefined;
|
|
249
|
+
if (trailing && lastArgs) {
|
|
250
|
+
invoke();
|
|
251
|
+
} else {
|
|
252
|
+
lastArgs = undefined;
|
|
253
|
+
lastThis = undefined;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const start = (): void => {
|
|
258
|
+
timer = setTimeout(expire, delay);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const debounced = function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> | undefined {
|
|
262
|
+
const invokeLeading = leading && timer === undefined;
|
|
263
|
+
lastArgs = args;
|
|
264
|
+
lastThis = this;
|
|
265
|
+
|
|
266
|
+
if (timer !== undefined) {
|
|
267
|
+
clearTimeout(timer);
|
|
268
|
+
}
|
|
269
|
+
start();
|
|
270
|
+
|
|
271
|
+
if (invokeLeading) {
|
|
272
|
+
return invoke();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return result;
|
|
276
|
+
} as DeferredFunction<F>;
|
|
277
|
+
|
|
278
|
+
debounced.cancel = (): void => {
|
|
279
|
+
if (timer !== undefined) {
|
|
280
|
+
clearTimeout(timer);
|
|
281
|
+
}
|
|
282
|
+
timer = undefined;
|
|
283
|
+
lastArgs = undefined;
|
|
284
|
+
lastThis = undefined;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
debounced.flush = (): ReturnType<F> | undefined => {
|
|
288
|
+
if (timer !== undefined) {
|
|
289
|
+
clearTimeout(timer);
|
|
290
|
+
timer = undefined;
|
|
291
|
+
if (lastArgs && trailing) {
|
|
292
|
+
return invoke();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return result;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
debounced.pending = (): boolean => timer !== undefined;
|
|
299
|
+
|
|
300
|
+
return debounced;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Limit how often a function can run during a time window.
|
|
305
|
+
* Calls made while the function is throttled can optionally be replayed once at the end with the latest arguments.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* const onScroll = throttle(() => {
|
|
310
|
+
* console.log(window.scrollY);
|
|
311
|
+
* }, 100);
|
|
312
|
+
*
|
|
313
|
+
* window.addEventListener("scroll", onScroll);
|
|
314
|
+
* ```
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```ts
|
|
318
|
+
* const refresh = throttle(loadData, 500, {leading: false});
|
|
319
|
+
*
|
|
320
|
+
* refresh(); // scheduled instead of executed immediately
|
|
321
|
+
* refresh.flush(); // execute the scheduled call now
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
export function throttle<F extends AnyFunction>(
|
|
325
|
+
fn: F,
|
|
326
|
+
wait: number,
|
|
327
|
+
options: ThrottleOptions = {}
|
|
328
|
+
): DeferredFunction<F> {
|
|
329
|
+
const delay = normalizeDelay(wait);
|
|
330
|
+
const leading = options.leading ?? true;
|
|
331
|
+
const trailing = options.trailing ?? true;
|
|
332
|
+
|
|
333
|
+
let timer: TimerHandle | undefined;
|
|
334
|
+
let lastArgs: Parameters<F> | undefined;
|
|
335
|
+
let lastThis: ThisParameterType<F> | undefined;
|
|
336
|
+
let lastInvokeTime = 0;
|
|
337
|
+
let hasInvoked = false;
|
|
338
|
+
let result: ReturnType<F> | undefined;
|
|
339
|
+
|
|
340
|
+
const invoke = (time: number): ReturnType<F> => {
|
|
341
|
+
const args = lastArgs!;
|
|
342
|
+
const context = lastThis!;
|
|
343
|
+
lastArgs = undefined;
|
|
344
|
+
lastThis = undefined;
|
|
345
|
+
hasInvoked = true;
|
|
346
|
+
lastInvokeTime = time;
|
|
347
|
+
result = fn.apply(context, args);
|
|
348
|
+
return result as ReturnType<F>;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const expire = (): void => {
|
|
352
|
+
timer = undefined;
|
|
353
|
+
if (trailing && lastArgs) {
|
|
354
|
+
invoke(Date.now());
|
|
355
|
+
} else {
|
|
356
|
+
lastArgs = undefined;
|
|
357
|
+
lastThis = undefined;
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const throttled = function (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F> | undefined {
|
|
362
|
+
const now = Date.now();
|
|
363
|
+
lastArgs = args;
|
|
364
|
+
lastThis = this;
|
|
365
|
+
|
|
366
|
+
if (!hasInvoked) {
|
|
367
|
+
if (leading) {
|
|
368
|
+
return invoke(now);
|
|
369
|
+
}
|
|
370
|
+
hasInvoked = true;
|
|
371
|
+
lastInvokeTime = now;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const remaining = delay - (now - lastInvokeTime);
|
|
375
|
+
if (remaining <= 0 || remaining > delay) {
|
|
376
|
+
if (timer !== undefined) {
|
|
377
|
+
clearTimeout(timer);
|
|
378
|
+
timer = undefined;
|
|
379
|
+
}
|
|
380
|
+
return invoke(now);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (timer === undefined && trailing) {
|
|
384
|
+
timer = setTimeout(expire, remaining);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return result;
|
|
388
|
+
} as DeferredFunction<F>;
|
|
389
|
+
|
|
390
|
+
throttled.cancel = (): void => {
|
|
391
|
+
if (timer !== undefined) {
|
|
392
|
+
clearTimeout(timer);
|
|
393
|
+
}
|
|
394
|
+
timer = undefined;
|
|
395
|
+
lastArgs = undefined;
|
|
396
|
+
lastThis = undefined;
|
|
397
|
+
lastInvokeTime = 0;
|
|
398
|
+
hasInvoked = false;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
throttled.flush = (): ReturnType<F> | undefined => {
|
|
402
|
+
if (timer !== undefined) {
|
|
403
|
+
clearTimeout(timer);
|
|
404
|
+
timer = undefined;
|
|
405
|
+
if (lastArgs && trailing) {
|
|
406
|
+
return invoke(Date.now());
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return result;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
throttled.pending = (): boolean => timer !== undefined;
|
|
413
|
+
|
|
414
|
+
return throttled;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Create a function with one or more arguments already provided.
|
|
419
|
+
* Later arguments are appended after the preset arguments.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* const sum = (a: number, b: number, c: number) => a + b + c;
|
|
424
|
+
* const addFive = partial(sum, 2, 3);
|
|
425
|
+
*
|
|
426
|
+
* addFive(4); // 9
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
export function partial<F extends AnyFunction>(
|
|
430
|
+
fn: F,
|
|
431
|
+
...presetArgs: unknown[]
|
|
432
|
+
): (...laterArgs: unknown[]) => ReturnType<F> {
|
|
433
|
+
return function (this: ThisParameterType<F>, ...laterArgs: unknown[]): ReturnType<F> {
|
|
434
|
+
return fn.apply(this, [...presetArgs, ...laterArgs] as Parameters<F>);
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Compose unary functions from left to right.
|
|
440
|
+
* The first function receives the original arguments, and each next function receives the previous result.
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* ```ts
|
|
444
|
+
* const transform = pipe(
|
|
445
|
+
* (value: number) => value + 1,
|
|
446
|
+
* (value: number) => value * 2,
|
|
447
|
+
* );
|
|
448
|
+
*
|
|
449
|
+
* transform(2); // 6
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
export function pipe<A, B>(ab: Unary<A, B>): Unary<A, B>;
|
|
453
|
+
export function pipe<A, B, C>(ab: Unary<A, B>, bc: Unary<B, C>): Unary<A, C>;
|
|
454
|
+
export function pipe<A, B, C, D>(ab: Unary<A, B>, bc: Unary<B, C>, cd: Unary<C, D>): Unary<A, D>;
|
|
455
|
+
export 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>;
|
|
456
|
+
export function pipe(...fns: AnyFunction[]): AnyFunction {
|
|
457
|
+
return createFlow(fns);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Compose unary functions from right to left.
|
|
462
|
+
* The rightmost function receives the original arguments, and each function to its left receives the previous result.
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```ts
|
|
466
|
+
* const transform = compose(
|
|
467
|
+
* (value: number) => value + 1,
|
|
468
|
+
* (value: number) => value * 2,
|
|
469
|
+
* );
|
|
470
|
+
*
|
|
471
|
+
* transform(2); // 5
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
export function compose<A, B>(ab: Unary<A, B>): Unary<A, B>;
|
|
475
|
+
export function compose<A, B, C>(bc: Unary<B, C>, ab: Unary<A, B>): Unary<A, C>;
|
|
476
|
+
export function compose<A, B, C, D>(cd: Unary<C, D>, bc: Unary<B, C>, ab: Unary<A, B>): Unary<A, D>;
|
|
477
|
+
export 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>;
|
|
478
|
+
export function compose(...fns: AnyFunction[]): AnyFunction {
|
|
479
|
+
return createFlow(fns.slice().reverse());
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function createFlow(fns: AnyFunction[]): AnyFunction {
|
|
483
|
+
if (fns.length === 0) return identity;
|
|
484
|
+
|
|
485
|
+
return function (this: any, ...args: any[]): unknown {
|
|
486
|
+
let result = fns[0].apply(this, args);
|
|
487
|
+
for (let i = 1; i < fns.length; i++) {
|
|
488
|
+
result = fns[i].call(this, result);
|
|
489
|
+
}
|
|
490
|
+
return result;
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function normalizeDelay(wait: number): number {
|
|
495
|
+
return Number.isFinite(wait) ? Math.max(0, wait) : 0;
|
|
496
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,43 +1,45 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
|
|
3
|
-
// ─── Types ───────────────────────────────────────────
|
|
4
|
-
export * from './types.js';
|
|
5
|
-
export {default as Type} from './type.js';
|
|
6
|
-
export {default as TypeCode} from './type-code.js';
|
|
7
|
-
|
|
8
|
-
// ─── Utilities ───────────────────────────────────────
|
|
9
|
-
export * from './utilities.js';
|
|
10
|
-
export * from './
|
|
11
|
-
export * from './
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export {default as
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export {default as
|
|
21
|
-
export {default as
|
|
22
|
-
export {default as
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
export {default as
|
|
27
|
-
export {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/** @deprecated */
|
|
1
|
+
// src/index.ts
|
|
2
|
+
|
|
3
|
+
// ─── Types ───────────────────────────────────────────
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
export {default as Type} from './type.js';
|
|
6
|
+
export {default as TypeCode} from './type-code.js';
|
|
7
|
+
|
|
8
|
+
// ─── Utilities ───────────────────────────────────────
|
|
9
|
+
export * from './utilities.js';
|
|
10
|
+
export * from './functions.js';
|
|
11
|
+
export * from './string.js';
|
|
12
|
+
export * from './math.js';
|
|
13
|
+
export {default as CancellablePromise} from "./cancellable-promise.js";
|
|
14
|
+
|
|
15
|
+
// ─── Data Structures ────────────────────────────────
|
|
16
|
+
export {default as KeyValuePair} from './key-value-pair.js';
|
|
17
|
+
export {default as Order} from './order.js';
|
|
18
|
+
|
|
19
|
+
// ─── Async & Timing ─────────────────────────────────
|
|
20
|
+
export {default as Timer} from './timer.js';
|
|
21
|
+
export {default as TimeSpan} from './timespan.js';
|
|
22
|
+
export {default as Stopwatch} from './stopwatch.js';
|
|
23
|
+
export {default as AsyncPoolExecutor} from './async-pool-executor.js';
|
|
24
|
+
|
|
25
|
+
// ─── Patterns ────────────────────────────────────────
|
|
26
|
+
export {default as Lazy} from './lazy.js';
|
|
27
|
+
export {default as from} from './collections/from.js';
|
|
28
|
+
export {EventBus, Event} from './event-bus.js';
|
|
29
|
+
export {default as Range} from './range.js';
|
|
30
|
+
|
|
31
|
+
// ─── Draft ────────────────────────────────────────
|
|
32
|
+
export {default as draft} from './draft.js';
|
|
33
|
+
|
|
34
|
+
// ─── Web ─────────────────────────────────────────────
|
|
35
|
+
export {default as CleanURLSearchParams} from './clean-url-search-params.js';
|
|
36
|
+
/** @deprecated */
|
|
37
|
+
export {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// ─── Deprecated ──────────────────────────────────────
|
|
42
|
+
/** @deprecated */
|
|
43
|
+
export * from './array.js';
|
|
44
|
+
/** @deprecated */
|
|
43
45
|
export * from './collections/index.js';
|
package/src/key-value-pair.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
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<TKey, TValue> {
|
|
8
|
-
get value(): TValue {
|
|
9
|
-
return this._value;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get key(): TKey {
|
|
13
|
-
return this._key;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
private readonly _key: TKey;
|
|
17
|
-
private readonly _value: TValue;
|
|
18
|
-
|
|
19
|
-
constructor(key: TKey, value: TValue) {
|
|
20
|
-
this._key = key;
|
|
21
|
-
this._value = value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
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<TKey, TValue> {
|
|
8
|
+
get value(): TValue {
|
|
9
|
+
return this._value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get key(): TKey {
|
|
13
|
+
return this._key;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private readonly _key: TKey;
|
|
17
|
+
private readonly _value: TValue;
|
|
18
|
+
|
|
19
|
+
constructor(key: TKey, value: TValue) {
|
|
20
|
+
this._key = key;
|
|
21
|
+
this._value = value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
25
|
}
|
package/src/lazy.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import type {Nullable, Supplier} from "./types.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Represents a lazy value that is created only when it is accessed for the first time.
|
|
6
|
-
* @template T The type of the lazy value.
|
|
7
|
-
*/
|
|
8
|
-
export default class Lazy<T> {
|
|
9
|
-
private _valueCreated = false;
|
|
10
|
-
private _value: Nullable<T> = null;
|
|
11
|
-
private readonly _factoryMethod: Supplier<T>;
|
|
12
|
-
|
|
13
|
-
constructor(getter: Supplier<T>) {
|
|
14
|
-
this._factoryMethod = getter;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get valueCreated(): boolean {
|
|
18
|
-
return this._valueCreated;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public get value(): T {
|
|
22
|
-
if (!this._valueCreated) {
|
|
23
|
-
this._value = this._factoryMethod();
|
|
24
|
-
this._valueCreated = true;
|
|
25
|
-
}
|
|
26
|
-
return this._value as T;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
reset(): void {
|
|
30
|
-
this._valueCreated = false;
|
|
31
|
-
this._value = null;
|
|
32
|
-
}
|
|
1
|
+
import type {Nullable, Supplier} from "./types.js";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a lazy value that is created only when it is accessed for the first time.
|
|
6
|
+
* @template T The type of the lazy value.
|
|
7
|
+
*/
|
|
8
|
+
export default class Lazy<T> {
|
|
9
|
+
private _valueCreated = false;
|
|
10
|
+
private _value: Nullable<T> = null;
|
|
11
|
+
private readonly _factoryMethod: Supplier<T>;
|
|
12
|
+
|
|
13
|
+
constructor(getter: Supplier<T>) {
|
|
14
|
+
this._factoryMethod = getter;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get valueCreated(): boolean {
|
|
18
|
+
return this._valueCreated;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public get value(): T {
|
|
22
|
+
if (!this._valueCreated) {
|
|
23
|
+
this._value = this._factoryMethod();
|
|
24
|
+
this._valueCreated = true;
|
|
25
|
+
}
|
|
26
|
+
return this._value as T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
reset(): void {
|
|
30
|
+
this._valueCreated = false;
|
|
31
|
+
this._value = null;
|
|
32
|
+
}
|
|
33
33
|
}
|