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