atomic-fns 0.2.0 → 0.2.3

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