moderndash 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +52 -93
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +196 -73
- package/dist/index.js +49 -93
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ type MinimumTwoArrays<TInput> = [TInput[], TInput[], ...TInput[][]];
|
|
|
41
41
|
* @description Generic function type, should fit any function
|
|
42
42
|
* @typeParam TFunc - The input function type
|
|
43
43
|
*/
|
|
44
|
-
type GenericFunction<TFunc extends (...args: any) =>
|
|
44
|
+
type GenericFunction<TFunc extends (...args: any) => void> = (...args: Parameters<TFunc>) => ReturnType<TFunc>;
|
|
45
45
|
type PlainObject$1 = Record<PropertyKey, unknown>;
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -260,6 +260,136 @@ declare function takeWhile<TArr>(array: TArr[], predicate: (elem: TArr) => boole
|
|
|
260
260
|
*/
|
|
261
261
|
declare function unique<TInput>(array: TInput[], compareFn?: (a: TInput, b: TInput) => boolean): TInput[];
|
|
262
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Debouces the decorated function. Only calling it after a specified amount of time has passed without any new calls.
|
|
265
|
+
*
|
|
266
|
+
* Look at {@link debounce} for the non-decorator version.
|
|
267
|
+
*
|
|
268
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* class TestClass {
|
|
272
|
+
* @decDebounce(1000)
|
|
273
|
+
* testMethod(str: string) {
|
|
274
|
+
* console.log("Debounced:", str);
|
|
275
|
+
* }
|
|
276
|
+
* }
|
|
277
|
+
*
|
|
278
|
+
* const instance = new TestClass();
|
|
279
|
+
* instance.testMethod("Hello");
|
|
280
|
+
* instance.testMethod("World");
|
|
281
|
+
* // => Only the second invocation of `debouncedSayHello` is executed, after a delay of 1000ms.
|
|
282
|
+
*
|
|
283
|
+
* @param wait - Milliseconds to wait before invoking the decorated function after the last invocation.
|
|
284
|
+
*/
|
|
285
|
+
declare const decDebounce: (wait: number) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Only invokes the decorated function as long as it's called `<= n` times.
|
|
289
|
+
* Subsequent calls to the decorated function return the result of the last invocation.
|
|
290
|
+
*
|
|
291
|
+
* Look at {@link maxCalls} for the non-decorator version.
|
|
292
|
+
*
|
|
293
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* class TestClass {
|
|
297
|
+
* private count = 0;
|
|
298
|
+
* @decMaxCalls(2)
|
|
299
|
+
* testMethod() {
|
|
300
|
+
* return ++this.count;
|
|
301
|
+
* }
|
|
302
|
+
* }
|
|
303
|
+
* const instance = new TestClass();
|
|
304
|
+
* instance.testMethod(); // => 1
|
|
305
|
+
* instance.testMethod(); // => 2
|
|
306
|
+
* instance.testMethod(); // => 2
|
|
307
|
+
*
|
|
308
|
+
* @param n - The number of calls before the cached result is returned.
|
|
309
|
+
*/
|
|
310
|
+
declare const decMaxCalls: (n: number) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Memoizes the decorated function.
|
|
314
|
+
* The cache key is either determined by the provided resolver or by the arguments used in the memoized function.
|
|
315
|
+
*
|
|
316
|
+
* **Options:**
|
|
317
|
+
* - `resolver` A function that determines the cache key for storing the result based on the arguments provided.
|
|
318
|
+
* - `ttl` sets the time to live for the cache in milliseconds. After `ttl` milliseconds, the next call to the memoized function will result in a cache miss.
|
|
319
|
+
*
|
|
320
|
+
* Look at {@link memoize} for the non-decorator version.
|
|
321
|
+
*
|
|
322
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* class TestClass {
|
|
326
|
+
* @decMemoize({ ttl: 1000 })
|
|
327
|
+
* testMethod(a: number, b: number) {
|
|
328
|
+
* return a + b;
|
|
329
|
+
* }
|
|
330
|
+
* }
|
|
331
|
+
* const instance = new TestClass();
|
|
332
|
+
* instance.testMethod(1, 2); // => 3
|
|
333
|
+
* instance.testMethod(1, 2); // => 3 (cached)
|
|
334
|
+
*
|
|
335
|
+
* // After 1 second:
|
|
336
|
+
* instance.testMethod(1, 2); // => 3 (cache miss)
|
|
337
|
+
*
|
|
338
|
+
*
|
|
339
|
+
* @param options - The options object.
|
|
340
|
+
* @param options.resolver - A function that determines the cache key for storing the result based on the arguments provided.
|
|
341
|
+
* @param options.ttl - The time to live for the cache in milliseconds.
|
|
342
|
+
*/
|
|
343
|
+
declare const decMemoize: (options?: {
|
|
344
|
+
resolver?: ((...args: unknown[]) => string | symbol) | undefined;
|
|
345
|
+
ttl?: number | undefined;
|
|
346
|
+
} | undefined) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Only invokes the decorated function after it's called more than `n` times.
|
|
350
|
+
*
|
|
351
|
+
* Look at {@link minCalls} for the non-decorator version.
|
|
352
|
+
*
|
|
353
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
354
|
+
* @example
|
|
355
|
+
* class TestClass {
|
|
356
|
+
* @decAfter(2)
|
|
357
|
+
* testMethod() {
|
|
358
|
+
* return 1;
|
|
359
|
+
* }
|
|
360
|
+
* }
|
|
361
|
+
* const instance = new TestClass();
|
|
362
|
+
* instance.testMethod(); // => undefined
|
|
363
|
+
* instance.testMethod(); // => undefined
|
|
364
|
+
* instance.testMethod(); // => 1
|
|
365
|
+
*
|
|
366
|
+
* @param n The number of calls before the decorated function is invoked.
|
|
367
|
+
*/
|
|
368
|
+
declare const decMinCalls: (n: number) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The decorated function is invoked at most once per every `wait` milliseconds.
|
|
372
|
+
*
|
|
373
|
+
* Look at {@link throttle} for the non-decorator version.
|
|
374
|
+
*
|
|
375
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* class TestClass {
|
|
379
|
+
* @decThrottle(1000)
|
|
380
|
+
* testMethod() {
|
|
381
|
+
* console.log("Throttled!");
|
|
382
|
+
* }
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* const instance = new TestClass();
|
|
386
|
+
* instance.testMethod(); // => "Throttled!" is logged once per second.
|
|
387
|
+
* instance.testMethod(); // nothing happens
|
|
388
|
+
*
|
|
389
|
+
* @param wait - The number of milliseconds to wait between invocations.
|
|
390
|
+
*/
|
|
391
|
+
declare const decThrottle: (wait: number) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
392
|
+
|
|
263
393
|
type Tail<T extends unknown[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
|
|
264
394
|
/**
|
|
265
395
|
* Transforms a function that takes a function as first argument into a decorator function.
|
|
@@ -290,11 +420,32 @@ type Tail<T extends unknown[]> = T extends [infer _Head, ...infer Tail] ? Tail :
|
|
|
290
420
|
*/
|
|
291
421
|
declare function toDecorator<TFunc extends GenericFunction<TFunc>>(func: TFunc): (...args: Tail<Parameters<TFunc>>) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
292
422
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
423
|
+
/**
|
|
424
|
+
* Creates a debounced version of a function. Only calling it after a specified amount of time has passed without any new calls.
|
|
425
|
+
*
|
|
426
|
+
* ----
|
|
427
|
+
*
|
|
428
|
+
* **Methods:**
|
|
429
|
+
* - `cancel` will cancel the next invocation of the debounced function.
|
|
430
|
+
* - `flush` will immediately invoke the debounced function and cancel any pending invocations.
|
|
431
|
+
*
|
|
432
|
+
* This function can be used as a decorator with {@link decDebounce}.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* const sayHello = (name: string) => console.log(`Hello, ${name}!`);
|
|
436
|
+
* const debouncedSayHello = debounce(sayHello, 1000);
|
|
437
|
+
*
|
|
438
|
+
* debouncedSayHello("John");
|
|
439
|
+
* debouncedSayHello("Jane");
|
|
440
|
+
* // => Only the second invocation of `debouncedSayHello` is executed, after a delay of 1000ms.
|
|
441
|
+
* @param func - The function to debounce.
|
|
442
|
+
* @param wait - The number of milliseconds to wait before invoking `func`.
|
|
443
|
+
* @returns A debounced version of `func` with `cancel` and `flush` methods.
|
|
444
|
+
*/
|
|
445
|
+
declare function debounce<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait: number): TFunc & {
|
|
446
|
+
cancel: () => void;
|
|
447
|
+
flush: () => void;
|
|
448
|
+
};
|
|
298
449
|
|
|
299
450
|
/**
|
|
300
451
|
* Creates a function that invokes the given function as long as it's called `<= n` times.
|
|
@@ -307,7 +458,7 @@ declare function debounce<TFunc extends GenericFunction<TFunc>>(fn: TFunc, wait?
|
|
|
307
458
|
* const addCount = () => ++count;
|
|
308
459
|
*
|
|
309
460
|
* // Allow addCount to be invoked twice.
|
|
310
|
-
* const limitAddCount = maxCalls(
|
|
461
|
+
* const limitAddCount = maxCalls(addCount, 2)
|
|
311
462
|
*
|
|
312
463
|
* limitAddCount() // => 1
|
|
313
464
|
* limitAddCount() // => 2
|
|
@@ -318,67 +469,49 @@ declare function debounce<TFunc extends GenericFunction<TFunc>>(fn: TFunc, wait?
|
|
|
318
469
|
* @returns Returns the new restricted function.
|
|
319
470
|
*/
|
|
320
471
|
declare function maxCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): TFunc;
|
|
321
|
-
/**
|
|
322
|
-
* Only invokes the decorated function as long as it's called `<= n` times.
|
|
323
|
-
* Subsequent calls to the decorated function return the result of the last invocation.
|
|
324
|
-
*
|
|
325
|
-
* Look at {@link maxCalls} for the non-decorator version.
|
|
326
|
-
*
|
|
327
|
-
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
328
|
-
*
|
|
329
|
-
* @example
|
|
330
|
-
* class TestClass {
|
|
331
|
-
* private count = 0;
|
|
332
|
-
* @decMaxCalls(2)
|
|
333
|
-
* testMethod() {
|
|
334
|
-
* return ++this.count;
|
|
335
|
-
* }
|
|
336
|
-
* }
|
|
337
|
-
* const instance = new TestClass();
|
|
338
|
-
* instance.testMethod(); // => 1
|
|
339
|
-
* instance.testMethod(); // => 2
|
|
340
|
-
* instance.testMethod(); // => 2
|
|
341
|
-
*
|
|
342
|
-
*/
|
|
343
|
-
declare const decMaxCalls: (n: number) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
344
472
|
|
|
345
473
|
/**
|
|
346
474
|
* Creates a function that memoizes the result of `func`.
|
|
347
|
-
*
|
|
348
|
-
*
|
|
475
|
+
* The cache key is either determined by the provided resolver or by the arguments used in the memoized function.
|
|
476
|
+
*
|
|
477
|
+
* The cache is exposed as the `cache` property on the memoized function.
|
|
478
|
+
* Its creation may be customized by replacing the `memoize.cache` value.
|
|
479
|
+
* The new cache must implement `get` and `set` methods like the built-in `Map` constructors.
|
|
480
|
+
*
|
|
481
|
+
* **Options:**
|
|
482
|
+
* - `resolver` A function that determines the cache key for storing the result based on the arguments provided.
|
|
483
|
+
* - `ttl` sets the time to live for the cache in milliseconds. After `ttl` milliseconds, the next call to the memoized function will result in a cache miss.
|
|
349
484
|
*
|
|
350
|
-
*
|
|
351
|
-
* function. Its creation may be customized by replacing the `memoize.Cache`
|
|
352
|
-
* constructor with one whose instances implement the
|
|
353
|
-
* [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)
|
|
354
|
-
* method interface of `clear`, `delete`, `get`, `has`, and `set`.
|
|
485
|
+
* This function can be used as a decorator with {@link decMemoize}.
|
|
355
486
|
*
|
|
356
487
|
* @example
|
|
357
488
|
* const object = { 'a': 1, 'b': 2 }
|
|
358
489
|
*
|
|
359
|
-
* const values = memoize(values)
|
|
490
|
+
* const values = memoize(Object.values, { ttl: 1000 })
|
|
360
491
|
* values(object)
|
|
361
492
|
* // => [1, 2]
|
|
362
493
|
*
|
|
363
494
|
* values(object)
|
|
364
495
|
* // => [1, 2]
|
|
365
496
|
*
|
|
366
|
-
* object
|
|
367
|
-
*
|
|
368
|
-
* // => [2, 2]
|
|
369
|
-
*
|
|
370
|
-
* // Modify the result cache.
|
|
371
|
-
* values.cache.set(object, ['a', 'b'])
|
|
372
|
-
* values(object)
|
|
373
|
-
* // => ['a', 'b']
|
|
497
|
+
* setTimeout(() => values(object), 1000)
|
|
498
|
+
* // => [1, 2] (cache miss after 1 second)
|
|
374
499
|
*
|
|
375
500
|
* // Replace `memoize.Cache`.
|
|
376
501
|
* memoize.Cache = WeakMap
|
|
502
|
+
*
|
|
503
|
+
* // This is the default way to create cache keys.
|
|
504
|
+
* const defaultResolver = (...args: unknown[]) => JSON.stringify(args);
|
|
377
505
|
* @param func - The function to have its output memoized.
|
|
378
|
-
* @param
|
|
379
|
-
* @
|
|
506
|
+
* @param options - The options object with optional `resolver` and `ttl` parameters.
|
|
507
|
+
* @param options.resolver - A function that determines the cache key for storing the result based on the arguments provided.
|
|
508
|
+
* @param options.ttl - The time to live for the cache in milliseconds.
|
|
509
|
+
* @returns Returns the new memoized function.
|
|
380
510
|
*/
|
|
381
|
-
declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map<string | symbol, ReturnType<TFunc
|
|
511
|
+
declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map<string | symbol, [ReturnType<TFunc>, number]>>(func: TFunc, options?: {
|
|
512
|
+
resolver?: (...args: Parameters<TFunc>) => string | symbol;
|
|
513
|
+
ttl?: number;
|
|
514
|
+
}): TFunc & {
|
|
382
515
|
cache: Cache;
|
|
383
516
|
};
|
|
384
517
|
|
|
@@ -399,33 +532,23 @@ declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map
|
|
|
399
532
|
* @param func The function to restrict.
|
|
400
533
|
* @returns Returns the new restricted function.
|
|
401
534
|
*/
|
|
402
|
-
declare function minCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): (this:
|
|
535
|
+
declare function minCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): (this: unknown, ...args: Parameters<TFunc>) => ReturnType<TFunc> | undefined;
|
|
536
|
+
|
|
403
537
|
/**
|
|
404
|
-
*
|
|
538
|
+
* Generates a function that invokes the given function at most once per every `wait` milliseconds.
|
|
405
539
|
*
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
540
|
+
* This function can be used as a decorator with {@link decThrottle}.
|
|
409
541
|
* @example
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
* instance.testMethod(); // => undefined
|
|
419
|
-
* instance.testMethod(); // => 1
|
|
420
|
-
*
|
|
421
|
-
* @param runAfter The number of calls before the decorated function is invoked.
|
|
542
|
+
* const throttled = throttle(() => console.log("Throttled!"), 1000);
|
|
543
|
+
*
|
|
544
|
+
* throttled();
|
|
545
|
+
* throttled();
|
|
546
|
+
* // => "Throttled!" is logged once per second.
|
|
547
|
+
* @param func - The function to throttle.
|
|
548
|
+
* @param wait - The number of milliseconds to throttle invocations to.
|
|
549
|
+
* @returns Returns the new throttled function.
|
|
422
550
|
*/
|
|
423
|
-
declare
|
|
424
|
-
|
|
425
|
-
declare function throttle<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait?: number, options?: {
|
|
426
|
-
leading?: boolean;
|
|
427
|
-
trailing?: boolean;
|
|
428
|
-
}): (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>) => ReturnType<TFunc>;
|
|
551
|
+
declare function throttle<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait: number): TFunc;
|
|
429
552
|
|
|
430
553
|
/**
|
|
431
554
|
* Invokes a function `n` times, returning an array of the results of
|
|
@@ -922,4 +1045,4 @@ declare function isPlainObject(value: unknown): value is PlainObject$1;
|
|
|
922
1045
|
*/
|
|
923
1046
|
declare function isUrl(str: string): boolean;
|
|
924
1047
|
|
|
925
|
-
export { PlainObject, Queue, camelCase, capitalize, chunk, count, debounce, deburr, decMaxCalls, decMinCalls, difference, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, group, intersection, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, maxCalls, memoize, merge, minCalls, omit, pascalCase, pick, races, retry, sample, set, shuffle, sleep, snakeCase, sort, startCase, stripSpecial, takeRightWhile, takeWhile, throttle, timeout, times, toDecorator, unescapeHtml, unique };
|
|
1048
|
+
export { PlainObject, Queue, camelCase, capitalize, chunk, count, debounce, deburr, decDebounce, decMaxCalls, decMemoize, decMinCalls, decThrottle, difference, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, group, intersection, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, maxCalls, memoize, merge, minCalls, omit, pascalCase, pick, races, retry, sample, set, shuffle, sleep, snakeCase, sort, startCase, stripSpecial, takeRightWhile, takeWhile, throttle, timeout, times, toDecorator, unescapeHtml, unique };
|
package/dist/index.js
CHANGED
|
@@ -223,94 +223,29 @@ function toDecorator(func) {
|
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
// src/function/debounce.ts
|
|
226
|
-
function debounce(
|
|
227
|
-
let
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
lastArgs = lastThis = void 0;
|
|
241
|
-
lastInvokeTime = time;
|
|
242
|
-
result = fn.apply(thisArg, args);
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
function leadingEdge(time) {
|
|
246
|
-
lastInvokeTime = time;
|
|
247
|
-
timerId = setTimeout(timerExpired, wait);
|
|
248
|
-
return leading ? invokeFunc(time) : result;
|
|
249
|
-
}
|
|
250
|
-
function remainingWait(time) {
|
|
251
|
-
const timeSinceLastCall = time - lastCallTime;
|
|
252
|
-
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
253
|
-
const timeWaiting = wait - timeSinceLastCall;
|
|
254
|
-
return maxing ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting;
|
|
255
|
-
}
|
|
256
|
-
function shouldInvoke(time) {
|
|
257
|
-
if (lastCallTime === void 0)
|
|
258
|
-
return true;
|
|
259
|
-
const timeSinceLastCall = time - lastCallTime;
|
|
260
|
-
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
261
|
-
return timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
|
|
262
|
-
}
|
|
263
|
-
function timerExpired() {
|
|
264
|
-
const time = Date.now();
|
|
265
|
-
if (shouldInvoke(time)) {
|
|
266
|
-
return trailingEdge(time);
|
|
267
|
-
}
|
|
268
|
-
timerId = setTimeout(timerExpired, remainingWait(time));
|
|
269
|
-
}
|
|
270
|
-
function trailingEdge(time) {
|
|
271
|
-
timerId = void 0;
|
|
272
|
-
if (trailing && lastArgs) {
|
|
273
|
-
return invokeFunc(time);
|
|
274
|
-
}
|
|
275
|
-
lastArgs = lastThis = void 0;
|
|
276
|
-
return result;
|
|
277
|
-
}
|
|
278
|
-
function cancel() {
|
|
279
|
-
if (timerId !== void 0) {
|
|
280
|
-
clearTimeout(timerId);
|
|
281
|
-
}
|
|
282
|
-
lastInvokeTime = 0;
|
|
283
|
-
lastArgs = lastCallTime = lastThis = timerId = void 0;
|
|
284
|
-
}
|
|
285
|
-
function flush() {
|
|
286
|
-
return timerId === void 0 ? result : trailingEdge(Date.now());
|
|
287
|
-
}
|
|
288
|
-
function debounced(...args) {
|
|
289
|
-
const time = Date.now();
|
|
290
|
-
const isInvoking = shouldInvoke(time);
|
|
291
|
-
lastArgs = args;
|
|
292
|
-
lastThis = this;
|
|
293
|
-
lastCallTime = time;
|
|
294
|
-
if (isInvoking) {
|
|
295
|
-
if (timerId === void 0) {
|
|
296
|
-
return leadingEdge(lastCallTime);
|
|
297
|
-
}
|
|
298
|
-
if (maxing) {
|
|
299
|
-
clearTimeout(timerId);
|
|
300
|
-
timerId = setTimeout(timerExpired, wait);
|
|
301
|
-
return invokeFunc(lastCallTime);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (timerId === void 0) {
|
|
305
|
-
timerId = setTimeout(timerExpired, wait);
|
|
226
|
+
function debounce(func, wait) {
|
|
227
|
+
let timeoutId;
|
|
228
|
+
const debounced = function(...args) {
|
|
229
|
+
clearTimeout(timeoutId);
|
|
230
|
+
timeoutId = setTimeout(() => func.apply(this, args), wait);
|
|
231
|
+
};
|
|
232
|
+
debounced.cancel = function() {
|
|
233
|
+
clearTimeout(timeoutId);
|
|
234
|
+
timeoutId = void 0;
|
|
235
|
+
};
|
|
236
|
+
debounced.flush = function(...args) {
|
|
237
|
+
if (timeoutId) {
|
|
238
|
+
clearTimeout(timeoutId);
|
|
239
|
+
timeoutId = void 0;
|
|
306
240
|
}
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
debounced.cancel = cancel;
|
|
310
|
-
debounced.flush = flush;
|
|
241
|
+
func.apply(this, args);
|
|
242
|
+
};
|
|
311
243
|
return debounced;
|
|
312
244
|
}
|
|
313
245
|
|
|
246
|
+
// src/decorator/decDebounce.ts
|
|
247
|
+
var decDebounce = toDecorator(debounce);
|
|
248
|
+
|
|
314
249
|
// src/function/maxCalls.ts
|
|
315
250
|
function maxCalls(func, n) {
|
|
316
251
|
let count2 = 0;
|
|
@@ -323,25 +258,35 @@ function maxCalls(func, n) {
|
|
|
323
258
|
return result;
|
|
324
259
|
};
|
|
325
260
|
}
|
|
261
|
+
|
|
262
|
+
// src/decorator/decMaxCalls.ts
|
|
326
263
|
var decMaxCalls = toDecorator(maxCalls);
|
|
327
264
|
|
|
328
265
|
// src/function/memoize.ts
|
|
329
266
|
var defaultResolver = (...args) => JSON.stringify(args);
|
|
330
|
-
function memoize(func,
|
|
267
|
+
function memoize(func, options = {}) {
|
|
268
|
+
const resolver = options.resolver ?? defaultResolver;
|
|
269
|
+
const ttl = options.ttl;
|
|
331
270
|
const cache = /* @__PURE__ */ new Map();
|
|
332
271
|
const memoizedFunc = function(...args) {
|
|
333
272
|
const key = resolver(...args);
|
|
334
273
|
if (cache.has(key)) {
|
|
335
|
-
|
|
274
|
+
const [cacheResult, cacheTime] = cache.get(key);
|
|
275
|
+
if (ttl === void 0 || Date.now() - cacheTime < ttl) {
|
|
276
|
+
return cacheResult;
|
|
277
|
+
}
|
|
336
278
|
}
|
|
337
279
|
const result = func.apply(this, args);
|
|
338
|
-
cache.set(key, result);
|
|
280
|
+
cache.set(key, [result, Date.now()]);
|
|
339
281
|
return result;
|
|
340
282
|
};
|
|
341
283
|
memoizedFunc.cache = cache;
|
|
342
284
|
return memoizedFunc;
|
|
343
285
|
}
|
|
344
286
|
|
|
287
|
+
// src/decorator/decMemonize.ts
|
|
288
|
+
var decMemoize = toDecorator(memoize);
|
|
289
|
+
|
|
345
290
|
// src/function/minCalls.ts
|
|
346
291
|
function minCalls(func, n) {
|
|
347
292
|
let count2 = 1;
|
|
@@ -352,17 +297,25 @@ function minCalls(func, n) {
|
|
|
352
297
|
count2 += 1;
|
|
353
298
|
};
|
|
354
299
|
}
|
|
300
|
+
|
|
301
|
+
// src/decorator/decMinCalls.ts
|
|
355
302
|
var decMinCalls = toDecorator(minCalls);
|
|
356
303
|
|
|
357
304
|
// src/function/throttle.ts
|
|
358
|
-
function throttle(func, wait
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
305
|
+
function throttle(func, wait) {
|
|
306
|
+
let inThrottle = false;
|
|
307
|
+
return function(...args) {
|
|
308
|
+
if (!inThrottle) {
|
|
309
|
+
func.apply(this, args);
|
|
310
|
+
inThrottle = true;
|
|
311
|
+
setTimeout(() => inThrottle = false, wait);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
364
314
|
}
|
|
365
315
|
|
|
316
|
+
// src/decorator/decThrottle.ts
|
|
317
|
+
var decThrottle = toDecorator(throttle);
|
|
318
|
+
|
|
366
319
|
// src/function/times.ts
|
|
367
320
|
function times(func, n) {
|
|
368
321
|
const result = [];
|
|
@@ -684,8 +637,11 @@ export {
|
|
|
684
637
|
count,
|
|
685
638
|
debounce,
|
|
686
639
|
deburr,
|
|
640
|
+
decDebounce,
|
|
687
641
|
decMaxCalls,
|
|
642
|
+
decMemoize,
|
|
688
643
|
decMinCalls,
|
|
644
|
+
decThrottle,
|
|
689
645
|
difference,
|
|
690
646
|
dropRightWhile,
|
|
691
647
|
dropWhile,
|