atomic-fns 0.2.3 → 0.3.1
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/.firebaserc +5 -0
- package/README.md +5 -2
- package/firebase.json +10 -0
- package/package.json +7 -3
- package/src/collections/index.d.ts +14 -0
- package/src/collections/index.js +23 -0
- package/src/collections/index.ts +24 -0
- package/src/functools/index.d.ts +78 -12
- package/src/functools/index.js +147 -12
- package/src/functools/index.ts +174 -14
- package/src/globals/index.d.ts +36 -41
- package/src/globals/index.js +28 -31
- package/src/globals/index.ts +41 -46
- package/src/itertools/index.d.ts +1 -30
- package/src/itertools/index.js +4 -40
- package/src/itertools/index.ts +3 -43
- package/src/math/index.d.ts +76 -0
- package/src/math/index.js +53 -0
- package/src/math/index.ts +110 -2
- package/src/operators/index.d.ts +3 -3
- package/src/operators/index.js +8 -8
- package/src/operators/index.ts +8 -8
- package/tests/globals.spec.ts +8 -16
- package/tests/itertools.spec.ts +6 -20
- package/tests/math.spec.ts +25 -0
package/.firebaserc
ADDED
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# atomic-fns
|
|
2
2
|
|
|
3
|
+
[Read the Docs](https://atomic-fns.dev)
|
|
4
|
+
|
|
3
5
|
## Like Lodash, but for ESNext and with types.
|
|
4
6
|
|
|
5
7
|
Stop shipping code built for browsers from 2015. Really though, look at the [issues](https://github.com/lodash/lodash/issues/2930).
|
|
@@ -9,7 +11,8 @@ You may prefer this because it's:
|
|
|
9
11
|
* **Modular**: Pick what you need. Works with all bundlers and also supports tree-shaking.
|
|
10
12
|
* **ESNext**: Uses modern idiomatic syntax, data structures, and control flow techniques.
|
|
11
13
|
* **Lazy**: Leverage generators, iterators, and functional composition.
|
|
12
|
-
* **
|
|
14
|
+
* **Zero deps**: Built from scratch with no dependencies whatsoever.
|
|
15
|
+
* **Well tested**: All modules have comprehensive test suites aiming for 100% coverage.
|
|
13
16
|
* **TypeScript & docs**: Includes type declaration files and js with JSDocs for best in class experience.
|
|
14
17
|
|
|
15
18
|
## Get Started
|
|
@@ -39,7 +42,7 @@ uniqueId('user_')
|
|
|
39
42
|
|
|
40
43
|
## Docs
|
|
41
44
|
|
|
42
|
-
|
|
45
|
+
📖 Available on [atomic-fns.dev](https://atomic-fns.dev)
|
|
43
46
|
|
|
44
47
|
💡 See the `src` directory for comments.
|
|
45
48
|
|
package/firebase.json
ADDED
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "atomic-fns",
|
|
3
3
|
"description": "Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"module": "src/index.js",
|
|
9
|
+
"sideEffects": false,
|
|
7
10
|
"devDependencies": {
|
|
8
11
|
"@types/jasmine": "^4.3.0",
|
|
9
12
|
"@typescript-eslint/eslint-plugin": "^5.39.0",
|
|
10
|
-
"esdoc": "^1.1.0",
|
|
11
|
-
"esdoc-standard-plugin": "^1.0.0",
|
|
12
13
|
"eslint": "^8.25.0",
|
|
13
14
|
"eslint-config-prettier": "^8.5.0",
|
|
14
15
|
"eslint-config-standard-with-typescript": "^23.0.0",
|
|
@@ -65,7 +66,10 @@
|
|
|
65
66
|
"es2022",
|
|
66
67
|
"es2023",
|
|
67
68
|
"lodash",
|
|
69
|
+
"lodash-es",
|
|
70
|
+
"itertools",
|
|
68
71
|
"lazy",
|
|
72
|
+
"module",
|
|
69
73
|
"modules",
|
|
70
74
|
"stdlib",
|
|
71
75
|
"core-js",
|
|
@@ -595,6 +595,20 @@ merge(object, other)
|
|
|
595
595
|
```
|
|
596
596
|
*/
|
|
597
597
|
export declare function merge(object: any, ...sources: any[]): Object;
|
|
598
|
+
/**
|
|
599
|
+
* Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
|
|
600
|
+
* **Note:** This method mutates `object`.
|
|
601
|
+
|
|
602
|
+
* @param {Object} object The destination object.
|
|
603
|
+
* @param {...Object} [sources] The source objects.
|
|
604
|
+
* @returns Returns `object`.
|
|
605
|
+
* @example
|
|
606
|
+
```js
|
|
607
|
+
defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 })
|
|
608
|
+
// => { 'a': 1, 'b': 2 }
|
|
609
|
+
```
|
|
610
|
+
*/
|
|
611
|
+
export declare function defaults(object: Object, ...sources: Object[]): Object;
|
|
598
612
|
/**
|
|
599
613
|
* Returns a generator of array values not included in the other given arrays using a `Set` for equality comparisons. The order and references of result values are not guaranteed.
|
|
600
614
|
* @param args The initial arrays
|
package/src/collections/index.js
CHANGED
|
@@ -944,6 +944,29 @@ export function merge(object, ...sources) {
|
|
|
944
944
|
}
|
|
945
945
|
return object;
|
|
946
946
|
}
|
|
947
|
+
/**
|
|
948
|
+
* Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
|
|
949
|
+
* **Note:** This method mutates `object`.
|
|
950
|
+
|
|
951
|
+
* @param {Object} object The destination object.
|
|
952
|
+
* @param {...Object} [sources] The source objects.
|
|
953
|
+
* @returns Returns `object`.
|
|
954
|
+
* @example
|
|
955
|
+
```js
|
|
956
|
+
defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 })
|
|
957
|
+
// => { 'a': 1, 'b': 2 }
|
|
958
|
+
```
|
|
959
|
+
*/
|
|
960
|
+
export function defaults(object, ...sources) {
|
|
961
|
+
for (const source of sources) {
|
|
962
|
+
for (const key in source) {
|
|
963
|
+
if (object[key] === undefined) {
|
|
964
|
+
object[key] = source[key];
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
return object;
|
|
969
|
+
}
|
|
947
970
|
/**
|
|
948
971
|
* Returns a generator of array values not included in the other given arrays using a `Set` for equality comparisons. The order and references of result values are not guaranteed.
|
|
949
972
|
* @param args The initial arrays
|
package/src/collections/index.ts
CHANGED
|
@@ -1033,6 +1033,30 @@ export function merge(object, ...sources): Object {
|
|
|
1033
1033
|
return object
|
|
1034
1034
|
}
|
|
1035
1035
|
|
|
1036
|
+
/**
|
|
1037
|
+
* Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
|
|
1038
|
+
* **Note:** This method mutates `object`.
|
|
1039
|
+
|
|
1040
|
+
* @param {Object} object The destination object.
|
|
1041
|
+
* @param {...Object} [sources] The source objects.
|
|
1042
|
+
* @returns Returns `object`.
|
|
1043
|
+
* @example
|
|
1044
|
+
```js
|
|
1045
|
+
defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 })
|
|
1046
|
+
// => { 'a': 1, 'b': 2 }
|
|
1047
|
+
```
|
|
1048
|
+
*/
|
|
1049
|
+
export function defaults(object: Object, ...sources: Object[]) {
|
|
1050
|
+
for (const source of sources) {
|
|
1051
|
+
for (const key in source) {
|
|
1052
|
+
if (object[key] === undefined) {
|
|
1053
|
+
object[key] = source[key]
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
return object
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1036
1060
|
/**
|
|
1037
1061
|
* Returns a generator of array values not included in the other given arrays using a `Set` for equality comparisons. The order and references of result values are not guaranteed.
|
|
1038
1062
|
* @param args The initial arrays
|
package/src/functools/index.d.ts
CHANGED
|
@@ -4,38 +4,104 @@
|
|
|
4
4
|
* @module Functions
|
|
5
5
|
*/
|
|
6
6
|
import { Function } from '../globals/index.js';
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Creates a function that invokes `fn` with the this binding of `thisArg` and `partials` as partially applied arguments.
|
|
9
|
+
* @param {Function} fn The function to bind.
|
|
10
|
+
* @param {*} thisArg The `this` value of `fn`.
|
|
11
|
+
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
12
|
+
* @returns {Function} Returns the new bound function.
|
|
13
|
+
* @see {@link partial}
|
|
14
|
+
*/
|
|
15
|
+
export declare const bind: (fn: Function, thisArg: any, ...partials: any[]) => (...args: any[]) => any;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on. The arity of `func` may be specified if `func.length` is not sufficient.
|
|
18
|
+
* @param {Function} func The function to curry.
|
|
19
|
+
* @param {number} [arity=func.length]
|
|
20
|
+
* @returns Returns the new curried function.
|
|
21
|
+
*/
|
|
22
|
+
export declare const curry: (func: Function, arity?: number) => Function<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
25
|
+
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider {@link bind} instead.
|
|
26
|
+
* @param {Function} fn The function to bind.
|
|
27
|
+
* @param {...*} [args] The arguments to apply to `fn`.
|
|
28
|
+
* @returns {Function} Returns the new partially applied function
|
|
29
|
+
*/
|
|
30
|
+
export declare const partial: (fn: any, ...args: any[]) => (...args: any[]) => any;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
|
|
33
|
+
* @param {...*} [args] The set of functions to apply.
|
|
34
|
+
* @returns {Function} A new function that applies each given function on the result of the previous one.
|
|
35
|
+
*/
|
|
9
36
|
export declare const flow: (...args: any[]) => (x: any) => any;
|
|
37
|
+
/**
|
|
38
|
+
* This is just an alias for the {@link flow} function.
|
|
39
|
+
* @see {@link flow}
|
|
40
|
+
*/
|
|
10
41
|
export declare const pipe: (...args: any[]) => (x: any) => any;
|
|
11
42
|
export declare const compose: (...args: any[]) => (x: any) => any;
|
|
12
43
|
/**
|
|
13
44
|
* Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
|
|
14
45
|
*
|
|
15
|
-
*
|
|
16
46
|
* @param {Function} func The function to have its output memoized.
|
|
17
47
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
18
48
|
* @return {Function} Returns the new memoized function.
|
|
19
49
|
*/
|
|
20
50
|
export declare function memoize(func: Function, resolver?: Function): Function;
|
|
51
|
+
export declare function cache(target: any, name: any, descriptor: any): any;
|
|
21
52
|
/**
|
|
22
53
|
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
|
|
23
54
|
*
|
|
24
|
-
*
|
|
25
|
-
* @param
|
|
26
|
-
* @param
|
|
27
|
-
* @param maxWait The maximum time `func` is allowed to be delayed before it's invoked.
|
|
55
|
+
* @param {Function} func The function to debounce.
|
|
56
|
+
* @param {number} wait The number of milliseconds to delay.
|
|
57
|
+
* @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
|
|
28
58
|
* @returns Returns the new debounced function.
|
|
29
59
|
*/
|
|
30
|
-
export declare function debounce(func: Function, wait: number, { maxWait }
|
|
31
|
-
maxWait
|
|
60
|
+
export declare function debounce(func: Function, wait: number, { maxWait }?: {
|
|
61
|
+
maxWait?: number;
|
|
32
62
|
}): Function;
|
|
33
63
|
/**
|
|
34
64
|
* Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds. Subsequent calls to the throttled function return the result of the last successful invocation.
|
|
35
65
|
*
|
|
36
|
-
*
|
|
37
66
|
* @param {Function} func The function to throttle.
|
|
38
|
-
* @param {number}
|
|
67
|
+
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
39
68
|
* @return {Function} Returns the new throttled function.
|
|
40
69
|
*/
|
|
41
|
-
export declare function throttle(func: Function,
|
|
70
|
+
export declare function throttle(func: Function, wait: number): Function;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation. The `func` is invoked with the arguments of the created function.
|
|
73
|
+
*
|
|
74
|
+
* @param {Function} func The function to restrict
|
|
75
|
+
* @returns {Function} Returns the new restricted function.
|
|
76
|
+
* @example
|
|
77
|
+
```js
|
|
78
|
+
let func = x => x
|
|
79
|
+
let single = once(func)
|
|
80
|
+
single(1) // => 1
|
|
81
|
+
single(2) // => 1
|
|
82
|
+
single(3) // => 1
|
|
83
|
+
// ...
|
|
84
|
+
// => `func` is invoked only once.
|
|
85
|
+
```
|
|
86
|
+
*/
|
|
87
|
+
export declare function once(func: Function): (...args: any[]) => any;
|
|
88
|
+
export declare type Maybe<T> = T | undefined;
|
|
89
|
+
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
90
|
+
/**
|
|
91
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
92
|
+
*
|
|
93
|
+
* @param {Function} fun The function to wrap and catch if it throws.
|
|
94
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
95
|
+
* @returns {Function} A new function that wraps `fun`.
|
|
96
|
+
* @template T,E
|
|
97
|
+
*/
|
|
98
|
+
export declare function result<T = any, E = unknown>(fun: Function<T>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Result<T, E>;
|
|
99
|
+
/**
|
|
100
|
+
* Wraps a function or promise to catch any exceptions inside and return a `go` style error-return type. This is like {@link result} but it deals with async functions or promises.
|
|
101
|
+
*
|
|
102
|
+
* @param {Function|Promise} awaitable The function or promise to await.
|
|
103
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
104
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a return type.
|
|
105
|
+
* @template T,E
|
|
106
|
+
*/
|
|
107
|
+
export declare function resultAsync<T = any, E = unknown>(awaitable: Promise<T> | Function<Promise<T>>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Promise<Result<T, E>>;
|
package/src/functools/index.js
CHANGED
|
@@ -3,20 +3,76 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Functions
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
import { isPromise } from '../globals/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a function that invokes `fn` with the this binding of `thisArg` and `partials` as partially applied arguments.
|
|
9
|
+
* @param {Function} fn The function to bind.
|
|
10
|
+
* @param {*} thisArg The `this` value of `fn`.
|
|
11
|
+
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
12
|
+
* @returns {Function} Returns the new bound function.
|
|
13
|
+
* @see {@link partial}
|
|
14
|
+
*/
|
|
15
|
+
export const bind = (fn, thisArg, ...partials) => fn.bind(thisArg, ...partials);
|
|
16
|
+
/**
|
|
17
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on. The arity of `func` may be specified if `func.length` is not sufficient.
|
|
18
|
+
* @param {Function} func The function to curry.
|
|
19
|
+
* @param {number} [arity=func.length]
|
|
20
|
+
* @returns Returns the new curried function.
|
|
21
|
+
*/
|
|
22
|
+
export const curry = (func, arity) => {
|
|
23
|
+
if (typeof func !== 'function' || func.length < 2)
|
|
24
|
+
return func;
|
|
25
|
+
if (typeof arity !== 'number')
|
|
26
|
+
arity = func.length;
|
|
27
|
+
switch (arity) {
|
|
28
|
+
case 2:
|
|
29
|
+
return (arg0) => (arg1) => func(arg0, arg1);
|
|
30
|
+
case 3:
|
|
31
|
+
return (arg0) => (arg1) => (arg2) => func(arg0, arg1, arg2);
|
|
32
|
+
case 4:
|
|
33
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => func(arg0, arg1, arg2, arg3);
|
|
34
|
+
case 5:
|
|
35
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => func(arg0, arg1, arg2, arg3, arg4);
|
|
36
|
+
case 6:
|
|
37
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => func(arg0, arg1, arg2, arg3, arg4, arg5);
|
|
38
|
+
case 7:
|
|
39
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) => func(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
40
|
+
case 8:
|
|
41
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) => (arg7) => func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
|
|
42
|
+
case 9:
|
|
43
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) => (arg7) => (arg8) => func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
|
|
44
|
+
default:
|
|
45
|
+
throw Error('The specified function uses too many arguments. Consider refactoring it.');
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
50
|
+
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider {@link bind} instead.
|
|
51
|
+
* @param {Function} fn The function to bind.
|
|
52
|
+
* @param {...*} [args] The arguments to apply to `fn`.
|
|
53
|
+
* @returns {Function} Returns the new partially applied function
|
|
54
|
+
*/
|
|
55
|
+
export const partial = (fn, ...args) => bind(fn, undefined, ...args);
|
|
56
|
+
/**
|
|
57
|
+
* Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
|
|
58
|
+
* @param {...*} [args] The set of functions to apply.
|
|
59
|
+
* @returns {Function} A new function that applies each given function on the result of the previous one.
|
|
60
|
+
*/
|
|
8
61
|
export const flow = (...args) => (x) => {
|
|
9
62
|
for (const fn of args) {
|
|
10
63
|
x = fn(x);
|
|
11
64
|
}
|
|
12
65
|
return x;
|
|
13
66
|
};
|
|
67
|
+
/**
|
|
68
|
+
* This is just an alias for the {@link flow} function.
|
|
69
|
+
* @see {@link flow}
|
|
70
|
+
*/
|
|
14
71
|
export const pipe = flow;
|
|
15
72
|
export const compose = (...args) => flow(args.reverse());
|
|
16
73
|
/**
|
|
17
74
|
* Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
|
|
18
75
|
*
|
|
19
|
-
*
|
|
20
76
|
* @param {Function} func The function to have its output memoized.
|
|
21
77
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
22
78
|
* @return {Function} Returns the new memoized function.
|
|
@@ -33,16 +89,23 @@ export function memoize(func, resolver) {
|
|
|
33
89
|
return value;
|
|
34
90
|
};
|
|
35
91
|
}
|
|
92
|
+
// TODO: also sized lru_cache
|
|
93
|
+
export function cache(target, name, descriptor) {
|
|
94
|
+
const original = descriptor.value;
|
|
95
|
+
if (typeof original === 'function') {
|
|
96
|
+
descriptor.value = memoize(original);
|
|
97
|
+
}
|
|
98
|
+
return descriptor;
|
|
99
|
+
}
|
|
36
100
|
/**
|
|
37
101
|
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
|
|
38
102
|
*
|
|
39
|
-
*
|
|
40
|
-
* @param
|
|
41
|
-
* @param
|
|
42
|
-
* @param maxWait The maximum time `func` is allowed to be delayed before it's invoked.
|
|
103
|
+
* @param {Function} func The function to debounce.
|
|
104
|
+
* @param {number} wait The number of milliseconds to delay.
|
|
105
|
+
* @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
|
|
43
106
|
* @returns Returns the new debounced function.
|
|
44
107
|
*/
|
|
45
|
-
export function debounce(func, wait, { maxWait }) {
|
|
108
|
+
export function debounce(func, wait, { maxWait } = {}) {
|
|
46
109
|
let timer, maxTimer, lastResult;
|
|
47
110
|
return (...args) => {
|
|
48
111
|
if (timer)
|
|
@@ -65,12 +128,11 @@ export function debounce(func, wait, { maxWait }) {
|
|
|
65
128
|
/**
|
|
66
129
|
* Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds. Subsequent calls to the throttled function return the result of the last successful invocation.
|
|
67
130
|
*
|
|
68
|
-
*
|
|
69
131
|
* @param {Function} func The function to throttle.
|
|
70
|
-
* @param {number}
|
|
132
|
+
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
71
133
|
* @return {Function} Returns the new throttled function.
|
|
72
134
|
*/
|
|
73
|
-
export function throttle(func,
|
|
135
|
+
export function throttle(func, wait) {
|
|
74
136
|
let pending = false;
|
|
75
137
|
return (...args) => {
|
|
76
138
|
if (pending)
|
|
@@ -79,6 +141,79 @@ export function throttle(func, time) {
|
|
|
79
141
|
pending = true;
|
|
80
142
|
setTimeout(() => {
|
|
81
143
|
pending = false;
|
|
82
|
-
},
|
|
144
|
+
}, wait);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation. The `func` is invoked with the arguments of the created function.
|
|
149
|
+
*
|
|
150
|
+
* @param {Function} func The function to restrict
|
|
151
|
+
* @returns {Function} Returns the new restricted function.
|
|
152
|
+
* @example
|
|
153
|
+
```js
|
|
154
|
+
let func = x => x
|
|
155
|
+
let single = once(func)
|
|
156
|
+
single(1) // => 1
|
|
157
|
+
single(2) // => 1
|
|
158
|
+
single(3) // => 1
|
|
159
|
+
// ...
|
|
160
|
+
// => `func` is invoked only once.
|
|
161
|
+
```
|
|
162
|
+
*/
|
|
163
|
+
export function once(func) {
|
|
164
|
+
let result, isCalled;
|
|
165
|
+
return (...args) => {
|
|
166
|
+
if (!isCalled) {
|
|
167
|
+
result = func(...args);
|
|
168
|
+
isCalled = true;
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
175
|
+
*
|
|
176
|
+
* @param {Function} fun The function to wrap and catch if it throws.
|
|
177
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
178
|
+
* @returns {Function} A new function that wraps `fun`.
|
|
179
|
+
* @template T,E
|
|
180
|
+
*/
|
|
181
|
+
export function result(fun, onFinally) {
|
|
182
|
+
return (...args) => {
|
|
183
|
+
let value, err;
|
|
184
|
+
try {
|
|
185
|
+
value = fun(...args);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
err = error;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
onFinally?.(err, value);
|
|
192
|
+
}
|
|
193
|
+
return [value, err];
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Wraps a function or promise to catch any exceptions inside and return a `go` style error-return type. This is like {@link result} but it deals with async functions or promises.
|
|
198
|
+
*
|
|
199
|
+
* @param {Function|Promise} awaitable The function or promise to await.
|
|
200
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
201
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a return type.
|
|
202
|
+
* @template T,E
|
|
203
|
+
*/
|
|
204
|
+
export function resultAsync(awaitable, onFinally) {
|
|
205
|
+
return async (...args) => {
|
|
206
|
+
let value, err;
|
|
207
|
+
try {
|
|
208
|
+
const promise = isPromise(awaitable) ? awaitable : awaitable(...args);
|
|
209
|
+
value = await promise;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
err = error;
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
onFinally?.(err, value);
|
|
216
|
+
}
|
|
217
|
+
return [value, err];
|
|
83
218
|
};
|
|
84
219
|
}
|