atomic-fns 0.2.2 → 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.
- package/README.md +5 -3
- package/package.json +6 -7
- package/src/collections/index.d.ts +658 -0
- package/src/collections/index.js +1068 -0
- package/src/collections/index.ts +1162 -0
- package/src/datetime/index.d.ts +6 -0
- package/src/datetime/index.js +6 -0
- package/src/datetime/index.ts +7 -0
- package/src/{functools.d.ts → functools/index.d.ts} +8 -7
- package/src/functools/index.js +84 -0
- package/src/{functools.ts → functools/index.ts} +9 -7
- package/src/globals/index.d.ts +291 -0
- package/src/globals/index.js +391 -0
- package/src/globals/index.ts +456 -0
- package/src/index.d.ts +8 -7
- package/src/index.js +8 -7
- package/src/index.ts +8 -7
- package/src/itertools/index.d.ts +254 -0
- package/src/itertools/index.js +485 -0
- package/src/itertools/index.ts +511 -0
- package/src/math/index.d.ts +21 -0
- package/src/math/index.js +39 -0
- package/src/math/index.ts +44 -0
- package/src/{operators.d.ts → operators/index.d.ts} +8 -6
- package/src/operators/index.js +171 -0
- package/src/{operators.ts → operators/index.ts} +75 -30
- package/src/random/index.d.ts +42 -0
- package/src/random/index.js +65 -0
- package/src/random/index.ts +66 -0
- package/tests/collections.spec.ts +42 -25
- package/tests/globals.spec.ts +51 -47
- package/tests/itertools.spec.ts +190 -4
- package/tests/operators.spec.ts +1 -26
- package/tests/random.spec.ts +46 -0
- package/tsconfig.json +12 -4
- package/src/collections.d.ts +0 -228
- package/src/collections.js +0 -553
- package/src/collections.ts +0 -641
- package/src/datetime.d.ts +0 -5
- package/src/datetime.js +0 -5
- package/src/datetime.ts +0 -6
- package/src/functools.js +0 -78
- package/src/globals.d.ts +0 -179
- package/src/globals.js +0 -309
- package/src/globals.ts +0 -377
- package/src/itertools.d.ts +0 -20
- package/src/itertools.js +0 -98
- package/src/itertools.ts +0 -116
- package/src/math.d.ts +0 -16
- package/src/math.js +0 -70
- package/src/math.ts +0 -77
- package/src/objects.d.ts +0 -0
- package/src/objects.js +0 -1
- package/src/objects.ts +0 -0
- package/src/operators.js +0 -109
- package/src/random.d.ts +0 -22
- package/src/random.js +0 -48
- package/src/random.ts +0 -53
- package/src/strings.d.ts +0 -0
- package/src/strings.js +0 -1
- package/src/strings.ts +0 -0
- package/src/util.d.ts +0 -0
- package/src/util.js +0 -1
- 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/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
|