moderndash 0.8.0 → 0.10.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 +64 -95
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +218 -91
- package/dist/index.js +61 -95
- 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,64 +260,31 @@ 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
|
-
type Tail<T extends unknown[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
|
|
264
263
|
/**
|
|
265
|
-
*
|
|
264
|
+
* Debouces the decorated function. Only calling it after a specified amount of time has passed without any new calls.
|
|
266
265
|
*
|
|
267
|
-
* @
|
|
268
|
-
* function log(func: Function, message: string) {
|
|
269
|
-
* return function (...args: unknown[]) {
|
|
270
|
-
* console.log(message);
|
|
271
|
-
* return func(...args);
|
|
272
|
-
* };
|
|
273
|
-
* }
|
|
266
|
+
* Look at {@link debounce} for the non-decorator version.
|
|
274
267
|
*
|
|
275
|
-
*
|
|
268
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
276
269
|
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
277
272
|
* class TestClass {
|
|
278
|
-
* @
|
|
279
|
-
* testMethod() {
|
|
280
|
-
*
|
|
273
|
+
* @decDebounce(1000)
|
|
274
|
+
* testMethod(str: string) {
|
|
275
|
+
* console.log("Debounced:", str);
|
|
281
276
|
* }
|
|
282
277
|
* }
|
|
283
278
|
*
|
|
284
279
|
* const instance = new TestClass();
|
|
285
|
-
* instance.testMethod();
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
* @
|
|
280
|
+
* instance.testMethod("Hello");
|
|
281
|
+
* instance.testMethod("World");
|
|
282
|
+
* // => Only the second invocation of `debouncedSayHello` is executed, after a delay of 1000ms.
|
|
283
|
+
* ```
|
|
284
|
+
* @param wait - Milliseconds to wait before invoking the decorated function after the last invocation.
|
|
290
285
|
*/
|
|
291
|
-
declare function
|
|
286
|
+
declare function decDebounce(wait: number): (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
292
287
|
|
|
293
|
-
declare function debounce<TFunc extends GenericFunction<TFunc>>(fn: TFunc, wait?: number, options?: {
|
|
294
|
-
leading?: boolean;
|
|
295
|
-
maxWait?: number;
|
|
296
|
-
trailing?: boolean;
|
|
297
|
-
}): (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>) => ReturnType<TFunc>;
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Creates a function that invokes the given function as long as it's called `<= n` times.
|
|
301
|
-
*
|
|
302
|
-
* Subsequent calls to the created function return the result of the last `func` invocation.
|
|
303
|
-
*
|
|
304
|
-
* This function can be used as a decorator with {@link decMaxCalls}.
|
|
305
|
-
* @example
|
|
306
|
-
* let count = 0;
|
|
307
|
-
* const addCount = () => ++count;
|
|
308
|
-
*
|
|
309
|
-
* // Allow addCount to be invoked twice.
|
|
310
|
-
* const limitAddCount = maxCalls(2, addCount)
|
|
311
|
-
*
|
|
312
|
-
* limitAddCount() // => 1
|
|
313
|
-
* limitAddCount() // => 2
|
|
314
|
-
* limitAddCount() // => 2
|
|
315
|
-
* // => `limitAddCount` is invoked twice and the result is cached.
|
|
316
|
-
* @param n - The number of calls before the cached result is returned.
|
|
317
|
-
* @param func - The function to restrict.
|
|
318
|
-
* @returns Returns the new restricted function.
|
|
319
|
-
*/
|
|
320
|
-
declare function maxCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): TFunc;
|
|
321
288
|
/**
|
|
322
289
|
* Only invokes the decorated function as long as it's called `<= n` times.
|
|
323
290
|
* Subsequent calls to the decorated function return the result of the last invocation.
|
|
@@ -327,6 +294,7 @@ declare function maxCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n:
|
|
|
327
294
|
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
328
295
|
*
|
|
329
296
|
* @example
|
|
297
|
+
* ```typescript
|
|
330
298
|
* class TestClass {
|
|
331
299
|
* private count = 0;
|
|
332
300
|
* @decMaxCalls(2)
|
|
@@ -338,68 +306,91 @@ declare function maxCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n:
|
|
|
338
306
|
* instance.testMethod(); // => 1
|
|
339
307
|
* instance.testMethod(); // => 2
|
|
340
308
|
* instance.testMethod(); // => 2
|
|
341
|
-
*
|
|
309
|
+
* ```
|
|
310
|
+
* @param n - The number of calls before the cached result is returned.
|
|
342
311
|
*/
|
|
343
|
-
declare
|
|
312
|
+
declare function decMaxCalls(n: number): (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
344
313
|
|
|
345
314
|
/**
|
|
346
315
|
* Creates a function that memoizes the result of `func`.
|
|
347
|
-
*
|
|
348
|
-
* By default, all arguments provided to the memoized function are used as the map cache key.
|
|
316
|
+
* The cache key is either determined by the provided resolver or by the arguments used in the memoized function.
|
|
349
317
|
*
|
|
350
|
-
* The cache is exposed as the `cache` property on the memoized
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
318
|
+
* The cache is exposed as the `cache` property on the memoized function.
|
|
319
|
+
* Its creation may be customized by replacing the `memoize.cache` value.
|
|
320
|
+
* The new cache must implement `get` and `set` methods like the built-in `Map` constructors.
|
|
321
|
+
*
|
|
322
|
+
* **Options:**
|
|
323
|
+
* - `resolver` A function that determines the cache key for storing the result based on the arguments provided.
|
|
324
|
+
* - `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.
|
|
325
|
+
*
|
|
326
|
+
* This function can be used as a decorator with {@link decMemoize}.
|
|
355
327
|
*
|
|
356
328
|
* @example
|
|
329
|
+
* ```typescript
|
|
357
330
|
* const object = { 'a': 1, 'b': 2 }
|
|
358
331
|
*
|
|
359
|
-
* const values = memoize(values)
|
|
332
|
+
* const values = memoize(Object.values, { ttl: 1000 })
|
|
360
333
|
* values(object)
|
|
361
334
|
* // => [1, 2]
|
|
362
335
|
*
|
|
363
336
|
* values(object)
|
|
364
337
|
* // => [1, 2]
|
|
365
338
|
*
|
|
366
|
-
* object
|
|
367
|
-
*
|
|
368
|
-
* // => [2, 2]
|
|
339
|
+
* setTimeout(() => values(object), 1000)
|
|
340
|
+
* // => [1, 2] (cache miss after 1 second)
|
|
369
341
|
*
|
|
370
|
-
* //
|
|
371
|
-
*
|
|
372
|
-
* values(object)
|
|
373
|
-
* // => ['a', 'b']
|
|
342
|
+
* // Replace `memoize.cache`.
|
|
343
|
+
* memoize.cache = new WeakMap()
|
|
374
344
|
*
|
|
375
|
-
* //
|
|
376
|
-
*
|
|
345
|
+
* // This is the default way to create cache keys.
|
|
346
|
+
* const defaultResolver = (...args: unknown[]) => JSON.stringify(args);
|
|
347
|
+
* ```
|
|
377
348
|
* @param func - The function to have its output memoized.
|
|
378
|
-
* @param
|
|
379
|
-
* @
|
|
349
|
+
* @param options - The options object with optional `resolver` and `ttl` parameters.
|
|
350
|
+
* @param options.resolver - A function that determines the cache key for storing the result based on the arguments provided.
|
|
351
|
+
* @param options.ttl - The time to live for the cache in milliseconds.
|
|
352
|
+
* @returns Returns the new memoized function.
|
|
380
353
|
*/
|
|
381
|
-
declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map<string | symbol, ReturnType<TFunc
|
|
354
|
+
declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map<string | symbol, [ReturnType<TFunc>, number]>>(func: TFunc, options?: {
|
|
355
|
+
resolver?: (...args: Parameters<TFunc>) => string | symbol;
|
|
356
|
+
ttl?: number;
|
|
357
|
+
}): TFunc & {
|
|
382
358
|
cache: Cache;
|
|
383
359
|
};
|
|
384
360
|
|
|
385
361
|
/**
|
|
386
|
-
*
|
|
387
|
-
*
|
|
362
|
+
* Memoizes the decorated function.
|
|
363
|
+
* The cache key is either determined by the provided resolver or by the arguments used in the memoized function.
|
|
388
364
|
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
* const minCalls = after(caution, 2);
|
|
365
|
+
* **Options:**
|
|
366
|
+
* - `resolver` A function that determines the cache key for storing the result based on the arguments provided.
|
|
367
|
+
* - `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.
|
|
393
368
|
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
* @
|
|
399
|
-
*
|
|
400
|
-
*
|
|
369
|
+
* Look at {@link memoize} for the non-decorator version.
|
|
370
|
+
*
|
|
371
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```typescript
|
|
375
|
+
* class TestClass {
|
|
376
|
+
* @decMemoize({ ttl: 1000 })
|
|
377
|
+
* testMethod(a: number, b: number) {
|
|
378
|
+
* return a + b;
|
|
379
|
+
* }
|
|
380
|
+
* }
|
|
381
|
+
* const instance = new TestClass();
|
|
382
|
+
* instance.testMethod(1, 2); // => 3
|
|
383
|
+
* instance.testMethod(1, 2); // => 3 (cached)
|
|
384
|
+
*
|
|
385
|
+
* // After 1 second:
|
|
386
|
+
* instance.testMethod(1, 2); // => 3 (cache miss)
|
|
387
|
+
* ```
|
|
388
|
+
* @param options - The options object.
|
|
389
|
+
* @param options.resolver - A function that determines the cache key for storing the result based on the arguments provided.
|
|
390
|
+
* @param options.ttl - The time to live for the cache in milliseconds.
|
|
401
391
|
*/
|
|
402
|
-
declare function
|
|
392
|
+
declare function decMemoize(options?: Parameters<typeof memoize>[1]): (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
393
|
+
|
|
403
394
|
/**
|
|
404
395
|
* Only invokes the decorated function after it's called more than `n` times.
|
|
405
396
|
*
|
|
@@ -407,6 +398,7 @@ declare function minCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n:
|
|
|
407
398
|
*
|
|
408
399
|
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
409
400
|
* @example
|
|
401
|
+
* ```typescript
|
|
410
402
|
* class TestClass {
|
|
411
403
|
* @decAfter(2)
|
|
412
404
|
* testMethod() {
|
|
@@ -417,15 +409,150 @@ declare function minCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n:
|
|
|
417
409
|
* instance.testMethod(); // => undefined
|
|
418
410
|
* instance.testMethod(); // => undefined
|
|
419
411
|
* instance.testMethod(); // => 1
|
|
412
|
+
* ```
|
|
413
|
+
* @param n The number of calls before the decorated function is invoked.
|
|
414
|
+
*/
|
|
415
|
+
declare function decMinCalls(n: number): (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* The decorated function is invoked at most once per every `wait` milliseconds.
|
|
420
419
|
*
|
|
421
|
-
*
|
|
420
|
+
* Look at {@link throttle} for the non-decorator version.
|
|
421
|
+
*
|
|
422
|
+
* *Requires TypeScript >=5.0 or `experimentalDecorators` flag enabled.*
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```typescript
|
|
426
|
+
* class TestClass {
|
|
427
|
+
* @decThrottle(1000)
|
|
428
|
+
* testMethod() {
|
|
429
|
+
* console.log("Throttled!");
|
|
430
|
+
* }
|
|
431
|
+
* }
|
|
432
|
+
*
|
|
433
|
+
* const instance = new TestClass();
|
|
434
|
+
* instance.testMethod(); // => "Throttled!" is logged once per second.
|
|
435
|
+
* instance.testMethod(); // nothing happens
|
|
436
|
+
* ```
|
|
437
|
+
* @param wait - The number of milliseconds to wait between invocations.
|
|
422
438
|
*/
|
|
423
|
-
declare
|
|
439
|
+
declare function decThrottle(wait: number): (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
424
440
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
441
|
+
type Tail<T extends unknown[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
|
|
442
|
+
/**
|
|
443
|
+
* Transforms a function that takes a function as first argument into a decorator function.
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* function log(func: Function, message: string) {
|
|
448
|
+
* return function (...args: unknown[]) {
|
|
449
|
+
* console.log(message);
|
|
450
|
+
* return func(...args);
|
|
451
|
+
* };
|
|
452
|
+
* }
|
|
453
|
+
*
|
|
454
|
+
* const logger = toDecorator(log);
|
|
455
|
+
*
|
|
456
|
+
* class TestClass {
|
|
457
|
+
* @logger("Hello world!")
|
|
458
|
+
* testMethod() {
|
|
459
|
+
* return 1;
|
|
460
|
+
* }
|
|
461
|
+
* }
|
|
462
|
+
*
|
|
463
|
+
* const instance = new TestClass();
|
|
464
|
+
* instance.testMethod();
|
|
465
|
+
* // => Log "Hello World" and return 1
|
|
466
|
+
* ```
|
|
467
|
+
* @param func - The function to transform.
|
|
468
|
+
* @returns A decorator function that can be used to decorate a method.
|
|
469
|
+
*/
|
|
470
|
+
declare function toDecorator<TFunc extends GenericFunction<TFunc>>(func: TFunc): (...args: Tail<Parameters<TFunc>>) => (target: unknown, key: string, descriptor: PropertyDescriptor) => void;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Creates a debounced version of a function. Only calling it after a specified amount of time has passed without any new calls.
|
|
474
|
+
*
|
|
475
|
+
* ----
|
|
476
|
+
*
|
|
477
|
+
* **Methods:**
|
|
478
|
+
* - `cancel` will cancel the next invocation of the debounced function.
|
|
479
|
+
* - `flush` will immediately invoke the debounced function and cancel any pending invocations.
|
|
480
|
+
*
|
|
481
|
+
* This function can be used as a decorator with {@link decDebounce}.
|
|
482
|
+
*
|
|
483
|
+
* @example
|
|
484
|
+
* const sayHello = (name: string) => console.log(`Hello, ${name}!`);
|
|
485
|
+
* const debouncedSayHello = debounce(sayHello, 1000);
|
|
486
|
+
*
|
|
487
|
+
* debouncedSayHello("John");
|
|
488
|
+
* debouncedSayHello("Jane");
|
|
489
|
+
* // => Only the second invocation of `debouncedSayHello` is executed, after a delay of 1000ms.
|
|
490
|
+
* @param func - The function to debounce.
|
|
491
|
+
* @param wait - The number of milliseconds to wait before invoking `func`.
|
|
492
|
+
* @returns A debounced version of `func` with `cancel` and `flush` methods.
|
|
493
|
+
*/
|
|
494
|
+
declare function debounce<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait: number): TFunc & {
|
|
495
|
+
cancel: () => void;
|
|
496
|
+
flush: () => void;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Creates a function that invokes the given function as long as it's called `<= n` times.
|
|
501
|
+
*
|
|
502
|
+
* Subsequent calls to the created function return the result of the last `func` invocation.
|
|
503
|
+
*
|
|
504
|
+
* This function can be used as a decorator with {@link decMaxCalls}.
|
|
505
|
+
* @example
|
|
506
|
+
* let count = 0;
|
|
507
|
+
* const addCount = () => ++count;
|
|
508
|
+
*
|
|
509
|
+
* // Allow addCount to be invoked twice.
|
|
510
|
+
* const limitAddCount = maxCalls(addCount, 2)
|
|
511
|
+
*
|
|
512
|
+
* limitAddCount() // => 1
|
|
513
|
+
* limitAddCount() // => 2
|
|
514
|
+
* limitAddCount() // => 2
|
|
515
|
+
* // => `limitAddCount` is invoked twice and the result is cached.
|
|
516
|
+
* @param n - The number of calls before the cached result is returned.
|
|
517
|
+
* @param func - The function to restrict.
|
|
518
|
+
* @returns Returns the new restricted function.
|
|
519
|
+
*/
|
|
520
|
+
declare function maxCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): TFunc;
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Creates a function that invokes the given function once it's called more than `n` times.
|
|
524
|
+
* Returns undefined until the minimum call count is reached.
|
|
525
|
+
*
|
|
526
|
+
* This function can be used as a decorator with {@link decMinCalls}.
|
|
527
|
+
* @example
|
|
528
|
+
* const caution = () => console.log("Caution!");
|
|
529
|
+
* const minCalls = after(caution, 2);
|
|
530
|
+
*
|
|
531
|
+
* minCalls()
|
|
532
|
+
* minCalls()
|
|
533
|
+
* minCalls()
|
|
534
|
+
* // => `caution` is invoked on the third call.
|
|
535
|
+
* @param n The number of calls before the given function is invoked.
|
|
536
|
+
* @param func The function to restrict.
|
|
537
|
+
* @returns Returns the new restricted function.
|
|
538
|
+
*/
|
|
539
|
+
declare function minCalls<TFunc extends GenericFunction<TFunc>>(func: TFunc, n: number): (this: unknown, ...args: Parameters<TFunc>) => ReturnType<TFunc> | undefined;
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Generates a function that invokes the given function at most once per every `wait` milliseconds.
|
|
543
|
+
*
|
|
544
|
+
* This function can be used as a decorator with {@link decThrottle}.
|
|
545
|
+
* @example
|
|
546
|
+
* const throttled = throttle(() => console.log("Throttled!"), 1000);
|
|
547
|
+
*
|
|
548
|
+
* throttled();
|
|
549
|
+
* throttled();
|
|
550
|
+
* // => "Throttled!" is logged once per second.
|
|
551
|
+
* @param func - The function to throttle.
|
|
552
|
+
* @param wait - The number of milliseconds to throttle invocations to.
|
|
553
|
+
* @returns Returns the new throttled function.
|
|
554
|
+
*/
|
|
555
|
+
declare function throttle<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait: number): TFunc;
|
|
429
556
|
|
|
430
557
|
/**
|
|
431
558
|
* Invokes a function `n` times, returning an array of the results of
|
|
@@ -922,4 +1049,4 @@ declare function isPlainObject(value: unknown): value is PlainObject$1;
|
|
|
922
1049
|
*/
|
|
923
1050
|
declare function isUrl(str: string): boolean;
|
|
924
1051
|
|
|
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 };
|
|
1052
|
+
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,31 @@ 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
|
+
function decDebounce(wait) {
|
|
248
|
+
return toDecorator(debounce)(wait);
|
|
249
|
+
}
|
|
250
|
+
|
|
314
251
|
// src/function/maxCalls.ts
|
|
315
252
|
function maxCalls(func, n) {
|
|
316
253
|
let count2 = 0;
|
|
@@ -323,25 +260,39 @@ function maxCalls(func, n) {
|
|
|
323
260
|
return result;
|
|
324
261
|
};
|
|
325
262
|
}
|
|
326
|
-
|
|
263
|
+
|
|
264
|
+
// src/decorator/decMaxCalls.ts
|
|
265
|
+
function decMaxCalls(n) {
|
|
266
|
+
return toDecorator(maxCalls)(n);
|
|
267
|
+
}
|
|
327
268
|
|
|
328
269
|
// src/function/memoize.ts
|
|
329
270
|
var defaultResolver = (...args) => JSON.stringify(args);
|
|
330
|
-
function memoize(func,
|
|
271
|
+
function memoize(func, options = {}) {
|
|
272
|
+
const resolver = options.resolver ?? defaultResolver;
|
|
273
|
+
const ttl = options.ttl;
|
|
331
274
|
const cache = /* @__PURE__ */ new Map();
|
|
332
275
|
const memoizedFunc = function(...args) {
|
|
333
276
|
const key = resolver(...args);
|
|
334
277
|
if (cache.has(key)) {
|
|
335
|
-
|
|
278
|
+
const [cacheResult, cacheTime] = cache.get(key);
|
|
279
|
+
if (ttl === void 0 || Date.now() - cacheTime < ttl) {
|
|
280
|
+
return cacheResult;
|
|
281
|
+
}
|
|
336
282
|
}
|
|
337
283
|
const result = func.apply(this, args);
|
|
338
|
-
cache.set(key, result);
|
|
284
|
+
cache.set(key, [result, Date.now()]);
|
|
339
285
|
return result;
|
|
340
286
|
};
|
|
341
287
|
memoizedFunc.cache = cache;
|
|
342
288
|
return memoizedFunc;
|
|
343
289
|
}
|
|
344
290
|
|
|
291
|
+
// src/decorator/decMemonize.ts
|
|
292
|
+
function decMemoize(options = {}) {
|
|
293
|
+
return toDecorator(memoize)(options);
|
|
294
|
+
}
|
|
295
|
+
|
|
345
296
|
// src/function/minCalls.ts
|
|
346
297
|
function minCalls(func, n) {
|
|
347
298
|
let count2 = 1;
|
|
@@ -352,15 +303,27 @@ function minCalls(func, n) {
|
|
|
352
303
|
count2 += 1;
|
|
353
304
|
};
|
|
354
305
|
}
|
|
355
|
-
|
|
306
|
+
|
|
307
|
+
// src/decorator/decMinCalls.ts
|
|
308
|
+
function decMinCalls(n) {
|
|
309
|
+
return toDecorator(minCalls)(n);
|
|
310
|
+
}
|
|
356
311
|
|
|
357
312
|
// src/function/throttle.ts
|
|
358
|
-
function throttle(func, wait
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
313
|
+
function throttle(func, wait) {
|
|
314
|
+
let inThrottle = false;
|
|
315
|
+
return function(...args) {
|
|
316
|
+
if (!inThrottle) {
|
|
317
|
+
func.apply(this, args);
|
|
318
|
+
inThrottle = true;
|
|
319
|
+
setTimeout(() => inThrottle = false, wait);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/decorator/decThrottle.ts
|
|
325
|
+
function decThrottle(wait) {
|
|
326
|
+
return toDecorator(throttle)(wait);
|
|
364
327
|
}
|
|
365
328
|
|
|
366
329
|
// src/function/times.ts
|
|
@@ -684,8 +647,11 @@ export {
|
|
|
684
647
|
count,
|
|
685
648
|
debounce,
|
|
686
649
|
deburr,
|
|
650
|
+
decDebounce,
|
|
687
651
|
decMaxCalls,
|
|
652
|
+
decMemoize,
|
|
688
653
|
decMinCalls,
|
|
654
|
+
decThrottle,
|
|
689
655
|
difference,
|
|
690
656
|
dropRightWhile,
|
|
691
657
|
dropWhile,
|