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/src/functools/index.ts
CHANGED
|
@@ -4,21 +4,89 @@
|
|
|
4
4
|
* @module Functions
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { Function } from '../globals/index.js'
|
|
7
|
+
import { Function, isPromise } from '../globals/index.js'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Creates a function that invokes `fn` with the this binding of `thisArg` and `partials` as partially applied arguments.
|
|
11
|
+
* @param {Function} fn The function to bind.
|
|
12
|
+
* @param {*} thisArg The `this` value of `fn`.
|
|
13
|
+
* @param {...*} [partials] The arguments to prepend to `fn`.
|
|
14
|
+
* @returns {Function} Returns the new bound function.
|
|
15
|
+
* @see {@link partial}
|
|
16
|
+
*/
|
|
17
|
+
export const bind = (fn: Function, thisArg, ...partials) => fn.bind(thisArg, ...partials)
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 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.
|
|
21
|
+
* @param {Function} func The function to curry.
|
|
22
|
+
* @param {number} [arity=func.length]
|
|
23
|
+
* @returns Returns the new curried function.
|
|
24
|
+
*/
|
|
25
|
+
export const curry = (func: Function, arity?: number) => {
|
|
26
|
+
if (typeof func !== 'function' || func.length < 2) return func
|
|
27
|
+
if (typeof arity !== 'number') arity = func.length
|
|
28
|
+
switch (arity) {
|
|
29
|
+
case 2:
|
|
30
|
+
return (arg0) => (arg1) => func(arg0, arg1)
|
|
31
|
+
case 3:
|
|
32
|
+
return (arg0) => (arg1) => (arg2) => func(arg0, arg1, arg2)
|
|
33
|
+
case 4:
|
|
34
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => func(arg0, arg1, arg2, arg3)
|
|
35
|
+
case 5:
|
|
36
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => func(arg0, arg1, arg2, arg3, arg4)
|
|
37
|
+
case 6:
|
|
38
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) =>
|
|
39
|
+
func(arg0, arg1, arg2, arg3, arg4, arg5)
|
|
40
|
+
case 7:
|
|
41
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) =>
|
|
42
|
+
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6)
|
|
43
|
+
case 8:
|
|
44
|
+
return (arg0) => (arg1) => (arg2) => (arg3) => (arg4) => (arg5) => (arg6) => (arg7) =>
|
|
45
|
+
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
|
|
46
|
+
case 9:
|
|
47
|
+
return (arg0) =>
|
|
48
|
+
(arg1) =>
|
|
49
|
+
(arg2) =>
|
|
50
|
+
(arg3) =>
|
|
51
|
+
(arg4) =>
|
|
52
|
+
(arg5) =>
|
|
53
|
+
(arg6) =>
|
|
54
|
+
(arg7) =>
|
|
55
|
+
(arg8) =>
|
|
56
|
+
func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
|
|
57
|
+
|
|
58
|
+
default:
|
|
59
|
+
throw Error('The specified function uses too many arguments. Consider refactoring it.')
|
|
60
|
+
}
|
|
61
|
+
}
|
|
10
62
|
|
|
11
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Creates a function that invokes `fn` with partials prepended to the arguments it receives.
|
|
65
|
+
* **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider {@link bind} instead.
|
|
66
|
+
* @param {Function} fn The function to bind.
|
|
67
|
+
* @param {...*} [args] The arguments to apply to `fn`.
|
|
68
|
+
* @returns {Function} Returns the new partially applied function
|
|
69
|
+
*/
|
|
70
|
+
export const partial = (fn, ...args) => bind(fn, undefined, ...args)
|
|
12
71
|
|
|
72
|
+
/**
|
|
73
|
+
* 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.
|
|
74
|
+
* @param {...*} [args] The set of functions to apply.
|
|
75
|
+
* @returns {Function} A new function that applies each given function on the result of the previous one.
|
|
76
|
+
*/
|
|
13
77
|
export const flow =
|
|
14
78
|
(...args) =>
|
|
15
|
-
(x
|
|
79
|
+
(x) => {
|
|
16
80
|
for (const fn of args) {
|
|
17
81
|
x = fn(x)
|
|
18
82
|
}
|
|
19
83
|
return x
|
|
20
84
|
}
|
|
21
85
|
|
|
86
|
+
/**
|
|
87
|
+
* This is just an alias for the {@link flow} function.
|
|
88
|
+
* @see {@link flow}
|
|
89
|
+
*/
|
|
22
90
|
export const pipe = flow
|
|
23
91
|
|
|
24
92
|
export const compose = (...args) => flow(args.reverse())
|
|
@@ -26,7 +94,6 @@ export const compose = (...args) => flow(args.reverse())
|
|
|
26
94
|
/**
|
|
27
95
|
* 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.
|
|
28
96
|
*
|
|
29
|
-
*
|
|
30
97
|
* @param {Function} func The function to have its output memoized.
|
|
31
98
|
* @param {Function} [resolver] The function to resolve the cache key.
|
|
32
99
|
* @return {Function} Returns the new memoized function.
|
|
@@ -43,16 +110,28 @@ export function memoize(func: Function, resolver?: Function): Function {
|
|
|
43
110
|
}
|
|
44
111
|
}
|
|
45
112
|
|
|
113
|
+
// TODO: also sized lru_cache
|
|
114
|
+
export function cache(target, name, descriptor) {
|
|
115
|
+
const original = descriptor.value
|
|
116
|
+
if (typeof original === 'function') {
|
|
117
|
+
descriptor.value = memoize(original)
|
|
118
|
+
}
|
|
119
|
+
return descriptor
|
|
120
|
+
}
|
|
121
|
+
|
|
46
122
|
/**
|
|
47
123
|
* 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.
|
|
48
124
|
*
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
52
|
-
* @param maxWait The maximum time `func` is allowed to be delayed before it's invoked.
|
|
125
|
+
* @param {Function} func The function to debounce.
|
|
126
|
+
* @param {number} wait The number of milliseconds to delay.
|
|
127
|
+
* @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
|
|
53
128
|
* @returns Returns the new debounced function.
|
|
54
129
|
*/
|
|
55
|
-
export function debounce(
|
|
130
|
+
export function debounce(
|
|
131
|
+
func: Function,
|
|
132
|
+
wait: number,
|
|
133
|
+
{ maxWait }: { maxWait?: number } = {}
|
|
134
|
+
): Function {
|
|
56
135
|
let timer, maxTimer, lastResult
|
|
57
136
|
return (...args) => {
|
|
58
137
|
if (timer) clearTimeout(timer)
|
|
@@ -77,12 +156,11 @@ export function debounce(func: Function, wait: number, { maxWait }: { maxWait: n
|
|
|
77
156
|
/**
|
|
78
157
|
* 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.
|
|
79
158
|
*
|
|
80
|
-
*
|
|
81
159
|
* @param {Function} func The function to throttle.
|
|
82
|
-
* @param {number}
|
|
160
|
+
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
83
161
|
* @return {Function} Returns the new throttled function.
|
|
84
162
|
*/
|
|
85
|
-
export function throttle(func: Function,
|
|
163
|
+
export function throttle(func: Function, wait: number): Function {
|
|
86
164
|
let pending = false
|
|
87
165
|
return (...args) => {
|
|
88
166
|
if (pending) return
|
|
@@ -90,6 +168,88 @@ export function throttle(func: Function, time: number): Function {
|
|
|
90
168
|
pending = true
|
|
91
169
|
setTimeout(() => {
|
|
92
170
|
pending = false
|
|
93
|
-
},
|
|
171
|
+
}, wait)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 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.
|
|
177
|
+
*
|
|
178
|
+
* @param {Function} func The function to restrict
|
|
179
|
+
* @returns {Function} Returns the new restricted function.
|
|
180
|
+
* @example
|
|
181
|
+
```js
|
|
182
|
+
let func = x => x
|
|
183
|
+
let single = once(func)
|
|
184
|
+
single(1) // => 1
|
|
185
|
+
single(2) // => 1
|
|
186
|
+
single(3) // => 1
|
|
187
|
+
// ...
|
|
188
|
+
// => `func` is invoked only once.
|
|
189
|
+
```
|
|
190
|
+
*/
|
|
191
|
+
export function once(func: Function) {
|
|
192
|
+
let result, isCalled
|
|
193
|
+
return (...args) => {
|
|
194
|
+
if (!isCalled) {
|
|
195
|
+
result = func(...args)
|
|
196
|
+
isCalled = true
|
|
197
|
+
}
|
|
198
|
+
return result
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export type Maybe<T> = T | undefined
|
|
203
|
+
|
|
204
|
+
export type Result<T, E = any> = [Maybe<T>, Maybe<E>]
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
208
|
+
*
|
|
209
|
+
* @param {Function} fun The function to wrap and catch if it throws.
|
|
210
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
211
|
+
* @returns {Function} A new function that wraps `fun`.
|
|
212
|
+
* @template T,E
|
|
213
|
+
*/
|
|
214
|
+
export function result<T = any, E = unknown>(
|
|
215
|
+
fun: Function<T>,
|
|
216
|
+
onFinally?: (err: E, res: T) => any
|
|
217
|
+
) {
|
|
218
|
+
return (...args): Result<T, E> => {
|
|
219
|
+
let value, err
|
|
220
|
+
try {
|
|
221
|
+
value = fun(...args)
|
|
222
|
+
} catch (error) {
|
|
223
|
+
err = error
|
|
224
|
+
} finally {
|
|
225
|
+
onFinally?.(err, value)
|
|
226
|
+
}
|
|
227
|
+
return [value as T, err]
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 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.
|
|
233
|
+
*
|
|
234
|
+
* @param {Function|Promise} awaitable The function or promise to await.
|
|
235
|
+
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
236
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a return type.
|
|
237
|
+
* @template T,E
|
|
238
|
+
*/
|
|
239
|
+
export function resultAsync<T = any, E = unknown>(
|
|
240
|
+
awaitable: Promise<T> | Function<Promise<T>>,
|
|
241
|
+
onFinally?: (err: E, res: T) => any
|
|
242
|
+
) {
|
|
243
|
+
return async (...args): Promise<Result<T, E>> => {
|
|
244
|
+
let value, err
|
|
245
|
+
try {
|
|
246
|
+
const promise = isPromise(awaitable) ? awaitable : awaitable(...args)
|
|
247
|
+
value = await promise
|
|
248
|
+
} catch (error) {
|
|
249
|
+
err = error
|
|
250
|
+
} finally {
|
|
251
|
+
onFinally?.(err, value)
|
|
252
|
+
}
|
|
253
|
+
return [value as T, err]
|
|
94
254
|
}
|
|
95
255
|
}
|
package/src/globals/index.d.ts
CHANGED
|
@@ -5,25 +5,27 @@
|
|
|
5
5
|
*/
|
|
6
6
|
/** Describes a function used to compare two values.
|
|
7
7
|
*
|
|
8
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description}
|
|
8
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
|
|
9
9
|
*/
|
|
10
10
|
export declare type Comp = (x: any, y: any) => -1 | 0 | 1;
|
|
11
|
+
/** This is just a noop function. */
|
|
12
|
+
export declare const _: () => void;
|
|
11
13
|
/**
|
|
12
14
|
* A generic function type with arbitrary arguments and return.
|
|
13
15
|
*/
|
|
14
|
-
export declare type Function = (...args: any
|
|
16
|
+
export declare type Function<T = any> = (...args: any) => T;
|
|
15
17
|
/**
|
|
16
18
|
* An iteratee function used for collection methods.
|
|
17
|
-
* @see {@link filter}
|
|
18
|
-
* @see {@link find}
|
|
19
|
-
* @see {@link forEach}
|
|
20
|
-
* @see {@link index}
|
|
21
|
-
* @see {@link map}
|
|
22
|
-
* @see {@link pick}
|
|
23
|
-
* @see {@link omit}
|
|
24
|
-
* @see {@link uniq}
|
|
25
|
-
* @see {@link sortedUniq}
|
|
26
|
-
* @see {@link groupBy}
|
|
19
|
+
* @see {@link Collections.filter}
|
|
20
|
+
* @see {@link Collections.find}
|
|
21
|
+
* @see {@link Collections.forEach}
|
|
22
|
+
* @see {@link Collections.index}
|
|
23
|
+
* @see {@link Collections.map}
|
|
24
|
+
* @see {@link Collections.pick}
|
|
25
|
+
* @see {@link Collections.omit}
|
|
26
|
+
* @see {@link Collections.uniq}
|
|
27
|
+
* @see {@link Collections.sortedUniq}
|
|
28
|
+
* @see {@link Collections.groupBy}
|
|
27
29
|
*/
|
|
28
30
|
export declare type Iteratee<T = any> = (value: T, key?: any, arr?: any) => any;
|
|
29
31
|
export declare type Predicate<T = any> = (value: T) => any;
|
|
@@ -111,7 +113,7 @@ export declare const isArray: (x: any) => x is any[];
|
|
|
111
113
|
*/
|
|
112
114
|
export declare const isArrayLike: (x: any) => boolean;
|
|
113
115
|
/** Check if value is a function type. */
|
|
114
|
-
export declare const isFunc: (x: any) => x is Function
|
|
116
|
+
export declare const isFunc: (x: any) => x is Function<any>;
|
|
115
117
|
/** Check if value is a number type. */
|
|
116
118
|
export declare const isNumber: (x: any) => x is number;
|
|
117
119
|
/** Check if value is a bigint type. */
|
|
@@ -142,10 +144,29 @@ export declare const isSymbol: (x: any) => x is symbol;
|
|
|
142
144
|
*/
|
|
143
145
|
export declare function len(value: any): any;
|
|
144
146
|
/**
|
|
145
|
-
* Generates a unique ID using random numbers.
|
|
146
|
-
*
|
|
147
|
+
* Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
|
|
148
|
+
* @param {string} [pre=''] The id prefix.
|
|
149
|
+
* @returns {number|string} Returns the numeric or string ID.
|
|
150
|
+
* @example
|
|
151
|
+
```js
|
|
152
|
+
uniqueId()
|
|
153
|
+
// => 3445556877
|
|
154
|
+
|
|
155
|
+
uniqueId('user_')
|
|
156
|
+
// => 'user_1033763188'
|
|
157
|
+
```
|
|
147
158
|
*/
|
|
148
159
|
export declare const uniqueId: (pre?: string) => any;
|
|
160
|
+
/**
|
|
161
|
+
* Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15).
|
|
162
|
+
* @returns Returns a GUID formatted string.
|
|
163
|
+
* @example
|
|
164
|
+
```js
|
|
165
|
+
uuid()
|
|
166
|
+
// => '183f8ef3-b8f0-4000-81f5-89234c836d00'
|
|
167
|
+
```
|
|
168
|
+
*/
|
|
169
|
+
export declare const uuid: () => string;
|
|
149
170
|
/**
|
|
150
171
|
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
151
172
|
* @param {*} obj
|
|
@@ -172,32 +193,6 @@ export declare const keys: (object: Object) => string[];
|
|
|
172
193
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
173
194
|
*/
|
|
174
195
|
export declare const values: (object: any) => unknown[];
|
|
175
|
-
/**
|
|
176
|
-
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
177
|
-
* omitted, it returns the nearest integer to x.
|
|
178
|
-
*
|
|
179
|
-
* @param {number} x
|
|
180
|
-
* @param {number} [digits=0]
|
|
181
|
-
*
|
|
182
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
183
|
-
*/
|
|
184
|
-
export declare function round(x: number, digits?: number): number;
|
|
185
|
-
/**
|
|
186
|
-
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
187
|
-
* @param x A number.
|
|
188
|
-
* @returns Nearest integer less than or equal to number.
|
|
189
|
-
*
|
|
190
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
191
|
-
*/
|
|
192
|
-
export declare const floor: (x: number) => number;
|
|
193
|
-
/**
|
|
194
|
-
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
195
|
-
* @param x A number.
|
|
196
|
-
* @returns Nearest integer greater than or equal to number.
|
|
197
|
-
*
|
|
198
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
199
|
-
*/
|
|
200
|
-
export declare const ceil: (x: number) => number;
|
|
201
196
|
/**
|
|
202
197
|
* Check if a given property is present in a given object.
|
|
203
198
|
* @param obj The object to check.
|
package/src/globals/index.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
|
+
/** This is just a noop function. */
|
|
7
|
+
export const _ = () => { };
|
|
6
8
|
/** A function that always returns `true`. */
|
|
7
9
|
export const True = () => true;
|
|
8
10
|
/** A function that always returns `false`. */
|
|
@@ -84,6 +86,8 @@ export function type(value) {
|
|
|
84
86
|
return 'function';
|
|
85
87
|
if (value instanceof Array)
|
|
86
88
|
return 'array';
|
|
89
|
+
if (value instanceof Error)
|
|
90
|
+
return 'error';
|
|
87
91
|
if (value[Symbol.toStringTag])
|
|
88
92
|
return value[Symbol.toStringTag];
|
|
89
93
|
const className = str(value);
|
|
@@ -186,12 +190,34 @@ export function len(value) {
|
|
|
186
190
|
}
|
|
187
191
|
}
|
|
188
192
|
/**
|
|
189
|
-
* Generates a unique ID using random numbers.
|
|
190
|
-
*
|
|
193
|
+
* Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
|
|
194
|
+
* @param {string} [pre=''] The id prefix.
|
|
195
|
+
* @returns {number|string} Returns the numeric or string ID.
|
|
196
|
+
* @example
|
|
197
|
+
```js
|
|
198
|
+
uniqueId()
|
|
199
|
+
// => 3445556877
|
|
200
|
+
|
|
201
|
+
uniqueId('user_')
|
|
202
|
+
// => 'user_1033763188'
|
|
203
|
+
```
|
|
191
204
|
*/
|
|
192
205
|
export const uniqueId = (pre = '') =>
|
|
193
206
|
// @ts-expect-error
|
|
194
207
|
(pre || 0) + ((Math.random() * 1e10) >>> 0);
|
|
208
|
+
/**
|
|
209
|
+
* Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15).
|
|
210
|
+
* @returns Returns a GUID formatted string.
|
|
211
|
+
* @example
|
|
212
|
+
```js
|
|
213
|
+
uuid()
|
|
214
|
+
// => '183f8ef3-b8f0-4000-81f5-89234c836d00'
|
|
215
|
+
```
|
|
216
|
+
*/
|
|
217
|
+
export const uuid = () => {
|
|
218
|
+
const u = Date.now().toString(16) + Math.random().toString(16) + '0'.repeat(16);
|
|
219
|
+
return [u.substr(0, 8), u.substr(8, 4), '4000-8' + u.substr(13, 3), u.substr(16, 12)].join('-');
|
|
220
|
+
};
|
|
195
221
|
/**
|
|
196
222
|
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
197
223
|
* @param {*} obj
|
|
@@ -222,35 +248,6 @@ export const keys = (object) => Object.keys(object);
|
|
|
222
248
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
223
249
|
*/
|
|
224
250
|
export const values = (object) => Object.values(object);
|
|
225
|
-
/**
|
|
226
|
-
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
227
|
-
* omitted, it returns the nearest integer to x.
|
|
228
|
-
*
|
|
229
|
-
* @param {number} x
|
|
230
|
-
* @param {number} [digits=0]
|
|
231
|
-
*
|
|
232
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
233
|
-
*/
|
|
234
|
-
export function round(x, digits = 0) {
|
|
235
|
-
const base = 10 ** digits;
|
|
236
|
-
return Math.round(x * base) / base;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
240
|
-
* @param x A number.
|
|
241
|
-
* @returns Nearest integer less than or equal to number.
|
|
242
|
-
*
|
|
243
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
244
|
-
*/
|
|
245
|
-
export const floor = (x) => Math.floor(x);
|
|
246
|
-
/**
|
|
247
|
-
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
248
|
-
* @param x A number.
|
|
249
|
-
* @returns Nearest integer greater than or equal to number.
|
|
250
|
-
*
|
|
251
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
252
|
-
*/
|
|
253
|
-
export const ceil = (x) => Math.ceil(x);
|
|
254
251
|
/**
|
|
255
252
|
* Check if a given property is present in a given object.
|
|
256
253
|
* @param obj The object to check.
|
package/src/globals/index.ts
CHANGED
|
@@ -6,27 +6,30 @@
|
|
|
6
6
|
|
|
7
7
|
/** Describes a function used to compare two values.
|
|
8
8
|
*
|
|
9
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description}
|
|
9
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
|
|
10
10
|
*/
|
|
11
11
|
export type Comp = (x, y) => -1 | 0 | 1
|
|
12
12
|
|
|
13
|
+
/** This is just a noop function. */
|
|
14
|
+
export const _ = () => {}
|
|
15
|
+
|
|
13
16
|
/**
|
|
14
17
|
* A generic function type with arbitrary arguments and return.
|
|
15
18
|
*/
|
|
16
|
-
export type Function = (...args) =>
|
|
19
|
+
export type Function<T = any> = (...args: any) => T
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* An iteratee function used for collection methods.
|
|
20
|
-
* @see {@link filter}
|
|
21
|
-
* @see {@link find}
|
|
22
|
-
* @see {@link forEach}
|
|
23
|
-
* @see {@link index}
|
|
24
|
-
* @see {@link map}
|
|
25
|
-
* @see {@link pick}
|
|
26
|
-
* @see {@link omit}
|
|
27
|
-
* @see {@link uniq}
|
|
28
|
-
* @see {@link sortedUniq}
|
|
29
|
-
* @see {@link groupBy}
|
|
23
|
+
* @see {@link Collections.filter}
|
|
24
|
+
* @see {@link Collections.find}
|
|
25
|
+
* @see {@link Collections.forEach}
|
|
26
|
+
* @see {@link Collections.index}
|
|
27
|
+
* @see {@link Collections.map}
|
|
28
|
+
* @see {@link Collections.pick}
|
|
29
|
+
* @see {@link Collections.omit}
|
|
30
|
+
* @see {@link Collections.uniq}
|
|
31
|
+
* @see {@link Collections.sortedUniq}
|
|
32
|
+
* @see {@link Collections.groupBy}
|
|
30
33
|
*/
|
|
31
34
|
export type Iteratee<T = any> = (value: T, key?, arr?) => any
|
|
32
35
|
|
|
@@ -111,6 +114,7 @@ export function type(value) {
|
|
|
111
114
|
if (value instanceof Boolean) return 'boolean'
|
|
112
115
|
if (value instanceof Function) return 'function'
|
|
113
116
|
if (value instanceof Array) return 'array'
|
|
117
|
+
if (value instanceof Error) return 'error'
|
|
114
118
|
if (value[Symbol.toStringTag]) return value[Symbol.toStringTag]
|
|
115
119
|
|
|
116
120
|
const className = str(value)
|
|
@@ -228,13 +232,36 @@ export function len(value) {
|
|
|
228
232
|
}
|
|
229
233
|
|
|
230
234
|
/**
|
|
231
|
-
* Generates a unique ID using random numbers.
|
|
232
|
-
*
|
|
235
|
+
* Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
|
|
236
|
+
* @param {string} [pre=''] The id prefix.
|
|
237
|
+
* @returns {number|string} Returns the numeric or string ID.
|
|
238
|
+
* @example
|
|
239
|
+
```js
|
|
240
|
+
uniqueId()
|
|
241
|
+
// => 3445556877
|
|
242
|
+
|
|
243
|
+
uniqueId('user_')
|
|
244
|
+
// => 'user_1033763188'
|
|
245
|
+
```
|
|
233
246
|
*/
|
|
234
247
|
export const uniqueId = (pre: string = '') =>
|
|
235
248
|
// @ts-expect-error
|
|
236
249
|
(pre || 0) + ((Math.random() * 1e10) >>> 0)
|
|
237
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15).
|
|
253
|
+
* @returns Returns a GUID formatted string.
|
|
254
|
+
* @example
|
|
255
|
+
```js
|
|
256
|
+
uuid()
|
|
257
|
+
// => '183f8ef3-b8f0-4000-81f5-89234c836d00'
|
|
258
|
+
```
|
|
259
|
+
*/
|
|
260
|
+
export const uuid = () => {
|
|
261
|
+
const u = Date.now().toString(16) + Math.random().toString(16) + '0'.repeat(16)
|
|
262
|
+
return [u.substr(0, 8), u.substr(8, 4), '4000-8' + u.substr(13, 3), u.substr(16, 12)].join('-')
|
|
263
|
+
}
|
|
264
|
+
|
|
238
265
|
/**
|
|
239
266
|
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
240
267
|
* @param {*} obj
|
|
@@ -270,38 +297,6 @@ export const keys = (object: Object) => Object.keys(object)
|
|
|
270
297
|
*/
|
|
271
298
|
export const values = (object) => Object.values(object)
|
|
272
299
|
|
|
273
|
-
/**
|
|
274
|
-
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
275
|
-
* omitted, it returns the nearest integer to x.
|
|
276
|
-
*
|
|
277
|
-
* @param {number} x
|
|
278
|
-
* @param {number} [digits=0]
|
|
279
|
-
*
|
|
280
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
281
|
-
*/
|
|
282
|
-
export function round(x: number, digits = 0) {
|
|
283
|
-
const base = 10 ** digits
|
|
284
|
-
return Math.round(x * base) / base
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
289
|
-
* @param x A number.
|
|
290
|
-
* @returns Nearest integer less than or equal to number.
|
|
291
|
-
*
|
|
292
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
293
|
-
*/
|
|
294
|
-
export const floor = (x: number) => Math.floor(x)
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
298
|
-
* @param x A number.
|
|
299
|
-
* @returns Nearest integer greater than or equal to number.
|
|
300
|
-
*
|
|
301
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
302
|
-
*/
|
|
303
|
-
export const ceil = (x: number) => Math.ceil(x)
|
|
304
|
-
|
|
305
300
|
/**
|
|
306
301
|
* Check if a given property is present in a given object.
|
|
307
302
|
* @param obj The object to check.
|
package/src/itertools/index.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ contains('abcd', 'bc')
|
|
|
72
72
|
// => true
|
|
73
73
|
```
|
|
74
74
|
*
|
|
75
|
-
* @see {@link
|
|
75
|
+
* @see {@link Collections.find}
|
|
76
76
|
*/
|
|
77
77
|
export declare function contains(collection: Iterable<any>, value: any): any;
|
|
78
78
|
/**
|
|
@@ -141,20 +141,6 @@ export declare function itake<T>(n: number, iterable: Iterable<T>): Generator<un
|
|
|
141
141
|
*/
|
|
142
142
|
export declare function iter<T>(obj: any): Iterator<T>;
|
|
143
143
|
export declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[][];
|
|
144
|
-
/**
|
|
145
|
-
* Returns the smallest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `le` method, this will be called to compare.
|
|
146
|
-
* @param iterable The iterable to inspect.
|
|
147
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
148
|
-
* @returns The smallest element in the iterable.
|
|
149
|
-
*/
|
|
150
|
-
export declare function min<T>(iterable: Iterable<T>, key?: (x: any) => any): any;
|
|
151
|
-
/**
|
|
152
|
-
* Returns the largest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `gt` method, this will be called to compare.
|
|
153
|
-
* @param iterable The iterable to inspect.
|
|
154
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
155
|
-
* @returns The largest element in the iterable.
|
|
156
|
-
*/
|
|
157
|
-
export declare function max<T>(iterable: Iterable<T>, key?: (x: any) => any): any;
|
|
158
144
|
/**
|
|
159
145
|
* Returns a generator for a sequence of numbers in the given range, starting from `start` (default 0) to `stop` (not-inclusive) in increments of `step` (default 1).
|
|
160
146
|
*
|
|
@@ -208,21 +194,6 @@ export declare function sort(args: any[]): any[];
|
|
|
208
194
|
export declare function sort(args: any[], reverse: boolean): any[];
|
|
209
195
|
export declare function sort(args: any[], compareFn: Comp): any[];
|
|
210
196
|
export declare function sort(args: any[], reverse: boolean, compareFn: Comp): any[];
|
|
211
|
-
/**
|
|
212
|
-
* Sums `initial` and the items of an iterable and returns the total.
|
|
213
|
-
* @param args The elements to add to `initial`.
|
|
214
|
-
* @param initial The initial value to add to (default `0`).
|
|
215
|
-
* @returns The total sum of initial and iterable elements.
|
|
216
|
-
* @example
|
|
217
|
-
```js
|
|
218
|
-
sum([1, 2, 3, 4])
|
|
219
|
-
// => 10
|
|
220
|
-
|
|
221
|
-
sum([1, 2, 3, 4], 5)
|
|
222
|
-
// => 15
|
|
223
|
-
```
|
|
224
|
-
*/
|
|
225
|
-
export declare function sum(args: Iterable<number>, initial?: number): number;
|
|
226
197
|
export declare function take<T>(n: number, iterable: Iterable<T>): unknown[];
|
|
227
198
|
/**
|
|
228
199
|
* Returns a generator that takes elements from the iterable as long as the predicate is `true`.
|