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