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
package/src/math.ts DELETED
@@ -1,77 +0,0 @@
1
- /**
2
- * Math
3
- * @module
4
- */
5
- import { call, isFunc } from './globals.js'
6
- import { comp } from './operators.js'
7
-
8
- export function abs(x: number) {
9
- const op = call(x, 'abs')
10
- if (op != null) return op
11
- return Math.abs(x)
12
- }
13
-
14
- /**
15
- * Returns a tuple like (x / y, x % y)
16
- *
17
- * @param {number} x
18
- * @param {number} y
19
- * @return {number[]}
20
- */
21
- export function divmod(x: number, y: number) {
22
- return [Math.floor(x / y), x % y]
23
- }
24
-
25
- export function log2(x) {
26
- if (x > 0) {
27
- return Math.log(x) * 1.442695
28
- }
29
- return Number.NaN
30
- }
31
-
32
- export function logBase(x, y) {
33
- if (x > 0 && y > 0) {
34
- return Math.log(y) / Math.log(x)
35
- }
36
- return Number.NaN
37
- }
38
-
39
- /** First argument can be a comparer func */
40
- export function min(args) {
41
- // Uses `le` by default
42
- let cmp = comp
43
- if (isFunc(args[0])) {
44
- cmp = args.shift()
45
- }
46
- let res = args[0]
47
- for (const x of args) {
48
- if (cmp(x, res) === -1) {
49
- res = x
50
- }
51
- }
52
- return res
53
- }
54
-
55
- /** First argument can be a comparer func */
56
- export function max(args) {
57
- // Uses ge by default
58
- let cmp = comp
59
- if (isFunc(args[0])) {
60
- cmp = args.shift()
61
- }
62
- let res = args[0]
63
- for (const x of args) {
64
- if (cmp(x, res) === 1) {
65
- res = x
66
- }
67
- }
68
- return res
69
- }
70
-
71
- export function sum(args) {
72
- let total = 0
73
- for (const x of args) {
74
- total += x
75
- }
76
- return total
77
- }
package/src/objects.d.ts DELETED
File without changes
package/src/objects.js DELETED
@@ -1 +0,0 @@
1
- 'use strict'
package/src/objects.ts DELETED
File without changes
package/src/operators.js DELETED
@@ -1,109 +0,0 @@
1
- /**
2
- * Operators
3
- * @module
4
- */
5
- import { call, isNumber, isObject } from './globals.js'
6
- export const id = (x) => x
7
- export const bool = (x) => !!x
8
- export const not = (x) => !x
9
- export const isinstance = (x, y) => x instanceof y
10
- export const all = (arr, fn = bool) => arr.every(fn)
11
- export const any = (arr, fn = bool) => arr.some(fn)
12
- export const and = all
13
- export const or = any
14
- export function comp(x, y) {
15
- const op = call(x, 'compare', y)
16
- if (isNumber(op)) return op
17
- if (eq(x, y)) return 0
18
- if (le(x, y)) return -1
19
- if (ge(x, y)) return 1
20
- return 0
21
- }
22
- export function eq(x, y) {
23
- const op = call(x, 'eq', y)
24
- if (op != null) return op
25
- return x === y
26
- }
27
- export function shallowEqual(x, y) {
28
- if (!x || !y) return x === y
29
- if (Array.isArray(x)) {
30
- for (let i = 0; i < x.length; i++) {
31
- if (!eq(x[i], y[i])) return false
32
- }
33
- return true
34
- }
35
- if (isObject(x)) {
36
- if (eq(x, y)) return true
37
- for (const key in x) {
38
- if (!eq(x[key], y[key])) return false
39
- }
40
- return true
41
- }
42
- return eq(x, y)
43
- }
44
- export function lt(x, y) {
45
- const op = call(x, 'lt', y)
46
- if (op != null) return op
47
- return x < y
48
- }
49
- export function le(x, y) {
50
- let op = call(x, 'le', y)
51
- if (op != null) return op
52
- op = call(x, 'eq', y)
53
- if (op === true) return op
54
- op = call(x, 'lt', y)
55
- if (op != null) return op
56
- return x <= y
57
- }
58
- export function gt(x, y) {
59
- const op = call(x, 'gt', y)
60
- if (op != null) return op
61
- return x > y
62
- }
63
- export function ge(x, y) {
64
- let op = call(x, 'ge', y)
65
- if (op != null) return op
66
- op = call(x, 'eq', y)
67
- if (op === true) return op
68
- op = call(x, 'gt', y)
69
- if (op != null) return op
70
- return x >= y
71
- }
72
- export function add(x, y) {
73
- const op = call(x, 'add', y)
74
- if (op != null) return op
75
- return x + y
76
- }
77
- export function sub(x, y) {
78
- const op = call(x, 'sub', y)
79
- if (op != null) return op
80
- return x - y
81
- }
82
- export function mult(x, y) {
83
- const op = call(x, 'mult', y)
84
- if (op != null) return op
85
- return x * y
86
- }
87
- export function div(x, y) {
88
- const op = call(x, 'div', y)
89
- if (op != null) return op
90
- return x / y
91
- }
92
- export function mod(x, y) {
93
- const op = call(x, 'mod', y)
94
- if (op != null) return op
95
- return x % y
96
- }
97
- /**
98
- * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
99
- * it will return `x.pow(y)`.
100
- *
101
- * @param {*} x
102
- * @param {*} y
103
- * @return `x` raised to the power `y`
104
- */
105
- export function pow(x, y) {
106
- const op = call(x, 'pow', y)
107
- if (op != null) return op
108
- return x ** y
109
- }
package/src/operators.ts DELETED
@@ -1,130 +0,0 @@
1
- /**
2
- * Operators
3
- * @module
4
- */
5
- import { call, isNumber, isObject } from './globals.js'
6
-
7
- export const id = (x) => x
8
-
9
- export const bool = (x) => !!x
10
-
11
- export const not = (x) => !x
12
-
13
- export const isinstance = (x, y) => x instanceof y
14
-
15
- export const all = (arr: any[], fn = bool) => arr.every(fn)
16
-
17
- export const any = (arr: any[], fn = bool) => arr.some(fn)
18
-
19
- export const and = all
20
-
21
- export const or = any
22
-
23
- export function comp(x, y) {
24
- const op = call(x, 'compare', y)
25
- if (isNumber(op)) return op
26
- if (eq(x, y)) return 0
27
- if (le(x, y)) return -1
28
- if (ge(x, y)) return 1
29
- return 0
30
- }
31
-
32
- export function eq(x, y) {
33
- const op = call(x, 'eq', y)
34
- if (op != null) return op
35
- return x === y
36
- }
37
-
38
- export function shallowEqual(x, y) {
39
- if (!x || !y) return x === y
40
- if (Array.isArray(x)) {
41
- for (let i = 0; i < x.length; i++) {
42
- if (!eq(x[i], y[i])) return false
43
- }
44
- return true
45
- }
46
- if (isObject(x)) {
47
- if (eq(x, y)) return true
48
- for (const key in x) {
49
- if (!eq(x[key], y[key])) return false
50
- }
51
- return true
52
- }
53
- return eq(x, y)
54
- }
55
-
56
- export function lt(x, y) {
57
- const op = call(x, 'lt', y)
58
- if (op != null) return op
59
- return x < y
60
- }
61
-
62
- export function le(x, y) {
63
- let op = call(x, 'le', y)
64
- if (op != null) return op
65
- op = call(x, 'eq', y)
66
- if (op === true) return op
67
- op = call(x, 'lt', y)
68
- if (op != null) return op
69
- return x <= y
70
- }
71
-
72
- export function gt(x, y) {
73
- const op = call(x, 'gt', y)
74
- if (op != null) return op
75
- return x > y
76
- }
77
-
78
- export function ge(x, y) {
79
- let op = call(x, 'ge', y)
80
- if (op != null) return op
81
- op = call(x, 'eq', y)
82
- if (op === true) return op
83
- op = call(x, 'gt', y)
84
- if (op != null) return op
85
- return x >= y
86
- }
87
-
88
- export function add(x, y) {
89
- const op = call(x, 'add', y)
90
- if (op != null) return op
91
- return x + y
92
- }
93
-
94
- export function sub(x, y) {
95
- const op = call(x, 'sub', y)
96
- if (op != null) return op
97
- return x - y
98
- }
99
-
100
- export function mult(x, y) {
101
- const op = call(x, 'mult', y)
102
- if (op != null) return op
103
- return x * y
104
- }
105
-
106
- export function div(x, y) {
107
- const op = call(x, 'div', y)
108
- if (op != null) return op
109
- return x / y
110
- }
111
-
112
- export function mod(x, y) {
113
- const op = call(x, 'mod', y)
114
- if (op != null) return op
115
- return x % y
116
- }
117
-
118
- /**
119
- * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
120
- * it will return `x.pow(y)`.
121
- *
122
- * @param {*} x
123
- * @param {*} y
124
- * @return `x` raised to the power `y`
125
- */
126
- export function pow(x, y) {
127
- const op = call(x, 'pow', y)
128
- if (op != null) return op
129
- return x ** y
130
- }
package/src/random.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /** Returns a random float between a [min, max) value (max exclusive). */
2
- export declare const random: (min?: number, max?: number) => number;
3
- /** Returns a random integer between a [min, max] value (inclusive). */
4
- export declare const randomInt: (min?: number, max?: number) => number;
5
- /** Returns the item at some random index in a sequence. */
6
- export declare const choice: (arr: any[]) => any;
7
- /**
8
- * Returns a random sample of size k from a list of items.
9
- *
10
- * @export
11
- * @param {any[]} arr
12
- * @param {number} k
13
- * @return
14
- */
15
- export declare function sample(arr: any[], k: number): any[] | undefined;
16
- /**
17
- * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
18
- *
19
- * @param {*} arr
20
- * @return
21
- */
22
- export declare function shuffle(arr: any): any;
package/src/random.js DELETED
@@ -1,48 +0,0 @@
1
- /**
2
- * Random
3
- * @module
4
- */
5
- import { isObject } from './globals'
6
- /** Returns a random float between a [min, max) value (max exclusive). */
7
- export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
8
- /** Returns a random integer between a [min, max] value (inclusive). */
9
- export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
10
- /** Returns the item at some random index in a sequence. */
11
- export const choice = (arr) => {
12
- if (!arr || !arr.length) return
13
- return arr[randomInt(0, arr.length - 1)]
14
- }
15
- /**
16
- * Returns a random sample of size k from a list of items.
17
- *
18
- * @export
19
- * @param {any[]} arr
20
- * @param {number} k
21
- * @return
22
- */
23
- export function sample(arr, k) {
24
- if (!arr || !arr.length || arr.length < k) return
25
- const result = []
26
- for (let i = 0; i < k; i++) {
27
- result.push(choice(arr))
28
- }
29
- return result
30
- }
31
- /**
32
- * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
33
- *
34
- * @param {*} arr
35
- * @return
36
- */
37
- export function shuffle(arr) {
38
- if (!arr || !arr.length) return
39
- if (isObject(arr)) arr = Object.values(arr)
40
- const n = arr.length - 1
41
- for (let i = n; i > 0; i--) {
42
- const j = randomInt(0, i)
43
- const temp = arr[j]
44
- arr[j] = arr[i]
45
- arr[i] = temp
46
- }
47
- return arr
48
- }
package/src/random.ts DELETED
@@ -1,53 +0,0 @@
1
- /**
2
- * Random
3
- * @module
4
- */
5
- import { isObject } from './globals'
6
-
7
- /** Returns a random float between a [min, max) value (max exclusive). */
8
- export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
9
-
10
- /** Returns a random integer between a [min, max] value (inclusive). */
11
- export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
12
-
13
- /** Returns the item at some random index in a sequence. */
14
- export const choice = (arr: any[]) => {
15
- if (!arr || !arr.length) return
16
- return arr[randomInt(0, arr.length - 1)]
17
- }
18
-
19
- /**
20
- * Returns a random sample of size k from a list of items.
21
- *
22
- * @export
23
- * @param {any[]} arr
24
- * @param {number} k
25
- * @return
26
- */
27
- export function sample(arr: any[], k: number) {
28
- if (!arr || !arr.length || arr.length < k) return
29
- const result: typeof arr = []
30
- for (let i = 0; i < k; i++) {
31
- result.push(choice(arr))
32
- }
33
- return result
34
- }
35
-
36
- /**
37
- * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
38
- *
39
- * @param {*} arr
40
- * @return
41
- */
42
- export function shuffle(arr) {
43
- if (!arr || !arr.length) return
44
- if (isObject(arr)) arr = Object.values(arr)
45
- const n = arr.length - 1
46
- for (let i = n; i > 0; i--) {
47
- const j = randomInt(0, i)
48
- const temp = arr[j]
49
- arr[j] = arr[i]
50
- arr[i] = temp
51
- }
52
- return arr
53
- }
package/src/strings.d.ts DELETED
File without changes
package/src/strings.js DELETED
@@ -1 +0,0 @@
1
- 'use strict'
package/src/strings.ts DELETED
File without changes
package/src/util.d.ts DELETED
File without changes
package/src/util.js DELETED
@@ -1 +0,0 @@
1
- 'use strict'
package/src/util.ts DELETED
File without changes