atomic-fns 0.2.0 → 0.2.2
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/.esdoc.json +19 -1
- package/.eslintrc.json +1 -0
- package/README.md +3 -6
- package/package.json +6 -5
- package/src/collections.d.ts +4 -0
- package/src/collections.js +422 -439
- package/src/collections.ts +6 -2
- package/src/datetime.d.ts +4 -0
- package/src/datetime.js +5 -1
- package/src/datetime.ts +5 -0
- package/src/functools.d.ts +4 -0
- package/src/functools.js +45 -46
- package/src/functools.ts +4 -0
- package/src/globals.d.ts +7 -3
- package/src/globals.js +175 -195
- package/src/globals.ts +8 -4
- package/src/index.js +7 -7
- package/src/itertools.d.ts +4 -0
- package/src/itertools.js +51 -54
- package/src/itertools.ts +4 -0
- package/src/math.js +45 -42
- package/src/math.ts +4 -0
- package/src/objects.js +1 -1
- package/src/operators.js +73 -92
- package/src/operators.ts +4 -0
- package/src/random.js +26 -26
- package/src/random.ts +5 -1
- package/src/strings.js +1 -1
- package/src/util.js +1 -1
- package/tests/collections.spec.ts +81 -9
- package/tests/globals.spec.ts +40 -2
- package/tests/itertools.spec.ts +17 -10
- package/tests/operators.spec.ts +8 -5
- package/tsconfig.json +10 -2
package/src/collections.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collections
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
1
5
|
import {
|
|
2
6
|
call,
|
|
3
7
|
Function,
|
|
@@ -261,7 +265,7 @@ export function filter(arr, fn: Iteratee | PropertyKey | Object = isNull) {
|
|
|
261
265
|
export function find(arr, fn: Iteratee | PropertyKey | Object) {
|
|
262
266
|
if (Array.isArray(arr)) {
|
|
263
267
|
if (typeof fn === 'function') return arr.find(fn as Iteratee)
|
|
264
|
-
if (typeof fn === 'string') return arr.find((x) => x
|
|
268
|
+
if (typeof fn === 'string') return arr.find((x) => x?.[fn])
|
|
265
269
|
if (isObject(fn)) return arr.find(matches(fn))
|
|
266
270
|
}
|
|
267
271
|
}
|
|
@@ -273,7 +277,7 @@ export function findRight(arr, fn: Iteratee | PropertyKey | Object) {
|
|
|
273
277
|
if (typeof fn === 'function') {
|
|
274
278
|
if (fn(x)) return x
|
|
275
279
|
} else if (typeof fn === 'string') {
|
|
276
|
-
if (x
|
|
280
|
+
if (x?.[fn]) return x
|
|
277
281
|
} else if (isObject(fn)) {
|
|
278
282
|
if (matches(fn)(x)) return x
|
|
279
283
|
}
|
package/src/datetime.d.ts
CHANGED
package/src/datetime.js
CHANGED
package/src/datetime.ts
CHANGED
package/src/functools.d.ts
CHANGED
package/src/functools.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
export const bind = (fn, self, ...args) => fn.bind(self, ...args)
|
|
2
|
-
export const partial = (fn, ...args) => bind(fn, {}, ...args)
|
|
3
|
-
export const flow =
|
|
1
|
+
export const bind = (fn, self, ...args) => fn.bind(self, ...args)
|
|
2
|
+
export const partial = (fn, ...args) => bind(fn, {}, ...args)
|
|
3
|
+
export const flow =
|
|
4
|
+
(...args) =>
|
|
5
|
+
(x) => {
|
|
4
6
|
for (const fn of args) {
|
|
5
|
-
|
|
7
|
+
x = fn(x)
|
|
6
8
|
}
|
|
7
|
-
return x
|
|
8
|
-
}
|
|
9
|
-
export const pipe = flow
|
|
10
|
-
export const compose = (...args) => flow(args.reverse())
|
|
9
|
+
return x
|
|
10
|
+
}
|
|
11
|
+
export const pipe = flow
|
|
12
|
+
export const compose = (...args) => flow(args.reverse())
|
|
11
13
|
/**
|
|
12
14
|
* 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.
|
|
13
15
|
*
|
|
@@ -17,16 +19,15 @@ export const compose = (...args) => flow(args.reverse());
|
|
|
17
19
|
* @return {Function} Returns the new memoized function.
|
|
18
20
|
*/
|
|
19
21
|
export function memoize(func, resolver) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
22
|
+
const cache = new Map()
|
|
23
|
+
return (...args) => {
|
|
24
|
+
const key = resolver ? resolver(...args) : args[0]
|
|
25
|
+
const prev = cache.get(key)
|
|
26
|
+
if (prev !== undefined) return prev
|
|
27
|
+
const value = func(...args)
|
|
28
|
+
cache.set(key, value)
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* 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,24 +39,23 @@ export function memoize(func, resolver) {
|
|
|
38
39
|
* @return {Function} Returns the new debounced function.
|
|
39
40
|
*/
|
|
40
41
|
export function debounce(func, wait, { maxWait }) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
};
|
|
42
|
+
let timer, maxTimer, lastResult
|
|
43
|
+
return (...args) => {
|
|
44
|
+
if (timer) clearTimeout(timer)
|
|
45
|
+
timer = setTimeout(() => {
|
|
46
|
+
clearTimeout(maxTimer)
|
|
47
|
+
maxTimer = null
|
|
48
|
+
lastResult = func(...args)
|
|
49
|
+
}, wait)
|
|
50
|
+
if (maxWait && !maxTimer) {
|
|
51
|
+
maxTimer = setTimeout(() => {
|
|
52
|
+
clearTimeout(timer)
|
|
53
|
+
maxTimer = null
|
|
54
|
+
lastResult = func(...args)
|
|
55
|
+
}, maxWait)
|
|
56
|
+
}
|
|
57
|
+
return lastResult
|
|
58
|
+
}
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* 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.
|
|
@@ -66,14 +66,13 @@ export function debounce(func, wait, { maxWait }) {
|
|
|
66
66
|
* @return {Function} Returns the new throttled function.
|
|
67
67
|
*/
|
|
68
68
|
export function throttle(func, time) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
};
|
|
69
|
+
let pending = false
|
|
70
|
+
return (...args) => {
|
|
71
|
+
if (pending) return
|
|
72
|
+
func(...args)
|
|
73
|
+
pending = true
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
pending = false
|
|
76
|
+
}, time)
|
|
77
|
+
}
|
|
79
78
|
}
|
package/src/functools.ts
CHANGED
package/src/globals.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Globals
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
1
5
|
export declare type Comp = (x: any, y: any) => number;
|
|
2
6
|
export declare type Function = (...args: any[]) => any;
|
|
3
7
|
export declare type Iteratee = (value: any, key?: any, arr?: any) => any;
|
|
@@ -13,10 +17,10 @@ export declare const False: () => boolean;
|
|
|
13
17
|
export declare class CustomError extends Error {
|
|
14
18
|
/**
|
|
15
19
|
* Returns a new CustomError with the specified message.
|
|
16
|
-
* @param message
|
|
20
|
+
* @param {string} message
|
|
17
21
|
* @constructor
|
|
18
22
|
*/
|
|
19
|
-
constructor(message?:
|
|
23
|
+
constructor(message?: string);
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
* Custom error to signal an invalid index access.
|
|
@@ -136,7 +140,7 @@ export declare function round(x: number, digits?: number): number;
|
|
|
136
140
|
export declare const floor: (x: number) => number;
|
|
137
141
|
export declare const ceil: (x: number) => number;
|
|
138
142
|
/**
|
|
139
|
-
* Yields elements like [index, item] from an iterable.
|
|
143
|
+
* Yields elements like `[index, item]` from an iterable.
|
|
140
144
|
*
|
|
141
145
|
*
|
|
142
146
|
* @template T
|