atomic-fns 0.2.2 → 0.3.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/.firebaserc +5 -0
- package/README.md +9 -4
- package/firebase.json +10 -0
- package/package.json +9 -9
- package/src/collections/index.d.ts +672 -0
- package/src/collections/index.js +1091 -0
- package/src/collections/index.ts +1186 -0
- package/src/datetime/index.d.ts +6 -0
- package/src/datetime/index.js +6 -0
- package/src/datetime/index.ts +7 -0
- package/src/functools/index.d.ts +107 -0
- package/src/functools/index.js +219 -0
- package/src/functools/index.ts +255 -0
- package/src/globals/index.d.ts +286 -0
- package/src/globals/index.js +388 -0
- package/src/globals/index.ts +451 -0
- package/src/index.d.ts +8 -7
- package/src/index.js +8 -7
- package/src/index.ts +8 -7
- package/src/itertools/index.d.ts +225 -0
- package/src/itertools/index.js +449 -0
- package/src/itertools/index.ts +471 -0
- package/src/math/index.d.ts +97 -0
- package/src/math/index.js +92 -0
- package/src/math/index.ts +152 -0
- package/src/{operators.d.ts → operators/index.d.ts} +11 -9
- package/src/operators/index.js +171 -0
- package/src/operators/index.ts +175 -0
- package/src/random/index.d.ts +42 -0
- package/src/random/index.js +65 -0
- package/src/random/index.ts +66 -0
- package/tests/collections.spec.ts +42 -25
- package/tests/globals.spec.ts +56 -60
- package/tests/itertools.spec.ts +176 -4
- package/tests/math.spec.ts +25 -0
- package/tests/operators.spec.ts +1 -26
- package/tests/random.spec.ts +46 -0
- package/tsconfig.json +12 -4
- package/src/collections.d.ts +0 -228
- package/src/collections.js +0 -553
- package/src/collections.ts +0 -641
- package/src/datetime.d.ts +0 -5
- package/src/datetime.js +0 -5
- package/src/datetime.ts +0 -6
- package/src/functools.d.ts +0 -40
- package/src/functools.js +0 -78
- package/src/functools.ts +0 -93
- package/src/globals.d.ts +0 -179
- package/src/globals.js +0 -309
- package/src/globals.ts +0 -377
- package/src/itertools.d.ts +0 -20
- package/src/itertools.js +0 -98
- package/src/itertools.ts +0 -116
- package/src/math.d.ts +0 -16
- package/src/math.js +0 -70
- package/src/math.ts +0 -77
- package/src/objects.d.ts +0 -0
- package/src/objects.js +0 -1
- package/src/objects.ts +0 -0
- package/src/operators.js +0 -109
- package/src/operators.ts +0 -130
- package/src/random.d.ts +0 -22
- package/src/random.js +0 -48
- package/src/random.ts +0 -53
- package/src/strings.d.ts +0 -0
- package/src/strings.js +0 -1
- package/src/strings.ts +0 -0
- package/src/util.d.ts +0 -0
- package/src/util.js +0 -1
- package/src/util.ts +0 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module includes global functions and constants.
|
|
3
|
+
*
|
|
4
|
+
* @module Globals
|
|
5
|
+
*/
|
|
6
|
+
/** Describes a function used to compare two values.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
|
|
9
|
+
*/
|
|
10
|
+
export declare type Comp = (x: any, y: any) => -1 | 0 | 1;
|
|
11
|
+
/** This is just a noop function. */
|
|
12
|
+
export declare const _: () => void;
|
|
13
|
+
/**
|
|
14
|
+
* A generic function type with arbitrary arguments and return.
|
|
15
|
+
*/
|
|
16
|
+
export declare type Function<T = any> = (...args: any) => T;
|
|
17
|
+
/**
|
|
18
|
+
* An iteratee function used for collection methods.
|
|
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}
|
|
29
|
+
*/
|
|
30
|
+
export declare type Iteratee<T = any> = (value: T, key?: any, arr?: any) => any;
|
|
31
|
+
export declare type Predicate<T = any> = (value: T) => any;
|
|
32
|
+
/** A function that always returns `true`. */
|
|
33
|
+
export declare const True: () => boolean;
|
|
34
|
+
/** A function that always returns `false`. */
|
|
35
|
+
export declare const False: () => boolean;
|
|
36
|
+
/**
|
|
37
|
+
* A custom error class that inherits from the Error object.
|
|
38
|
+
* @class CustomError
|
|
39
|
+
* @extends {Error}
|
|
40
|
+
*/
|
|
41
|
+
export declare class CustomError extends Error {
|
|
42
|
+
/**
|
|
43
|
+
* Returns a new CustomError with the specified message.
|
|
44
|
+
* @param {string} message
|
|
45
|
+
* @constructor
|
|
46
|
+
*/
|
|
47
|
+
constructor(message?: string);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Custom error to signal an invalid index access.
|
|
51
|
+
* @extends {CustomError}
|
|
52
|
+
*/
|
|
53
|
+
export declare class IndexError extends CustomError {
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Raised when a system function returns an I/O failure such as "file not found"
|
|
57
|
+
* or "disk full".
|
|
58
|
+
* @extends {CustomError}
|
|
59
|
+
*/
|
|
60
|
+
export declare class IOError extends CustomError {
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Custom error to signal an invalid key access.
|
|
64
|
+
* @extends {CustomError}
|
|
65
|
+
*/
|
|
66
|
+
export declare class KeyError extends CustomError {
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Custom error to signal an invalid operation.
|
|
70
|
+
* @extends {CustomError}
|
|
71
|
+
*/
|
|
72
|
+
export declare class NotImplementedError extends CustomError {
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Custom error to signal an invalid value.
|
|
76
|
+
* @extends {CustomError}
|
|
77
|
+
*/
|
|
78
|
+
export declare class ValueError extends CustomError {
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Raised when the second argument of a division or modulo operation is zero.
|
|
82
|
+
* @extends {CustomError}
|
|
83
|
+
*/
|
|
84
|
+
export declare class ZeroDivisionError extends CustomError {
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns the true type of any value with correct detection for null, Array,
|
|
88
|
+
* Object, Promise, Symbol, and NaN. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype.
|
|
89
|
+
* @param value
|
|
90
|
+
* @returns The value type.
|
|
91
|
+
*/
|
|
92
|
+
export declare function type(value: any): any;
|
|
93
|
+
/**
|
|
94
|
+
* Returns a string representing the object.
|
|
95
|
+
* @param obj
|
|
96
|
+
* @returns `obj.toString()` or `''` if `obj` is `null` or `undefined`.
|
|
97
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
|
|
98
|
+
*/
|
|
99
|
+
export declare const str: (obj: any) => any;
|
|
100
|
+
/** Check if value is a boolean type. */
|
|
101
|
+
export declare const isBool: (x: any) => x is boolean;
|
|
102
|
+
/** Check if value is an iterable type. */
|
|
103
|
+
export declare const isIterable: (x: any) => x is IterableIterator<any>;
|
|
104
|
+
/** Check if value is an object type. */
|
|
105
|
+
export declare const isObject: (x: any) => x is Object;
|
|
106
|
+
/** Check if value is a string type. */
|
|
107
|
+
export declare const isString: (x: any) => x is string;
|
|
108
|
+
/** Check if value is an Array type. */
|
|
109
|
+
export declare const isArray: (x: any) => x is any[];
|
|
110
|
+
/**
|
|
111
|
+
* Check if value is Array-like type.
|
|
112
|
+
* A value is considered array-like if it's not a function and has a `.length` number property.
|
|
113
|
+
*/
|
|
114
|
+
export declare const isArrayLike: (x: any) => boolean;
|
|
115
|
+
/** Check if value is a function type. */
|
|
116
|
+
export declare const isFunc: (x: any) => x is Function<any>;
|
|
117
|
+
/** Check if value is a number type. */
|
|
118
|
+
export declare const isNumber: (x: any) => x is number;
|
|
119
|
+
/** Check if value is a bigint type. */
|
|
120
|
+
export declare const isBigint: (x: any) => x is BigInt;
|
|
121
|
+
/** Check if value is NaN based on `Number.isNaN`. */
|
|
122
|
+
export declare const isNaN: (x: any) => x is number;
|
|
123
|
+
/** Check if value is a Promise type. */
|
|
124
|
+
export declare const isPromise: (x: any) => x is Promise<any>;
|
|
125
|
+
/** Check if value is an async function type. */
|
|
126
|
+
export declare const isAsync: (x: any) => boolean;
|
|
127
|
+
/** Check if value is a generator function type. */
|
|
128
|
+
export declare const isGenerator: (x: any) => x is Generator<unknown, any, unknown>;
|
|
129
|
+
/** Check if value is `null` or `undefined`. */
|
|
130
|
+
export declare const isNull: (x: any) => x is null | undefined;
|
|
131
|
+
/** Check if value is not `null` or `undefined`. */
|
|
132
|
+
export declare const notNull: (x: any) => boolean;
|
|
133
|
+
/** Returns `true` for objects without length or falsey values. */
|
|
134
|
+
export declare const isEmpty: (x: any) => boolean;
|
|
135
|
+
/** Check if value is a `Symbol` type */
|
|
136
|
+
export declare const isSymbol: (x: any) => x is symbol;
|
|
137
|
+
/**
|
|
138
|
+
* Returns the number of elements in a collection type.
|
|
139
|
+
*
|
|
140
|
+
* If `value.length` or `value.size()` is defined, this will be returned.
|
|
141
|
+
* If `value` is an `Object`, it returns the number of keys.
|
|
142
|
+
* @param value
|
|
143
|
+
* @returns The number of elements in the collection or `undefined`.
|
|
144
|
+
*/
|
|
145
|
+
export declare function len(value: any): any;
|
|
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
|
+
```
|
|
158
|
+
*/
|
|
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;
|
|
170
|
+
/**
|
|
171
|
+
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
172
|
+
* @param {*} obj
|
|
173
|
+
* @param {PropertyKey} key
|
|
174
|
+
* @param {...any[]} args
|
|
175
|
+
* @return `obj.key(...args)` or `undefined`.
|
|
176
|
+
*/
|
|
177
|
+
export declare function call(obj: any, key: PropertyKey, ...args: any[]): any;
|
|
178
|
+
/** Convert number to unicode character */
|
|
179
|
+
export declare const chr: (x: number) => string;
|
|
180
|
+
/** Convert character to Unicode code point */
|
|
181
|
+
export declare const ord: (x: string) => number;
|
|
182
|
+
/**
|
|
183
|
+
* Returns an array of the own enumerable property names of `object`.
|
|
184
|
+
* @param object The given object.
|
|
185
|
+
* @returns The array of object keys.
|
|
186
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
|
|
187
|
+
*/
|
|
188
|
+
export declare const keys: (object: Object) => string[];
|
|
189
|
+
/**
|
|
190
|
+
* Returns an array of the own enumerable property values of `object`.
|
|
191
|
+
* @param object The given object.
|
|
192
|
+
* @returns The array of object keys.
|
|
193
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
194
|
+
*/
|
|
195
|
+
export declare const values: (object: any) => unknown[];
|
|
196
|
+
/**
|
|
197
|
+
* Check if a given property is present in a given object.
|
|
198
|
+
* @param obj The object to check.
|
|
199
|
+
* @param attr A property key name.
|
|
200
|
+
* @returns `true` if the object has the property name.
|
|
201
|
+
*/
|
|
202
|
+
export declare const has: (obj: any, attr: PropertyKey) => any;
|
|
203
|
+
/**
|
|
204
|
+
* Check if the attribute is present in the object or return a default value instead.
|
|
205
|
+
* @param {PropertyKey} key
|
|
206
|
+
* @param {*} obj
|
|
207
|
+
* @param {*} [value=undefined]
|
|
208
|
+
* @return The property value or default value.
|
|
209
|
+
*/
|
|
210
|
+
export declare function get(key: PropertyKey | PropertyKey[], obj: any, value?: any): any;
|
|
211
|
+
/**
|
|
212
|
+
* Deletes a given property key from an object.
|
|
213
|
+
* @param attr The property name.
|
|
214
|
+
* @param x The given object.
|
|
215
|
+
* @returns The modified object.
|
|
216
|
+
*/
|
|
217
|
+
export declare function del(attr: PropertyKey, x: any): any;
|
|
218
|
+
/**
|
|
219
|
+
* Adds a property to an object, or modifies the existing value.
|
|
220
|
+
* @param attr The name or Symbol of the property to be defined or modified.
|
|
221
|
+
* @param obj The container object.
|
|
222
|
+
* @param value The value associated with the property
|
|
223
|
+
* @param {boolean} [writable=false] If `true` the value may be changed later. Defaults to `false`.
|
|
224
|
+
* @param {boolean} [enumerable=false] If `true` this property shows up during enumeration of the properties in the object. Defaults to `false`.
|
|
225
|
+
* @returns
|
|
226
|
+
*/
|
|
227
|
+
export declare function set(attr: PropertyKey, obj: any, value: any, writable?: boolean, enumerable?: boolean): any;
|
|
228
|
+
export declare const HASH_KEY: unique symbol;
|
|
229
|
+
/**
|
|
230
|
+
* Return a hash value for a given object. The hash will be the same for subsequent invocations.
|
|
231
|
+
* @param obj The given object.
|
|
232
|
+
* @returns The hash value.
|
|
233
|
+
*/
|
|
234
|
+
export declare function hash(obj: any): any;
|
|
235
|
+
/**
|
|
236
|
+
* A hash value generator for strings.
|
|
237
|
+
* @param {string} str The given string.
|
|
238
|
+
* @returns {number} The hash value of the string.
|
|
239
|
+
* @see {@link http://isthe.com/chongo/tech/comp/fnv/ FNV hash}
|
|
240
|
+
*/
|
|
241
|
+
export declare function hashCode(str: string): number;
|
|
242
|
+
/**
|
|
243
|
+
* Converts a number to a binary string.
|
|
244
|
+
* @param n The given number.
|
|
245
|
+
* @returns The binary string representation of `n`.
|
|
246
|
+
*/
|
|
247
|
+
export declare const bin: (n: number) => string;
|
|
248
|
+
/**
|
|
249
|
+
* Converts a number to a hexadecimal string.
|
|
250
|
+
* @param n The given number.
|
|
251
|
+
* @returns The hexadecimal string representation of `n`.
|
|
252
|
+
*/
|
|
253
|
+
export declare const hex: (n: number) => string;
|
|
254
|
+
/**
|
|
255
|
+
* Converts a number to an octal string.
|
|
256
|
+
* @param n The given number.
|
|
257
|
+
* @returns The octal string representation of `n`.
|
|
258
|
+
*/
|
|
259
|
+
export declare const oct: (n: number) => string;
|
|
260
|
+
/**
|
|
261
|
+
* Returns a floating point number constructed from a number or string `x`.
|
|
262
|
+
* @param x The number or string.
|
|
263
|
+
* @returns A floating point number parsed from the given value or `NaN`.
|
|
264
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat}
|
|
265
|
+
*/
|
|
266
|
+
export declare const float: (x: any) => number;
|
|
267
|
+
/**
|
|
268
|
+
* Returns an integer number constructed from a number or string `x`.
|
|
269
|
+
* @param x The number or string.
|
|
270
|
+
* @returns An integer number parsed from the given value or `NaN`.
|
|
271
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
|
|
272
|
+
*/
|
|
273
|
+
export declare const int: (x: any, base?: number) => number;
|
|
274
|
+
/**
|
|
275
|
+
* Converts the value to an array using `Array.from()`.
|
|
276
|
+
* @param value
|
|
277
|
+
* @returns A new array value.
|
|
278
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
|
|
279
|
+
*/
|
|
280
|
+
export declare const list: (value?: any) => unknown[];
|
|
281
|
+
/**
|
|
282
|
+
* Retrieve the next item from the iterator by calling it's `next()` method.
|
|
283
|
+
* @param iter The iterator.
|
|
284
|
+
* @returns The next value from the iterator.
|
|
285
|
+
*/
|
|
286
|
+
export declare const next: (iter: Iterator<any> | Generator) => any;
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module includes global functions and constants.
|
|
3
|
+
*
|
|
4
|
+
* @module Globals
|
|
5
|
+
*/
|
|
6
|
+
/** This is just a noop function. */
|
|
7
|
+
export const _ = () => { };
|
|
8
|
+
/** A function that always returns `true`. */
|
|
9
|
+
export const True = () => true;
|
|
10
|
+
/** A function that always returns `false`. */
|
|
11
|
+
export const False = () => false;
|
|
12
|
+
/**
|
|
13
|
+
* A custom error class that inherits from the Error object.
|
|
14
|
+
* @class CustomError
|
|
15
|
+
* @extends {Error}
|
|
16
|
+
*/
|
|
17
|
+
export class CustomError extends Error {
|
|
18
|
+
/**
|
|
19
|
+
* Returns a new CustomError with the specified message.
|
|
20
|
+
* @param {string} message
|
|
21
|
+
* @constructor
|
|
22
|
+
*/
|
|
23
|
+
constructor(message) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = this.constructor.name;
|
|
26
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
27
|
+
Error.captureStackTrace(this, this.constructor);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.stack = new Error(message).stack;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Custom error to signal an invalid index access.
|
|
36
|
+
* @extends {CustomError}
|
|
37
|
+
*/
|
|
38
|
+
export class IndexError extends CustomError {
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Raised when a system function returns an I/O failure such as "file not found"
|
|
42
|
+
* or "disk full".
|
|
43
|
+
* @extends {CustomError}
|
|
44
|
+
*/
|
|
45
|
+
export class IOError extends CustomError {
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Custom error to signal an invalid key access.
|
|
49
|
+
* @extends {CustomError}
|
|
50
|
+
*/
|
|
51
|
+
export class KeyError extends CustomError {
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Custom error to signal an invalid operation.
|
|
55
|
+
* @extends {CustomError}
|
|
56
|
+
*/
|
|
57
|
+
export class NotImplementedError extends CustomError {
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Custom error to signal an invalid value.
|
|
61
|
+
* @extends {CustomError}
|
|
62
|
+
*/
|
|
63
|
+
export class ValueError extends CustomError {
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Raised when the second argument of a division or modulo operation is zero.
|
|
67
|
+
* @extends {CustomError}
|
|
68
|
+
*/
|
|
69
|
+
export class ZeroDivisionError extends CustomError {
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the true type of any value with correct detection for null, Array,
|
|
73
|
+
* Object, Promise, Symbol, and NaN. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype.
|
|
74
|
+
* @param value
|
|
75
|
+
* @returns The value type.
|
|
76
|
+
*/
|
|
77
|
+
export function type(value) {
|
|
78
|
+
const result = typeof value;
|
|
79
|
+
if (result === 'object') {
|
|
80
|
+
if (value) {
|
|
81
|
+
if (value instanceof String)
|
|
82
|
+
return 'string';
|
|
83
|
+
if (value instanceof Boolean)
|
|
84
|
+
return 'boolean';
|
|
85
|
+
if (value instanceof Function)
|
|
86
|
+
return 'function';
|
|
87
|
+
if (value instanceof Array)
|
|
88
|
+
return 'array';
|
|
89
|
+
if (value instanceof Error)
|
|
90
|
+
return 'error';
|
|
91
|
+
if (value[Symbol.toStringTag])
|
|
92
|
+
return value[Symbol.toStringTag];
|
|
93
|
+
const className = str(value);
|
|
94
|
+
if (className === '[object Window]')
|
|
95
|
+
return 'object';
|
|
96
|
+
if (className.endsWith('Array]') || Array.isArray(value)) {
|
|
97
|
+
return 'array';
|
|
98
|
+
}
|
|
99
|
+
if (className.endsWith('Function]') || typeof value.call === 'function') {
|
|
100
|
+
return 'function';
|
|
101
|
+
}
|
|
102
|
+
// Other primitive types
|
|
103
|
+
if (className === '[object Promise]' || value instanceof Promise) {
|
|
104
|
+
return 'Promise';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
return 'null';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (result === 'function' && typeof value.call === 'undefined') {
|
|
112
|
+
return 'object';
|
|
113
|
+
}
|
|
114
|
+
else if (Number.isNaN(value))
|
|
115
|
+
return 'NaN';
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns a string representing the object.
|
|
120
|
+
* @param obj
|
|
121
|
+
* @returns `obj.toString()` or `''` if `obj` is `null` or `undefined`.
|
|
122
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
|
|
123
|
+
*/
|
|
124
|
+
export const str = (obj) => (obj != null ? obj.toString() : '');
|
|
125
|
+
/** Check if value is a boolean type. */
|
|
126
|
+
export const isBool = (x) => type(x) === 'boolean';
|
|
127
|
+
/** Check if value is an iterable type. */
|
|
128
|
+
export const isIterable = (x) => {
|
|
129
|
+
if (x == null)
|
|
130
|
+
return false;
|
|
131
|
+
return typeof x[Symbol.iterator] === 'function';
|
|
132
|
+
};
|
|
133
|
+
/** Check if value is an object type. */
|
|
134
|
+
export const isObject = (x) => typeof x === 'object' && type(x) === 'object';
|
|
135
|
+
/** Check if value is a string type. */
|
|
136
|
+
export const isString = (x) => type(x) === 'string';
|
|
137
|
+
/** Check if value is an Array type. */
|
|
138
|
+
export const isArray = (x) => Array.isArray(x);
|
|
139
|
+
/**
|
|
140
|
+
* Check if value is Array-like type.
|
|
141
|
+
* A value is considered array-like if it's not a function and has a `.length` number property.
|
|
142
|
+
*/
|
|
143
|
+
export const isArrayLike = (x) => {
|
|
144
|
+
const T = type(x);
|
|
145
|
+
return T === 'array' || (T === 'object' && isNumber(x.length));
|
|
146
|
+
};
|
|
147
|
+
/** Check if value is a function type. */
|
|
148
|
+
export const isFunc = (x) => x?.constructor === Function;
|
|
149
|
+
/** Check if value is a number type. */
|
|
150
|
+
export const isNumber = (x) => type(x) === 'number';
|
|
151
|
+
/** Check if value is a bigint type. */
|
|
152
|
+
export const isBigint = (x) => type(x) === 'bigint';
|
|
153
|
+
/** Check if value is NaN based on `Number.isNaN`. */
|
|
154
|
+
export const isNaN = (x) => Number.isNaN(x);
|
|
155
|
+
/** Check if value is a Promise type. */
|
|
156
|
+
export const isPromise = (x) => type(x) === 'Promise';
|
|
157
|
+
/** Check if value is an async function type. */
|
|
158
|
+
export const isAsync = (x) => x?.constructor.name === 'AsyncFunction';
|
|
159
|
+
/** Check if value is a generator function type. */
|
|
160
|
+
export const isGenerator = (x) => {
|
|
161
|
+
return x?.constructor.constructor?.name === 'GeneratorFunction';
|
|
162
|
+
};
|
|
163
|
+
/** Check if value is `null` or `undefined`. */
|
|
164
|
+
export const isNull = (x) => x == null;
|
|
165
|
+
/** Check if value is not `null` or `undefined`. */
|
|
166
|
+
export const notNull = (x) => x != null;
|
|
167
|
+
/** Returns `true` for objects without length or falsey values. */
|
|
168
|
+
export const isEmpty = (x) => (len(x) === 0 ? true : !x);
|
|
169
|
+
/** Check if value is a `Symbol` type */
|
|
170
|
+
export const isSymbol = (x) => type(x) === 'symbol';
|
|
171
|
+
/**
|
|
172
|
+
* Returns the number of elements in a collection type.
|
|
173
|
+
*
|
|
174
|
+
* If `value.length` or `value.size()` is defined, this will be returned.
|
|
175
|
+
* If `value` is an `Object`, it returns the number of keys.
|
|
176
|
+
* @param value
|
|
177
|
+
* @returns The number of elements in the collection or `undefined`.
|
|
178
|
+
*/
|
|
179
|
+
export function len(value) {
|
|
180
|
+
if (value == null)
|
|
181
|
+
return;
|
|
182
|
+
if (value.length !== undefined) {
|
|
183
|
+
return value.length;
|
|
184
|
+
}
|
|
185
|
+
if (isFunc(value.size)) {
|
|
186
|
+
return value.size();
|
|
187
|
+
}
|
|
188
|
+
if (isObject(value)) {
|
|
189
|
+
return Object.keys(value).length;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
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
|
+
```
|
|
204
|
+
*/
|
|
205
|
+
export const uniqueId = (pre = '') =>
|
|
206
|
+
// @ts-expect-error
|
|
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
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Checks if `obj.key` is a function, and calls it with any given `args`.
|
|
223
|
+
* @param {*} obj
|
|
224
|
+
* @param {PropertyKey} key
|
|
225
|
+
* @param {...any[]} args
|
|
226
|
+
* @return `obj.key(...args)` or `undefined`.
|
|
227
|
+
*/
|
|
228
|
+
export function call(obj, key, ...args) {
|
|
229
|
+
if (isFunc(get(key, obj))) {
|
|
230
|
+
return obj[key](...args);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Convert number to unicode character */
|
|
234
|
+
export const chr = (x) => String.fromCodePoint(x);
|
|
235
|
+
/** Convert character to Unicode code point */
|
|
236
|
+
export const ord = (x) => x.charCodeAt(0);
|
|
237
|
+
/**
|
|
238
|
+
* Returns an array of the own enumerable property names of `object`.
|
|
239
|
+
* @param object The given object.
|
|
240
|
+
* @returns The array of object keys.
|
|
241
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
|
|
242
|
+
*/
|
|
243
|
+
export const keys = (object) => Object.keys(object);
|
|
244
|
+
/**
|
|
245
|
+
* Returns an array of the own enumerable property values of `object`.
|
|
246
|
+
* @param object The given object.
|
|
247
|
+
* @returns The array of object keys.
|
|
248
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
|
|
249
|
+
*/
|
|
250
|
+
export const values = (object) => Object.values(object);
|
|
251
|
+
/**
|
|
252
|
+
* Check if a given property is present in a given object.
|
|
253
|
+
* @param obj The object to check.
|
|
254
|
+
* @param attr A property key name.
|
|
255
|
+
* @returns `true` if the object has the property name.
|
|
256
|
+
*/
|
|
257
|
+
export const has = (obj, attr) => obj && attr in obj;
|
|
258
|
+
/**
|
|
259
|
+
* Check if the attribute is present in the object or return a default value instead.
|
|
260
|
+
* @param {PropertyKey} key
|
|
261
|
+
* @param {*} obj
|
|
262
|
+
* @param {*} [value=undefined]
|
|
263
|
+
* @return The property value or default value.
|
|
264
|
+
*/
|
|
265
|
+
export function get(key, obj, value = undefined) {
|
|
266
|
+
if (obj == null)
|
|
267
|
+
return value;
|
|
268
|
+
let paths = Array.isArray(key) ? key : [key];
|
|
269
|
+
if (typeof key === 'string') {
|
|
270
|
+
paths = key.split('.');
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
let result = obj;
|
|
274
|
+
for (const k of paths) {
|
|
275
|
+
result = result[k];
|
|
276
|
+
}
|
|
277
|
+
return result === undefined ? value : result;
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Deletes a given property key from an object.
|
|
285
|
+
* @param attr The property name.
|
|
286
|
+
* @param x The given object.
|
|
287
|
+
* @returns The modified object.
|
|
288
|
+
*/
|
|
289
|
+
export function del(attr, x) {
|
|
290
|
+
if (x != null)
|
|
291
|
+
delete x[attr];
|
|
292
|
+
return x;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Adds a property to an object, or modifies the existing value.
|
|
296
|
+
* @param attr The name or Symbol of the property to be defined or modified.
|
|
297
|
+
* @param obj The container object.
|
|
298
|
+
* @param value The value associated with the property
|
|
299
|
+
* @param {boolean} [writable=false] If `true` the value may be changed later. Defaults to `false`.
|
|
300
|
+
* @param {boolean} [enumerable=false] If `true` this property shows up during enumeration of the properties in the object. Defaults to `false`.
|
|
301
|
+
* @returns
|
|
302
|
+
*/
|
|
303
|
+
export function set(attr, obj, value, writable = false, enumerable = false) {
|
|
304
|
+
return Object.defineProperty(obj, attr, {
|
|
305
|
+
value,
|
|
306
|
+
enumerable,
|
|
307
|
+
writable
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
export const HASH_KEY = Symbol.for('HASH_KEY');
|
|
311
|
+
/**
|
|
312
|
+
* Return a hash value for a given object. The hash will be the same for subsequent invocations.
|
|
313
|
+
* @param obj The given object.
|
|
314
|
+
* @returns The hash value.
|
|
315
|
+
*/
|
|
316
|
+
export function hash(obj) {
|
|
317
|
+
if (typeof obj === 'string') {
|
|
318
|
+
return hashCode(obj);
|
|
319
|
+
}
|
|
320
|
+
if (typeof obj === 'object') {
|
|
321
|
+
if (get(HASH_KEY, obj) == null) {
|
|
322
|
+
return obj[HASH_KEY];
|
|
323
|
+
}
|
|
324
|
+
obj[HASH_KEY] = uniqueId();
|
|
325
|
+
return obj[HASH_KEY];
|
|
326
|
+
}
|
|
327
|
+
return obj;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* A hash value generator for strings.
|
|
331
|
+
* @param {string} str The given string.
|
|
332
|
+
* @returns {number} The hash value of the string.
|
|
333
|
+
* @see {@link http://isthe.com/chongo/tech/comp/fnv/ FNV hash}
|
|
334
|
+
*/
|
|
335
|
+
export function hashCode(str) {
|
|
336
|
+
const FNV1_32A_INIT = 0x811c9dc5;
|
|
337
|
+
let hval = FNV1_32A_INIT;
|
|
338
|
+
for (let i = 0; i < str.length; ++i) {
|
|
339
|
+
hval ^= str.charCodeAt(i);
|
|
340
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
341
|
+
}
|
|
342
|
+
return hval >>> 0;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Converts a number to a binary string.
|
|
346
|
+
* @param n The given number.
|
|
347
|
+
* @returns The binary string representation of `n`.
|
|
348
|
+
*/
|
|
349
|
+
export const bin = (n) => n.toString(2);
|
|
350
|
+
/**
|
|
351
|
+
* Converts a number to a hexadecimal string.
|
|
352
|
+
* @param n The given number.
|
|
353
|
+
* @returns The hexadecimal string representation of `n`.
|
|
354
|
+
*/
|
|
355
|
+
export const hex = (n) => n.toString(16);
|
|
356
|
+
/**
|
|
357
|
+
* Converts a number to an octal string.
|
|
358
|
+
* @param n The given number.
|
|
359
|
+
* @returns The octal string representation of `n`.
|
|
360
|
+
*/
|
|
361
|
+
export const oct = (n) => n.toString(8);
|
|
362
|
+
/**
|
|
363
|
+
* Returns a floating point number constructed from a number or string `x`.
|
|
364
|
+
* @param x The number or string.
|
|
365
|
+
* @returns A floating point number parsed from the given value or `NaN`.
|
|
366
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat}
|
|
367
|
+
*/
|
|
368
|
+
export const float = (x) => parseFloat(x);
|
|
369
|
+
/**
|
|
370
|
+
* Returns an integer number constructed from a number or string `x`.
|
|
371
|
+
* @param x The number or string.
|
|
372
|
+
* @returns An integer number parsed from the given value or `NaN`.
|
|
373
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
|
|
374
|
+
*/
|
|
375
|
+
export const int = (x, base = 10) => parseInt(x, base);
|
|
376
|
+
/**
|
|
377
|
+
* Converts the value to an array using `Array.from()`.
|
|
378
|
+
* @param value
|
|
379
|
+
* @returns A new array value.
|
|
380
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
|
|
381
|
+
*/
|
|
382
|
+
export const list = (value) => (value ? Array.from(value) : []);
|
|
383
|
+
/**
|
|
384
|
+
* Retrieve the next item from the iterator by calling it's `next()` method.
|
|
385
|
+
* @param iter The iterator.
|
|
386
|
+
* @returns The next value from the iterator.
|
|
387
|
+
*/
|
|
388
|
+
export const next = (iter) => iter.next().value;
|