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
@@ -1,637 +0,0 @@
1
- import {
2
- call,
3
- Function,
4
- get,
5
- isArray,
6
- isArrayLike,
7
- isBool,
8
- isEmpty,
9
- isFunc,
10
- isNull,
11
- isNumber,
12
- isObject,
13
- isString,
14
- Iteratee,
15
- keys,
16
- NotImplementedError,
17
- set
18
- } from './globals.js'
19
- import { comp, eq, id } from './operators.js'
20
-
21
- /**
22
- * A container type that provides contains().
23
- *
24
- *
25
- * @interface Container
26
- */
27
- export interface Container {
28
- contains: (x) => boolean
29
- }
30
-
31
- /**
32
- * These are the "rich comparison" methods, inspired by Python operators.
33
- *
34
- *
35
- * @interface Comparable
36
- */
37
- export interface Comparable {
38
- /** x < y: calls this.lt(other) reflection. */
39
- lt?: (other) => boolean
40
- /** x ≤ y: calls this.le(other) reflection. */
41
- le?: (other) => boolean
42
- /** x == y: calls this.eq(other) reflection. */
43
- eq: (other) => boolean
44
- /** x != y: calls this.ne(other) reflection. */
45
- ne: (other) => boolean
46
- /** x > y: calls this.gt(other) reflection. */
47
- gt?: (other) => boolean
48
- /** x ≥ y: calls this.ge(other) reflection. */
49
- ge?: (other) => boolean
50
- }
51
-
52
- /**
53
- * A Collection is an iterable container type.
54
- * This is an abstract base class for user-defined collection types.
55
- *
56
- *
57
- * @abstract
58
- * @class Collection
59
- * @implements {Container}
60
- */
61
- export abstract class Collection implements Container {
62
- /**
63
- * Returns an iterator for the container items
64
- * @return {Iterator}
65
- */
66
- [Symbol.iterator]() {
67
- return this
68
- }
69
-
70
- /**
71
- * Implements the iterator protocol and returns the next item in the iterable.
72
- */
73
- next() {}
74
-
75
- /**
76
- * Adds a new item to the container.
77
- * @param item
78
- */
79
- add(item) {}
80
-
81
- /**
82
- * Checks if item is present in the container.
83
- * @param item
84
- */
85
- contains(item) {
86
- return false
87
- }
88
-
89
- /**
90
- * Returns the total number of elements in the container.
91
- */
92
- size(): number {
93
- throw new NotImplementedError()
94
- }
95
-
96
- /**
97
- * Remove the first item from the container where `item == x`
98
- * @raises ValueError when x is not found
99
- */
100
- remove(x) {}
101
-
102
- /**
103
- * Retrieve and remove the item at index `i`.
104
- * @returns the item or undefined if not found.
105
- */
106
- pop(i) {}
107
-
108
- /**
109
- * Remove all items from the container
110
- */
111
- clear() {}
112
- }
113
-
114
- /**
115
- * Implements an iterable that allows backward iteration.
116
- * @interface Reversible
117
- */
118
- export interface Reversible<T = unknown> {
119
- reversed: () => Iterator<T>
120
- }
121
-
122
- /**
123
- * A sequence is an iterable type with efficient index-based access.
124
- *
125
- *
126
- * @abstract
127
- * @class Sequence
128
- * @extends {Collection}
129
- * @template T
130
- */
131
- export abstract class Sequence<T> extends Collection implements Reversible<T> {
132
- /**
133
- * Return the item at the given key or index
134
- * @param {*} key
135
- * @memberof Sequence
136
- */
137
- get(key): T | undefined {
138
- throw new NotImplementedError()
139
- }
140
-
141
- /**
142
- * Set a new value at the given key or index
143
- *
144
- * @param {*} key
145
- * @param {T} val
146
- * @memberof Sequence
147
- */
148
- set(key, val: T) {
149
- throw new NotImplementedError()
150
- }
151
-
152
- /**
153
- * Deletes the given key or index, and its value.
154
- * Raises ValueError if not key is found.
155
- *
156
- * @param {*} key
157
- * @memberof Sequence
158
- */
159
- delete(key) {
160
- throw new NotImplementedError()
161
- }
162
-
163
- /**
164
- * Adds a new item to the end of the sequence.
165
- *
166
- * @param {T} x
167
- * @memberof Sequence
168
- */
169
- append(x: T) {}
170
-
171
- /**
172
- * Append all the items to the sequence.
173
- *
174
- * @param {Iterable<T>} iter
175
- * @memberof Sequence
176
- */
177
- extend(iter: Iterable<T>) {}
178
-
179
- /**
180
- * Return the index of x in the sequence, or undefined if not found.
181
- *
182
- * @param {T} item
183
- * @return {(number | undefined)}
184
- * @memberof Sequence
185
- */
186
- indexOf(item: T): number | undefined {
187
- throw new NotImplementedError()
188
- }
189
-
190
- size() {
191
- return 0
192
- }
193
-
194
- reversed(): Iterator<T> {
195
- throw new NotImplementedError()
196
- }
197
- }
198
-
199
- /**
200
- * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
201
- *
202
- *
203
- * @class FrozenSet
204
- * @extends {Set<T>}
205
- * @template T
206
- */
207
- export class FrozenSet<T = any> extends Set<T> {
208
- constructor(iterable: Iterable<T>) {
209
- super(iterable)
210
- return this.freeze()
211
- }
212
-
213
- add() {
214
- throw TypeError('FrozenSet cannot be modified.')
215
- return this // eslint-disable-line
216
- }
217
-
218
- delete() {
219
- throw TypeError('FrozenSet cannot be modified.')
220
- return false // eslint-disable-line
221
- }
222
-
223
- clear() {
224
- throw TypeError('FrozenSet cannot be modified.')
225
- return this // eslint-disable-line
226
- }
227
-
228
- freeze() {
229
- return Object.freeze(this)
230
- }
231
- }
232
-
233
- export const compact = (...arr) => arr.filter((x) => !isEmpty(x))
234
-
235
- /**
236
- * Creates a function that can be used to create named tuple-like objects.
237
- * @example
238
- * ```
239
- * let Point = namedtuple('x', 'y', 'z')
240
- * let userObj = User(0, 0, 0)
241
- * // -> {x: 0, y: 0, z: 0}
242
- * ```
243
- *
244
- *
245
- * @param {...string[]} fields
246
- * @return
247
- */
248
- export function namedtuple(...fields: string[]) {
249
- return (...args) => fields.reduce((prev, f, i) => set(prev, f, args[i], false, true), {})
250
- }
251
-
252
- export function filter(arr, fn: Iteratee | PropertyKey | Object = isNull) {
253
- if (Array.isArray(arr)) {
254
- // @ts-expect-error function is not compatible?
255
- if (typeof fn === 'function') return arr.filter(fn)
256
- if (typeof fn === 'string') return arr.filter((x) => get(x, fn))
257
- if (isObject(fn)) return arr.filter(matches(fn))
258
- }
259
- }
260
-
261
- export function find(arr, fn: Iteratee | PropertyKey | Object) {
262
- if (Array.isArray(arr)) {
263
- if (typeof fn === 'function') return arr.find(fn as Iteratee)
264
- if (typeof fn === 'string') return arr.find((x) => x && x[fn])
265
- if (isObject(fn)) return arr.find(matches(fn))
266
- }
267
- }
268
-
269
- export function findRight(arr, fn: Iteratee | PropertyKey | Object) {
270
- if (Array.isArray(arr)) {
271
- for (let i = arr.length - 1; i >= 0; i--) {
272
- const x = arr[i]
273
- if (typeof fn === 'function') {
274
- if (fn(x)) return x
275
- } else if (typeof fn === 'string') {
276
- if (x && x[fn]) return x
277
- } else if (isObject(fn)) {
278
- if (matches(fn)(x)) return x
279
- }
280
- }
281
- }
282
- }
283
-
284
- export const matches = (o) => (x) => {
285
- if (!o || !x) return false
286
- for (const key in o) {
287
- if (!eq(x[key], o[key])) return false
288
- }
289
- return true
290
- }
291
-
292
- export function forEach(arr: any[], fn: Iteratee)
293
- export function forEach(arr: Object, fn: Iteratee)
294
- export function forEach(arr: any, fn: any) {
295
- if (Array.isArray(arr)) return arr.forEach(fn)
296
- if (isObject(arr)) {
297
- return Object.keys(arr).forEach((key) => fn(get(arr, key), key, arr))
298
- }
299
- }
300
-
301
- export function flatten(arr, depth: boolean | number = 1) {
302
- if (!depth) return arr
303
- if (Array.isArray(arr)) return flattenArray(arr, depth, [])
304
- if (isObject(arr)) return flattenObj(arr)
305
- return arr
306
- }
307
-
308
- export function flattenArray(arr: any[], depth: boolean | number = 1, result: any[] = []) {
309
- for (const value of arr) {
310
- if (depth && Array.isArray(value)) {
311
- flattenArray(value, typeof depth === 'number' ? depth - 1 : depth, result)
312
- } else {
313
- result.push(value)
314
- }
315
- }
316
- return result
317
- }
318
-
319
- export function flattenObj(o: any, prefix = '', result = {}, keepNull = false) {
320
- if (isString(o) || isNumber(o) || isBool(o) || (keepNull && isNull(o))) {
321
- result[prefix] = o
322
- return result
323
- }
324
-
325
- if (isArray(o) || isObject(o)) {
326
- for (const i in o) {
327
- let pref = prefix
328
- if (isArray(o)) {
329
- pref = pref + `[${i}]`
330
- } else {
331
- if (isEmpty(prefix)) {
332
- pref = i
333
- } else {
334
- pref = prefix + '.' + i
335
- }
336
- }
337
- flattenObj(o[i], pref, result, keepNull)
338
- }
339
- return result
340
- }
341
- return result
342
- }
343
-
344
- export const map = (arr, fn: Function | string) => {
345
- if (Array.isArray(arr)) {
346
- return arr.map((x, i) => {
347
- if (typeof fn === 'string') return x[fn]
348
- return fn(x, i, arr)
349
- })
350
- }
351
- if (isObject(arr)) {
352
- return Object.keys(arr).map((key) => {
353
- if (typeof fn === 'string') return get(arr[key], fn)
354
- return fn(arr[key], key, arr)
355
- })
356
- }
357
- }
358
-
359
- export function pick(obj, paths: Iteratee | string[]) {
360
- if (obj == null) {
361
- return {}
362
- }
363
- const result = {}
364
- if (Array.isArray(paths)) {
365
- for (const key of paths) {
366
- result[key] = obj[key]
367
- }
368
- return result
369
- } else if (typeof paths === 'function') {
370
- for (const key in obj) {
371
- const value = obj[key]
372
- if (paths(value, key)) result[key] = value
373
- }
374
- }
375
- return result
376
- }
377
-
378
- export function omit(obj, paths: Iteratee | string[]) {
379
- const result = {}
380
- for (const key in obj) {
381
- const value = obj[key]
382
- if (Array.isArray(paths) && !paths.includes(key)) {
383
- result[key] = value
384
- } else if (typeof paths === 'function' && !paths(value, key)) {
385
- result[key] = value
386
- }
387
- }
388
- return result
389
- }
390
-
391
- export function contains(arr, y) {
392
- const op = call(arr, 'contains', y)
393
- if (op != null) return op
394
- return indexOf(arr, y) >= 0
395
- }
396
-
397
- export function indexOf(obj, x, start = 0) {
398
- if (obj == null) return
399
- const op = call(obj, 'indexOf', x, start)
400
- if (op != null) return op
401
- const length = obj.length
402
- if (length && start < 0) {
403
- start = Math.max(start + length, 0)
404
- }
405
- if (!length || start >= length) return -1
406
- for (; start < length; start++) {
407
- if (eq(x, obj[start])) return start
408
- }
409
- return -1
410
- }
411
-
412
- export function lastIndexOf(obj, x, start?) {
413
- if (obj == null) return
414
- const op = call(obj, 'lastIndexOf', x, start)
415
- if (op != null) return op
416
- const length = obj.length
417
- if (start == null) start = length - 1
418
- if (!length || start < 0) return -1
419
- for (; start >= 0; start--) {
420
- if (eq(x, obj[start])) return start
421
- }
422
- return -1
423
- }
424
-
425
- /**
426
- * Creates a clone of the given `obj`. If `deep` is `true` it will clone it recursively.
427
- *
428
- *
429
- * @param {*} obj
430
- * @param {boolean} [deep=false]
431
- * @return The clone value
432
- */
433
- export function clone(obj, deep = false) {
434
- if (ArrayBuffer.isView(obj)) {
435
- return cloneTypedArray(obj, deep)
436
- }
437
- if (isArray(obj) || isArrayLike(obj)) {
438
- return cloneArray(obj, deep)
439
- }
440
- if (isObject(obj)) {
441
- if (isFunc(obj.clone)) {
442
- return obj.clone()
443
- }
444
- const copy = {}
445
- for (const key of keys(obj)) {
446
- if (deep) {
447
- copy[key] = clone(obj[key], deep)
448
- } else {
449
- copy[key] = obj[key]
450
- }
451
- }
452
- return copy
453
- }
454
- return obj
455
- }
456
-
457
- export function cloneArray(arr, deep = false) {
458
- if (!deep) return Array.from(arr)
459
- return arr.map((item) => clone(item, deep))
460
- }
461
-
462
- export function cloneArrayBuffer(arrayBuffer) {
463
- const result = new arrayBuffer.constructor(arrayBuffer.byteLength)
464
- new Uint8Array(result).set(new Uint8Array(arrayBuffer))
465
- return result
466
- }
467
-
468
- export function cloneTypedArray(typedArray, isDeep) {
469
- const buffer = isDeep ? cloneArrayBuffer(typedArray.buffer) : typedArray.buffer
470
- return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length)
471
- }
472
-
473
- export function uniq<T = any>(arr: T[], fn: string | Iteratee = id) {
474
- const keys = new Set<T>()
475
- for (const x of arr) {
476
- if (typeof fn === 'function') {
477
- keys.add(fn(x))
478
- } else {
479
- keys.add(get(x, fn))
480
- }
481
- }
482
- return Array.from(keys.values())
483
- }
484
-
485
- export function sortedUniq(arr, fn: string | Iteratee = id) {
486
- const lst = uniq(arr, fn)
487
- lst.sort(comp)
488
- return lst
489
- }
490
-
491
- function baseMergeDeep(obj, source, key, stack) {
492
- const objValue = obj[key]
493
- const srcValue = source[key]
494
- const stacked = stack.get(srcValue)
495
- if (stacked && (objValue !== stacked || !(key in obj))) {
496
- obj[key] = stacked
497
- return
498
- }
499
-
500
- let newValue: any
501
- let isCommon = newValue === undefined
502
- const isArray = Array.isArray(srcValue)
503
- const isTyped = ArrayBuffer.isView(srcValue)
504
-
505
- if (isArray || isTyped) {
506
- if (Array.isArray(objValue)) {
507
- newValue = objValue
508
- } else if (isArrayLike(objValue)) {
509
- newValue = Array.from(objValue)
510
- } else if (isTyped) {
511
- isCommon = false
512
- newValue = cloneTypedArray(srcValue, true)
513
- } else {
514
- newValue = []
515
- }
516
- } else if (isObject(srcValue)) {
517
- newValue = objValue
518
- if (!isObject(objValue)) {
519
- newValue = srcValue
520
- }
521
- } else {
522
- isCommon = false
523
- }
524
- if (isCommon) {
525
- stack.set(srcValue, newValue)
526
- baseMerge(newValue, srcValue, stack)
527
- stack.delete(srcValue)
528
- }
529
- obj[key] = newValue
530
- }
531
-
532
- function baseMerge(obj, source, stack?) {
533
- if (obj === source) return obj
534
- for (const key in source) {
535
- const srcValue = source[key]
536
- const objValue = obj[key]
537
- if (typeof srcValue === 'object' && srcValue !== null) {
538
- baseMergeDeep(obj, source, key, stack || new WeakMap())
539
- } else {
540
- if (objValue !== srcValue) {
541
- obj[key] = srcValue
542
- }
543
- }
544
- }
545
- }
546
-
547
- /**
548
- * Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to `undefined` are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
549
- *
550
- * Note: this method mutates `obj`
551
- *
552
- *
553
- * @param {Object} obj
554
- * @param {...Object} sources
555
- * @return {Object} The destination object.
556
- */
557
- export function merge(obj, ...sources): Object {
558
- for (const source of sources) {
559
- baseMerge(obj, source)
560
- }
561
- return obj
562
- }
563
-
564
- export function* difference(...args) {
565
- const sets = args.map((arr) => new Set(arr))
566
- const setA = sets[0]
567
-
568
- for (let i = 1; i < sets.length; i++) {
569
- for (const x of sets[i].values()) {
570
- if (!setA.delete(x)) setA.add(x)
571
- }
572
- }
573
-
574
- for (const x of setA.values()) {
575
- yield x
576
- }
577
- }
578
-
579
- export function* intersection(...args) {
580
- const sets = args.map((arr) => new Set(arr))
581
- // build a counter map to find items in all
582
- const results = new Map()
583
- const total = sets.length
584
-
585
- for (let i = 0; i < total; i++) {
586
- for (const x of sets[i].values()) {
587
- const count = results.get(x) || 0
588
- results.set(x, count + 1)
589
- }
590
- }
591
-
592
- for (const [key, value] of results.entries()) {
593
- if (value === total) yield key
594
- }
595
- }
596
-
597
- export function* union(...args) {
598
- const results = new Set()
599
- for (const arr of args) {
600
- for (const item of arr) {
601
- results.add(item)
602
- }
603
- }
604
- for (const item of results.values()) {
605
- yield item
606
- }
607
- }
608
-
609
- /**
610
- * Creates an object composed of keys generated from the results of running each element of `arr` thru `func`. The order of grouped values is determined by the order they occur in `arr`. The corresponding value of each key is an array of elements responsible for generating the key.
611
- *
612
- *
613
- * @param {(any[] | Object)} arr The collection to iterate over.
614
- * @param {(Iteratee | PropertyKey)} [func=id] The iteratee to transform keys.
615
- * @return {Object} Returns the composed aggregate object.
616
- */
617
- export function groupBy(arr: any[] | Object, func: Iteratee | PropertyKey = id): Object {
618
- const results = {}
619
- if (Array.isArray(arr)) {
620
- for (let i = 0; i < arr.length; i++) {
621
- const item = arr[i]
622
- const groupKey = typeof func === 'function' ? func(item, i) : get(item, func)
623
- const values = results[groupKey] || []
624
- values.push(item)
625
- results[groupKey] = values
626
- }
627
- } else {
628
- for (const k in arr) {
629
- const item = arr[k]
630
- const groupKey = typeof func === 'function' ? func(item, k) : get(item, func)
631
- const values = results[groupKey]
632
- values.push(item)
633
- results[groupKey] = values
634
- }
635
- }
636
- return results
637
- }
package/src/datetime.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const now: number;
package/src/datetime.js DELETED
@@ -1 +0,0 @@
1
- export const now = new Date().getTime();
package/src/datetime.ts DELETED
@@ -1 +0,0 @@
1
- export const now = new Date().getTime()