-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
1187 lines (1080 loc) · 27.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#region TYPES
/**
* unknown function type.
* @params args arguments
* @returns unknown value
*/
export type Function = (...args: unknown[]) => unknown;
//#endregion
//#region CONSTANTS
/**
* Do nothing.
* @param _args arguments (ignored)
* @example
* ```javascript
* xfunction.NOOP(1, 2);
* // → undefined
*
* xfunction.NOOP('a', 'b');
* // → undefined
* ```
*/
export function NOOP(..._args: unknown[]): void {}
// - https://lodash.com/docs/4.17.15#noop
/**
* Return false.
* @param _args arguments (ignored)
* @returns false.
* @example
* ```javascript
* xfunction.FALSE();
* // → false
* ```
*/
export function FALSE(..._args: unknown[]): false {
return false;
}
// - https://www.npmjs.com/package/boolbase
/**
* Return true.
* @param _args arguments (ignored)
* @returns true.
* @example
* ```javascript
* xfunction.TRUE();
* // → true
* ```
*/
export function TRUE(..._args: unknown[]): true {
return true;
}
// - https://www.npmjs.com/package/boolbase
/**
* Return the same (first) value.
* @param v a value
* @returns v
* @example
* ```javascript
* xfunction.IDENTITY(1);
* // → 1
*
* xfunction.IDENTITY('a');
* // → 'a'
* ```
*/
export function IDENTITY<T>(v: T): T {
return v;
}
/**
* Compare two values.
* @param a a value
* @param b another value
* @returns a<b: -1, a=b: 0, a>b: 1
* @example
* ```javascript
* xfunction.COMPARE(1, 2);
* // → -1
*
* xfunction.COMPARE(2, 1);
* // → 1
*
* xfunction.COMPARE(1, 1);
* // → 0
*/
export function COMPARE<T>(a: T, b: T): number {
return a<b? -1 : (a>b? 1 : 0);
}
/**
* Return the arguments passed as a array.
* @param args arguments
* @returns [...args]
* @example
* ```javascript
* xfunction.ARGUMENTS(1, 2);
* // → [1, 2]
*
* xfunction.ARGUMENTS('a', 'b');
* // → ['a', 'b']
* ```
*/
export function ARGUMENTS(...args: unknown[]): unknown[] {
return args;
}
//#endregion
//#region METHODS (BUILTIN)
//#region ABOUT
/**
* Get the name of a function.
* @param x a function
* @returns name
* @example
* ```javascript
* import {delay, debounce} from "jsr:@nodef/extra-function";
*
* xfunction.name(delay);
* // → 'delay'
*
* xfunction.name(debounce);
* // → 'debounce'
* ```
*/
export function name(x: Function): string {
return x.name;
}
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
// - https://www.npmjs.com/package/fn-name
/**
* Get the number of parameters of a function.
* @param x a function
* @returns |[p, q, ...]| | x(p, q, ...)
* @example
* ```javascript
* xfunction.length(() => 0);
* // → 0
*
* xfunction.length((x, y) => 0);
* // → 2
* ```
*/
export function length(x: Function): number {
return x.length;
}
export {length as arity};
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length
//#endregion
//#region BINDING
/**
* Bind this-object, and optional prefix arguments to a function.
* @param x a function
* @param ths this object to bind
* @param prefix prefix arguments
* @returns (...args) => this.x(...prefix, ...args)
* @example
* ```javascript
* var array = [1];
* var fn = xfunction.bind(Array.prototype.push, array);
* fn(2, 3, 4); // push(2, 3, 4)
* array;
* // → [1, 2, 3, 4]
*
* var array = [1, 2, 3, 4];
* var fn = xfunction.bind(Array.prototype.splice, array, 1);
* fn(2); // splice(1, 2)
* array;
* // → [1, 4]
* ```
*/
export function bind(x: Function, ths: unknown, ...prefix: unknown[]): Function {
return x.bind(ths, ...prefix);
}
// - https://underscorejs.org/#bind
// - https://lodash.com/docs/4.17.15#bind
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
//#endregion
//#region INVOCATION
/**
* Invoke a function with specified this-object, and arguments provided individually.
* @param x a function
* @param ths this object to invoke with
* @param args arguments
* @returns this.x(...args)
* @example
* ```javascript
* var array = [1];
* xfunction.call(Array.prototype.push, array, 2, 3, 4); // push(2, 3, 4)
* array;
* // → [1, 2, 3, 4]
*
* var array = [1, 2, 3, 4];
* xfunction.call(Array.prototype.splice, array, 1, 2); // splice(1, 2)
* array;
* // → [1, 4]
* ```
*/
export function call(x: Function, ths: unknown=null, ...args: unknown[]): unknown {
return x.call(ths, ...args);
}
/**
* Invoke a function with specified this-object, and arguments provided as an array.
* @param x a function
* @param ths this object to invoke with
* @param args arguments array
* @returns this.x(...args)
* @example
* ```javascript
* var array = [1];
* xfunction.apply(Array.prototype.push, array, [2, 3, 4]); // push(2, 3, 4)
* array;
* // → [1, 2, 3, 4]
*
* var array = [1, 2, 3, 4];
* xfunction.apply(Array.prototype.splice, array, [1, 2]); // splice(1, 2)
* array;
* // → [1, 4]
* ```
*/
export function apply(x: Function, ths: unknown=null, args: unknown[]): unknown {
return x.apply(ths, args);
}
//#endregion
//#endregion
//#region METHODS (CUSTOM)
//#region ABOUT
/**
* Check if value is a function.
* @param v a value
* @returns is function?
* @example
* ```javascript
* xfunction.is(Object.keys);
* // → true
*
* xfunction.is(() => 0);
* // → true
*
* xfunction.is(async () => 0);
* // → true
*
* xfunction.is(0);
* // → false
* ```
*/
export function is(v: unknown): v is Function {
return typeof v==="function";
}
// - https://www.npmjs.com/package/is-function
/**
* Check if value is an async function.
* @param v a value
* @returns is async function?
* @example
* ```javascript
* xfunction.isAsync(async () => 0);
* // → true
*
* xfunction.isAsync(() => 0);
* // → false
* ```
*/
export function isAsync(v: unknown): boolean {
const AsyncFunction = (async function () {}).constructor;
return v instanceof AsyncFunction;
}
// - https://www.npmjs.com/package/is-async-function
// - https://www.npmjs.com/package/is-callback-function
// - https://davidwalsh.name/javascript-detect-async-function
// - https://stackoverflow.com/questions/38508420/how-to-know-if-a-function-is-async
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
/**
* Check if value is a generator function.
* @param v a value
* @returns is generator function?
* @example
* ```javascript
* function* naturalNumbers() {
* for (var i=0;; ++i)
* yield i;
* }
*
* xfunction.isGenerator(naturalNumbers);
* // → true
*
* xfunction.isGenerator(() => 0);
* // → false
* ```
*/
export function isGenerator(v: unknown): v is GeneratorFunction {
const GeneratorFunction = (function* () {}).constructor;
return v instanceof GeneratorFunction;
}
// - https://www.npmjs.com/package/is-generator
// - https://www.npmjs.com/package/is-generator-fn
// - https://www.npmjs.com/package/is-generator-function
// - https://stackoverflow.com/questions/16754956/check-if-function-is-a-generator
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction
//#endregion
//#region CONTEXT
/**
* Contextify a function by accepting the first parameter as this-object.
* @param x a function
* @returns (...args) => x(this, ...args)
* @example
* ```javascript
* function count(x, value) {
* var a = 0;
* for (var v of x)
* if (v===value) ++a;
* return a;
* }
*
* var fn = xfunction.contextify(count);
*
* fn.call([6, 3, 4, 3], 3);
* // → 2
*
* fn.call([6, 1, 4, 3], 3);
* // → 1
* ```
*/
export function contextify(x: Function): Function {
return function(this: unknown, ...args: unknown[]) { return x(this, ...args); };
}
/**
* Decontextify a function by accepting this-object as the first argument.
* @param x a function
* @returns (this, ...args) => this.x(...args)
* @example
* ```javascript
* function count(value) {
* var a = 0;
* for (var v of this)
* if (v===value) ++a;
* return a;
* }
*
* var fn = xfunction.decontextify(count);
*
* fn([6, 3, 4, 3], 3);
* // → 2
*
* fn([6, 1, 4, 3], 3);
* // → 1
* ```
*/
export function decontextify(x: Function): Function {
return (ths: unknown=null, ...args: unknown[]) => x.call(ths, ...args);
}
//#endregion
//#region RESULT MANIPULATION
/**
* Generate a result-negated version of a function.
* @param x a function
* @returns (...args) => !x(...args)
* @example
* ```javascript
* var fn = xfunction.negate(isFinite);
* fn(Infinity)
* // → true
* fn(1)
* // → false
*
* var fn = xfunction.negate(isNaN);
* fn(1);
* // → true
* fn(NaN);
* // → false
* ```
*/
export function negate(x: Function): Function {
return (...args: unknown[]) => !x(...args);
}
//#endregion
//#region RESULT CACHING
/**
* Resolve arguments into a unique key.
* @param args arguments
* @returns unique key
*/
export type Resolver = (...args: unknown[]) => unknown;
/**
* Generate result-cached version of a function.
* @param x a function
* @param fr resolver ((...args) => unique key) [IDENTITY]
* @param cache result cache [Map()]
* @example
* ```javascript
* var calls = 0;
*
* function factorialRec(n: number) {
* if (n<=1) return 1;
* return n * factorialRec(n-1);
* }
*
* function factorial(n: number) {
* ++calls;
* return factorialRec(n);
* }
*
* var fn = xfunction.memoize(factorial);
* fn(3);
* // → 6
* fn(4);
* // → 24
* fn(5);
* // → 120
* fn(3);
* // → 6
* fn(4);
* // → 24
* fn(5);
* // → 120
* calls;
* // → 3
*
*
* var calls = 0;
*
* function hypot(x: number, y: number) {
* ++calls;
* return Math.hypot(x, y);
* }
*
* function resolver(x: number, y: number) {
* return 4093*y + x; // a hash
* }
*
* var fn = xfunction.memoize(hypot, resolver);
* fn(3, 4);
* // → 5
* fn(6, 8);
* // → 10
* fn(5, 12);
* // → 13
* fn(3, 4);
* // → 5
* fn(6, 8);
* // → 10
* fn(5, 12);
* // → 13
* calls;
* // → 3
* ```
*/
export function memoize(x: Function, fr: Resolver | null=null, cache: Map<unknown, unknown> | null=null): Function {
fr = fr || IDENTITY;
cache = cache || new Map();
return (...args: unknown[]) => {
const k = fr(...args);
if (cache.has(k)) return cache.get(k);
const v = x(...args);
cache.set(k, v);
return v;
};
}
// - https://www.npmjs.com/package/memoizee
// - https://www.npmjs.com/package/memoizerific
//#endregion
//#region PARAMETER MANIPULATION
/**
* Generate a parameter-reversed version of a function.
* @param x a function
* @returns (p, q, ...) => x(..., q, p)
* @example
* ```javascript
* function divide(x, y) {
* return x/y;
* }
*
* var fn = xfunction.reverse(divide);
* fn(2, 4);
* // → 2
* fn(2, 6);
* // → 3
* fn(2, 8);
* // → 4
*
*
* var array = [1];
*
* function push(...args) {
* array.push(...args);
* }
*
* var fn = xfunction.reverse(push);
* fn(2, 3, 4); // push(2, 3, 4) in reverse order
* array;
* // → [1, 4, 3, 2]
* ```
*/
export function reverse(x: Function): Function {
return (...args: unknown[]) => x(...args.reverse());
}
export {reverse as flip};
/**
* Generate a (first) parameter-spreaded version of a function.
* @param x a function
* @returns (p, q, ...) => x([p, q, ...])
* @example
* ```javascript
* function sum(x: number[]) {
* var a = 0;
* for (var v of x)
* a += v;
* return a;
* }
*
* var fn = xfunction.spread(sum);
* fn(1, 2, 3); // sum([1, 2, 3])
* // → 6
*
*
* var array = [1];
*
* function concat(x: number[]) {
* return array.concat(x);
* }
*
* var fn = xfunction.spread(concat);
* fn(2, 3, 4); // concat([2, 3, 4])
* // → [1, 2, 3, 4]
* ```
*/
export function spread(x: Function): Function {
return (...args: unknown[]) => x(args);
}
/**
* Generate a (first) parameter-collapsed version of a function.
* @param x a function
* @returns ([p, q, ...]) => x(p, q, ...)
* @example
* ```javascript
* var fn = xfunction.unspread(Math.min);
* fn([7, 4, 9]); // Math.min(7, 4, 9)
* // → 4
*
* var fn = xfunction.unspread((x, i, I) => x.slice(i, I));
* fn([[1, 2, 3, 4], 2]); // [1, 2, 3, 4].slice(2)
* // → [3, 4]
* ```
*/
export function unspread(x: Function): Function {
return (args: unknown) => x(...(args as unknown[]));
}
/**
* Attach prefix arguments to leftmost parameters of a function.
* @param x a function
* @param prefix prefix arguments
* @returns (...args) => x(...prefix, ...args)
* @example
* ```javascript
* var fn = xfunction.attach(Math.min, 100);
* fn(180, 130); // Math.min(100, 180, 130)
* // → 100
*
* var array = [1];
* var fn = xfunction.attach((...vs) => array.push(...vs), 10, 10);
* fn(1, 2, 3); // array.push(10, 10, 1, 2, 3)
* array;
* // → [1, 10, 10, 1, 2, 3]
* ```
*/
export function attach(x: Function, ...prefix: unknown[]): Function {
return (...args: unknown[]) => x(...prefix, ...args);
}
export {attach as partial};
/**
* Attach suffix arguments to rightmost parameters of a function.
* @param x a function
* @param suffix suffix arguments
* @returns (...args) => x(...args, ...suffix)
* @example
* ```javascript
* var fn = xfunction.attachRight(Math.min, 100);
* fn(180, 130); // Math.min(180, 130, 100)
* // → 100
*
* var array = [1];
* var fn = xfunction.attachRight((...vs) => array.push(...vs), 10, 10);
* fn(1, 2, 3); // array.push(1, 2, 3, 10, 10)
* array;
* // → [1, 1, 2, 3, 10, 10]
* ```
*/
export function attachRight(x: Function, ...suffix: unknown[]): Function {
return (...args: unknown[]) => x(...args, ...suffix);
}
export {attachRight as partialRight};
//#endregion
//#region FUNCTIONAL BEHAVIOUR
/**
* Compose functions together, in applicative order.
* @param xs functions (f, g)
* @returns (f o g), or f(g(x))
* @example
* ```javascript
* var fn = xfunction.compose(Math.sqrt, Math.abs);
* fn(-64); // Math.sqrt(Math.abs(-64))
* // → 8
*
* var fn = xfunction.compose(Math.sqrt, Math.min);
* fn(22, 9); // Math.sqrt(Math.min(22, 9))
* // → 3
* ```
*/
export function compose(...xs: Function[]): Function {
return composeRight(...xs.reverse());
}
// - https://en.wikipedia.org/wiki/Function_composition
// - http://learnyouahaskell.com/higher-order-functions
// - https://www.npmjs.com/package/compose-function
/**
* Compose functions together, such that result is piped forward.
* @param xs functions (f, g)
* @returns (f ▷ g), or g(f(x))
* @example
* ```javascript
* var fn = xfunction.composeRight(Math.abs, Math.sqrt);
* fn(-64); // Math.sqrt(Math.abs(-64))
* // → 8
*
* var fn = xfunction.composeRight(Math.min, Math.sqrt);
* fn(22, 9); // Math.sqrt(Math.min(22, 9))
* // → 3
* ```
*/
export function composeRight(...xs: Function[]): Function {
return (...args: unknown[]) => {
if (xs.length===0) return;
let a = xs[0](...args);
for (let i=1, I=xs.length; i<I; i++)
a = xs[i](a);
return a;
};
}
// - https://stackoverflow.com/questions/1457140/haskell-composition-vs-fs-pipe-forward-operator
// - https://www.npmjs.com/package/chain-function
/**
* Generate curried version of a function.
* @param x a function
* @param n number of parameters [all]
* @returns (p)(q)(...) => x(p, q, ...)
* @example
* ```javascript
* var sub = (x: number, y: number) => x - y;
* var fn = xfunction.curry(sub);
* fn(2)(3); // sub(2, 3)
* // → -1
*
* var fn = xfunction.curry(Math.min, 3);
* fn(5)(8)(3); // Math.min(5, 8, 3)
* // → 3
* ```
*/
export function curry(x: Function, n: number=x.length): Function {
return (...args: unknown[]) => {
if (args.length>=n) return x(...args);
else return curry((...rest: unknown[]) => x(...args, ...rest), n-args.length);
};
}
// - https://www.npmjs.com/package/@spudly/curry
/**
* Generate right-curried version of a function.
* @param x a function
* @param n number of parameters [all]
* @returns (p)(q)(...) => x(..., q, p)
* @example
* ```javascript
* var sub = (x: number, y: number) => x - y;
* var fn = xfunction.curryRight(sub);
* fn(2)(3); // sub(3, 2)
* // → 1
*
* var array = [1];
* var push2 = (x: number, y: number) => array.push(x, y);
* var fn = xfunction.curryRight(push2, 2);
* fn(2)(3); // push2(3, 2)
* array;
* // → [1, 3, 2]
* ```
*/
export function curryRight(x: Function, n: number=x.length): Function {
return curry(reverse(x), n);
}
// - https://www.npmjs.com/package/lodash.curryright
//#endregion
//#region TIME CONTROL
/** Invocation control for time/rate-controlled functions. */
export interface InvocationControl {
/** Disable invoking of target function. */
clear: () => void;
/** Immediately invoke target function. */
flush: () => void;
}
/**
* Generate deferred version of a function, that executes after the current stack has cleared.
* @param x a function
* @returns (...args) => invocation control
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.defer(() => ++count);
* fn();
* fn();
* // `count` incremented after 0s
* // `count` incremented after 0s
* ```
*/
export function defer(x: Function): Function {
return (...args: unknown[]): InvocationControl => {
let h = setTimeout(flush, 0);
function clear() { clearTimeout(h); h = 0; }
function flush() { x(...args); clear(); }
return {clear, flush};
};
}
/**
* Generate delayed version of a function.
* @param x a function
* @param t delay time (ms)
* @returns (...args) => invocation control
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.delay(() => ++count, 500);
* setTimeout(fn, 0);
* setTimeout(fn, 1000);
* // `count` incremented after 0.5s
* // `count` incremented after 1.5s
*
*
* var count = 0;
* var fn = xfunction.delay(() => ++count, 500);
* setTimeout(fn, 0);
* setTimeout(fn, 1000);
* setTimeout(fn, 2000);
* setTimeout(fn, 3000);
* // `count` incremented after 0.5s
* // `count` incremented after 1.5s
* // `count` incremented after 2.5s
* // `count` incremented after 3.5s
* ```
*/
export function delay(x: Function, t: number): Function {
return (...args: unknown[]): InvocationControl => {
let h = setTimeout(flush, t);
function clear() { clearTimeout(h); h = 0; }
function flush() { x(...args); clear(); }
return {clear, flush};
}
}
//#endregion
//#region RATE CONTROL (COUNT)
/**
* Generate restricted-use version of a function.
* @param x a function
* @param start usable from
* @param end usable till (excluding) [-1 ⇒ end]
* @returns (...args) => x(...args) from [start:end] calls
* @example
* ```javascript
* var sum = 0;
* var add = (x: number) => sum += x;
* var fn = xfunction.restrict(add, 0, 4);
* for (var i=0; i<10; ++i)
* fn(i);
* sum; // 0 + 1 + 2 + 3
* // → 6
*
*
* var sum = 0;
* var add = (x: number) => sum += x;
* var fn = xfunction.restrict(add, 4, 8);
* for (var i=0; i<10; ++i)
* fn(i);
* sum; // 4 + 5 + 6 + 7
* // → 22
* ```
*/
export function restrict(x: Function, start: number, end: number=-1): Function {
let i = -1;
return (...args: unknown[]) => {
if ((++i<start)===(i<end || end<0)) return;
return x(...args);
};
}
// - https://www.npmjs.com/package/one-time
// - https://www.npmjs.com/package/onetime
// - https://www.npmjs.com/package/once
// - https://lodash.com/docs/4.17.15#after
// - https://lodash.com/docs/4.17.15#before
/**
* Restrict a function to be used only once.
* @param x a function
* @returns (...args) => x(...args) from [0:1] calls
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.restrictOnce(x => ++count);
* for (var i=0; i<10; ++i)
* fn(i);
* count;
* // → 1
* ```
*/
export function restrictOnce(x: Function): Function {
return restrict(x, 0, 1);
}
export {restrictOnce as once};
// - https://www.npmjs.com/package/one-time
// - https://www.npmjs.com/package/onetime
// - https://www.npmjs.com/package/once
/**
* Restrict a function to be used only upto a certain number of calls.
* @param x a function
* @param n number of calls upto which it is usable
* @returns (...args) => x(...args) from [0:n] calls
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.restrictBefore(x => ++count, 3);
* for (var i=0; i<10; ++i)
* fn(i);
* count;
* // → 3
* ```
*/
export function restrictBefore(x: Function, n: number): Function {
return restrict(x, 0, n);
}
export {restrictBefore as before};
// - https://lodash.com/docs/4.17.15#before
/**
* Restrict a function to be used only after a certain number of calls.
* @param x a function
* @param n number of calls after which it is usable
* @returns (...args) => x(...args) from [n:end] calls
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.restrictAfter(x => ++count, 3);
* for (var i=0; i<10; ++i)
* fn(i);
* count;
* // → 7
* ```
*/
export function restrictAfter(x: Function, n: number): Function {
return restrict(x, n);
}
export {restrictAfter as after};
// - https://lodash.com/docs/4.17.15#after
//#endregion
//#region RATE CONTROL (TIME)
/**
* Generate debounced version of a function.
* @param x a function
* @param t delay time (ms)
* @param T max delay time [-1 ⇒ none]
* @returns (...args) => invocation control
* @example
* ```javascript
* var count = 0;
* var fn = xfunction.debounce(() => ++count, 1500);
* setTimeout(fn, 0);
* setTimeout(fn, 1000);
* setTimeout(fn, 2000);
* setTimeout(fn, 4000);
* setTimeout(fn, 5000);
* // `count` incremented after 3.5s
* // `count` incremented after 6.5s
*
*
* var count = 0;
* var fn = xfunction.debounce(() => ++count, 1500);
* setTimeout(fn, 0);
* setTimeout(fn, 500);
* setTimeout(fn, 1000);
* setTimeout(fn, 1500);
* setTimeout(fn, 2000);
* setTimeout(fn, 4000);
* setTimeout(fn, 4500);
* setTimeout(fn, 5000);
* // `count` incremented after 3.5s
* // `count` incremented after 6.5s
*
*
* var count = 0;
* var fn = xfunction.debounce(() => ++count, 1500, 3500);
* setTimeout(fn, 0);
* setTimeout(fn, 1000);
* setTimeout(fn, 2000);
* setTimeout(fn, 3000);
* setTimeout(fn, 4000);
* setTimeout(fn, 6000);
* // `count` incremented after 3.5s
* // `count` incremented after 5.5s
* // `count` incremented after 7.5s
* ```
*/
export function debounce(x: Function, t: number, T: number=-1): Function {