atomic-fns 0.1.1 → 0.2.2
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/.esdoc.json +27 -0
- package/.eslintrc.json +1 -0
- package/README.md +16 -17
- package/package.json +24 -13
- package/{build → src}/collections.d.ts +34 -10
- package/src/collections.js +553 -0
- package/src/collections.ts +48 -21
- package/{build → src}/datetime.d.ts +4 -0
- package/src/datetime.js +5 -0
- package/src/datetime.ts +5 -0
- package/{build → src}/functools.d.ts +7 -3
- package/{build → src}/functools.js +48 -49
- package/src/functools.ts +7 -3
- package/src/globals.d.ts +178 -4
- package/src/globals.js +309 -0
- package/src/globals.ts +24 -24
- package/{build → src}/index.d.ts +0 -0
- package/src/index.js +7 -0
- package/{build → src}/itertools.d.ts +4 -0
- package/src/itertools.js +98 -0
- package/src/itertools.ts +8 -5
- package/{build → src}/math.d.ts +1 -1
- package/src/math.js +70 -0
- package/src/math.ts +5 -1
- package/{build → src}/objects.d.ts +0 -0
- package/src/objects.js +1 -0
- package/{build → src}/operators.d.ts +1 -1
- package/src/operators.js +109 -0
- package/src/operators.ts +5 -1
- package/src/random.d.ts +22 -0
- package/src/random.js +48 -0
- package/src/random.ts +32 -8
- package/{build → src}/strings.d.ts +0 -0
- package/src/strings.js +1 -0
- package/{build → src}/util.d.ts +0 -0
- package/src/util.js +1 -0
- package/tests/collections.spec.ts +81 -9
- package/tests/globals.spec.ts +44 -4
- package/tests/itertools.spec.ts +17 -10
- package/tests/operators.spec.ts +8 -5
- package/tsconfig.json +17 -3
- package/build/collections.js +0 -545
- package/build/datetime.js +0 -1
- package/build/globals.d.ts +0 -175
- package/build/globals.js +0 -332
- package/build/index.js +0 -7
- package/build/itertools.js +0 -102
- package/build/math.js +0 -67
- package/build/objects.js +0 -1
- package/build/operators.js +0 -128
- package/build/random.d.ts +0 -5
- package/build/random.js +0 -28
- package/build/strings.js +0 -1
- package/build/util.js +0 -1
package/src/math.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Math
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { call, isFunc } from './globals.js'
|
|
6
|
+
import { comp } from './operators.js'
|
|
7
|
+
export function abs(x) {
|
|
8
|
+
const op = call(x, 'abs')
|
|
9
|
+
if (op != null) return op
|
|
10
|
+
return Math.abs(x)
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns a tuple like (x / y, x % y)
|
|
14
|
+
*
|
|
15
|
+
* @param {number} x
|
|
16
|
+
* @param {number} y
|
|
17
|
+
* @return {number[]}
|
|
18
|
+
*/
|
|
19
|
+
export function divmod(x, y) {
|
|
20
|
+
return [Math.floor(x / y), x % y]
|
|
21
|
+
}
|
|
22
|
+
export function log2(x) {
|
|
23
|
+
if (x > 0) {
|
|
24
|
+
return Math.log(x) * 1.442695
|
|
25
|
+
}
|
|
26
|
+
return Number.NaN
|
|
27
|
+
}
|
|
28
|
+
export function logBase(x, y) {
|
|
29
|
+
if (x > 0 && y > 0) {
|
|
30
|
+
return Math.log(y) / Math.log(x)
|
|
31
|
+
}
|
|
32
|
+
return Number.NaN
|
|
33
|
+
}
|
|
34
|
+
/** First argument can be a comparer func */
|
|
35
|
+
export function min(args) {
|
|
36
|
+
// Uses `le` by default
|
|
37
|
+
let cmp = comp
|
|
38
|
+
if (isFunc(args[0])) {
|
|
39
|
+
cmp = args.shift()
|
|
40
|
+
}
|
|
41
|
+
let res = args[0]
|
|
42
|
+
for (const x of args) {
|
|
43
|
+
if (cmp(x, res) === -1) {
|
|
44
|
+
res = x
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return res
|
|
48
|
+
}
|
|
49
|
+
/** First argument can be a comparer func */
|
|
50
|
+
export function max(args) {
|
|
51
|
+
// Uses ge by default
|
|
52
|
+
let cmp = comp
|
|
53
|
+
if (isFunc(args[0])) {
|
|
54
|
+
cmp = args.shift()
|
|
55
|
+
}
|
|
56
|
+
let res = args[0]
|
|
57
|
+
for (const x of args) {
|
|
58
|
+
if (cmp(x, res) === 1) {
|
|
59
|
+
res = x
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return res
|
|
63
|
+
}
|
|
64
|
+
export function sum(args) {
|
|
65
|
+
let total = 0
|
|
66
|
+
for (const x of args) {
|
|
67
|
+
total += x
|
|
68
|
+
}
|
|
69
|
+
return total
|
|
70
|
+
}
|
package/src/math.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Math
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
1
5
|
import { call, isFunc } from './globals.js'
|
|
2
6
|
import { comp } from './operators.js'
|
|
3
7
|
|
|
@@ -9,7 +13,7 @@ export function abs(x: number) {
|
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
15
|
* Returns a tuple like (x / y, x % y)
|
|
12
|
-
*
|
|
16
|
+
*
|
|
13
17
|
* @param {number} x
|
|
14
18
|
* @param {number} y
|
|
15
19
|
* @return {number[]}
|
|
File without changes
|
package/src/objects.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict'
|
|
@@ -21,7 +21,7 @@ export declare function mod(x: any, y: any): any;
|
|
|
21
21
|
/**
|
|
22
22
|
* Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
|
|
23
23
|
* it will return `x.pow(y)`.
|
|
24
|
-
*
|
|
24
|
+
*
|
|
25
25
|
* @param {*} x
|
|
26
26
|
* @param {*} y
|
|
27
27
|
* @return `x` raised to the power `y`
|
package/src/operators.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
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
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operators
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
1
5
|
import { call, isNumber, isObject } from './globals.js'
|
|
2
6
|
|
|
3
7
|
export const id = (x) => x
|
|
@@ -114,7 +118,7 @@ export function mod(x, y) {
|
|
|
114
118
|
/**
|
|
115
119
|
* Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
|
|
116
120
|
* it will return `x.pow(y)`.
|
|
117
|
-
*
|
|
121
|
+
*
|
|
118
122
|
* @param {*} x
|
|
119
123
|
* @param {*} y
|
|
120
124
|
* @return `x` raised to the power `y`
|
package/src/random.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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
CHANGED
|
@@ -1,23 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Random
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { isObject } from './globals'
|
|
2
6
|
|
|
7
|
+
/** Returns a random float between a [min, max) value (max exclusive). */
|
|
3
8
|
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
/** Returns a random integer between a [min, max] value (inclusive). */
|
|
11
|
+
export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
|
|
6
12
|
|
|
13
|
+
/** Returns the item at some random index in a sequence. */
|
|
7
14
|
export const choice = (arr: any[]) => {
|
|
8
|
-
if (!arr
|
|
9
|
-
|
|
10
|
-
return arr[index]
|
|
15
|
+
if (!arr || !arr.length) return
|
|
16
|
+
return arr[randomInt(0, arr.length - 1)]
|
|
11
17
|
}
|
|
12
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
|
+
*/
|
|
13
27
|
export function sample(arr: any[], k: number) {
|
|
14
|
-
if (!arr
|
|
15
|
-
const result:
|
|
16
|
-
|
|
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
|
+
}
|
|
17
33
|
return result
|
|
18
34
|
}
|
|
19
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
|
+
*/
|
|
20
42
|
export function shuffle(arr) {
|
|
43
|
+
if (!arr || !arr.length) return
|
|
21
44
|
if (isObject(arr)) arr = Object.values(arr)
|
|
22
45
|
const n = arr.length - 1
|
|
23
46
|
for (let i = n; i > 0; i--) {
|
|
@@ -26,4 +49,5 @@ export function shuffle(arr) {
|
|
|
26
49
|
arr[j] = arr[i]
|
|
27
50
|
arr[i] = temp
|
|
28
51
|
}
|
|
52
|
+
return arr
|
|
29
53
|
}
|
|
File without changes
|
package/src/strings.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict'
|
package/{build → src}/util.d.ts
RENAMED
|
File without changes
|
package/src/util.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict'
|
|
@@ -25,13 +25,18 @@ describe('collections', () => {
|
|
|
25
25
|
assert(!contains([4, 7, 1, 9], NaN))
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
it('clone', () => {
|
|
28
|
+
it('clone array', () => {
|
|
29
29
|
let arr = [4, 7, 1, 9]
|
|
30
30
|
assert.deepEqual(clone(arr), arr)
|
|
31
31
|
let target = { user: 'fred', age: 40, active: false }
|
|
32
32
|
assert.deepEqual(clone(target), target)
|
|
33
33
|
})
|
|
34
34
|
|
|
35
|
+
it('clone object', () => {
|
|
36
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
37
|
+
assert.deepEqual(clone(target), target)
|
|
38
|
+
})
|
|
39
|
+
|
|
35
40
|
it('difference', () => {
|
|
36
41
|
let a = [1, 2, 5]
|
|
37
42
|
let b = [3, 4, 5]
|
|
@@ -56,11 +61,27 @@ describe('collections', () => {
|
|
|
56
61
|
{ user: 'barney', age: 36, active: true },
|
|
57
62
|
{ user: 'fred', age: 40, active: false }
|
|
58
63
|
]
|
|
59
|
-
assert.deepEqual(filter(users, { age: 36 }), [users[0]])
|
|
60
64
|
assert.deepEqual(
|
|
61
65
|
filter(users, (o) => o.active),
|
|
62
66
|
[users[0]]
|
|
63
67
|
)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('filter matches', () => {
|
|
71
|
+
assert(filter(null) == undefined)
|
|
72
|
+
let users = [
|
|
73
|
+
{ user: 'barney', age: 36, active: true },
|
|
74
|
+
{ user: 'fred', age: 40, active: false }
|
|
75
|
+
]
|
|
76
|
+
assert.deepEqual(filter(users, { age: 36 }), [users[0]])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('filter property', () => {
|
|
80
|
+
assert(filter(null) == undefined)
|
|
81
|
+
let users = [
|
|
82
|
+
{ user: 'barney', age: 36, active: true },
|
|
83
|
+
{ user: 'fred', age: 40, active: false }
|
|
84
|
+
]
|
|
64
85
|
assert.deepEqual(filter(users, 'active'), [users[0]])
|
|
65
86
|
})
|
|
66
87
|
|
|
@@ -69,8 +90,22 @@ describe('collections', () => {
|
|
|
69
90
|
{ user: 'barney', age: 36, active: true },
|
|
70
91
|
{ user: 'fred', age: 40, active: false }
|
|
71
92
|
]
|
|
72
|
-
assert(find(users, { age: 36 }) === users[0])
|
|
73
93
|
assert(find(users, (o) => o.active) === users[0])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('find matches', () => {
|
|
97
|
+
let users = [
|
|
98
|
+
{ user: 'barney', age: 36, active: true },
|
|
99
|
+
{ user: 'fred', age: 40, active: false }
|
|
100
|
+
]
|
|
101
|
+
assert(find(users, { age: 36 }) === users[0])
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('find property', () => {
|
|
105
|
+
let users = [
|
|
106
|
+
{ user: 'barney', age: 36, active: true },
|
|
107
|
+
{ user: 'fred', age: 40, active: false }
|
|
108
|
+
]
|
|
74
109
|
assert(find(users, 'active') === users[0])
|
|
75
110
|
})
|
|
76
111
|
|
|
@@ -81,7 +116,23 @@ describe('collections', () => {
|
|
|
81
116
|
{ user: 'barney', age: 36, active: true }
|
|
82
117
|
]
|
|
83
118
|
assert(findRight(users, { age: 36 }) === users[2])
|
|
84
|
-
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('findRight matches', () => {
|
|
122
|
+
let users = [
|
|
123
|
+
{ user: 'barney', age: 36, active: true },
|
|
124
|
+
{ user: 'fred', age: 40, active: false },
|
|
125
|
+
{ user: 'barney', age: 36, active: true }
|
|
126
|
+
]
|
|
127
|
+
assert(findRight(users, { age: 36 }) === users[2])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('findRight property', () => {
|
|
131
|
+
let users = [
|
|
132
|
+
{ user: 'barney', age: 36, active: true },
|
|
133
|
+
{ user: 'fred', age: 40, active: false },
|
|
134
|
+
{ user: 'barney', age: 36, active: true }
|
|
135
|
+
]
|
|
85
136
|
assert(findRight(users, 'active') === users[2])
|
|
86
137
|
})
|
|
87
138
|
|
|
@@ -90,7 +141,8 @@ describe('collections', () => {
|
|
|
90
141
|
{ user: 'barney', age: 36, active: true },
|
|
91
142
|
{ user: 'fred', age: 40, active: false }
|
|
92
143
|
]
|
|
93
|
-
let result = []
|
|
144
|
+
let result = []
|
|
145
|
+
// @ts-expect-error
|
|
94
146
|
forEach(users, (value, key) => result.push([key, value]))
|
|
95
147
|
assert.deepEqual(
|
|
96
148
|
result,
|
|
@@ -100,15 +152,19 @@ describe('collections', () => {
|
|
|
100
152
|
|
|
101
153
|
it('forEach object key', () => {
|
|
102
154
|
let target = { user: 'fred', age: 40, active: false }
|
|
103
|
-
let objs = []
|
|
155
|
+
let objs = []
|
|
156
|
+
// @ts-expect-error
|
|
104
157
|
forEach(target, (value, key) => objs.push([key, value]))
|
|
105
158
|
assert.deepEqual(objs, Object.entries(target))
|
|
106
159
|
})
|
|
107
160
|
|
|
108
|
-
it('
|
|
161
|
+
it('flatten', () => {
|
|
109
162
|
let data = flatten([1, 2, [3, 4], 5])
|
|
110
163
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
111
|
-
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('flattenDeep', () => {
|
|
167
|
+
let data = flatten([1, 2, [3, [4]], [5]], true)
|
|
112
168
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
113
169
|
})
|
|
114
170
|
|
|
@@ -174,9 +230,21 @@ describe('collections', () => {
|
|
|
174
230
|
)
|
|
175
231
|
})
|
|
176
232
|
|
|
233
|
+
it('pickBy', () => {
|
|
234
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
235
|
+
assert.deepEqual(
|
|
236
|
+
pick(target, (x) => x >= 40),
|
|
237
|
+
{ age: 40 }
|
|
238
|
+
)
|
|
239
|
+
})
|
|
240
|
+
|
|
177
241
|
it('omit', () => {
|
|
178
242
|
let target = { user: 'fred', age: 40, active: false }
|
|
179
243
|
assert.deepEqual(omit(target, ['age', 'active']), { user: 'fred' })
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('omitBy', () => {
|
|
247
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
180
248
|
assert.deepEqual(
|
|
181
249
|
omit(target, (x) => !x),
|
|
182
250
|
{ user: 'fred', age: 40 }
|
|
@@ -193,7 +261,11 @@ describe('collections', () => {
|
|
|
193
261
|
let arr = [4, 7, 1, 1, 9, 4, 7]
|
|
194
262
|
let distinct = uniq(arr)
|
|
195
263
|
assert.deepEqual(distinct, [4, 7, 1, 9])
|
|
196
|
-
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('uniqBy', () => {
|
|
267
|
+
let arr = [4, 7, 1, 1, 9, 4, 7]
|
|
268
|
+
let distinct = uniq(arr, (x) => x * 2)
|
|
197
269
|
assert.deepEqual(distinct, [8, 14, 2, 18])
|
|
198
270
|
})
|
|
199
271
|
})
|
package/tests/globals.spec.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
values,
|
|
27
27
|
round,
|
|
28
28
|
enumerate,
|
|
29
|
+
isEmpty,
|
|
29
30
|
isGenerator,
|
|
30
31
|
hash,
|
|
31
32
|
hashCode,
|
|
@@ -36,8 +37,7 @@ import {
|
|
|
36
37
|
list,
|
|
37
38
|
next,
|
|
38
39
|
get
|
|
39
|
-
} from '../
|
|
40
|
-
import { isEmpty } from '../src/globals.js'
|
|
40
|
+
} from '../src/globals.js'
|
|
41
41
|
|
|
42
42
|
function cb() {}
|
|
43
43
|
|
|
@@ -47,9 +47,11 @@ describe('globals', () => {
|
|
|
47
47
|
assert.equal(err.message, 'custom')
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
+
it('True', () => assert(True() === true))
|
|
51
|
+
|
|
52
|
+
it('False', () => assert(False() === false))
|
|
53
|
+
|
|
50
54
|
it('type', () => {
|
|
51
|
-
assert(True())
|
|
52
|
-
assert(!False())
|
|
53
55
|
assert(type(null) === 'null')
|
|
54
56
|
assert(type(undefined) === 'undefined')
|
|
55
57
|
assert(type(true) === 'boolean')
|
|
@@ -70,35 +72,71 @@ describe('globals', () => {
|
|
|
70
72
|
assert(type(type) === 'function')
|
|
71
73
|
assert(type(Symbol()) === 'symbol')
|
|
72
74
|
assert(type(new Promise(cb)) === 'promise')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('isBool', () => {
|
|
73
78
|
assert(isBool(true))
|
|
74
79
|
assert(isBool(Boolean()))
|
|
75
80
|
assert(!isBool(''))
|
|
76
81
|
assert(!isBool({}))
|
|
77
82
|
assert(!isBool(undefined))
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('isObject', () => {
|
|
78
86
|
assert(isObject({}))
|
|
79
87
|
assert(!isObject(''))
|
|
80
88
|
assert(!isObject([]))
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('isString', () => {
|
|
81
92
|
assert(isString(''))
|
|
82
93
|
assert(!isString([]))
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('isArray', () => {
|
|
83
97
|
assert(isArray([]))
|
|
84
98
|
assert(isArrayLike([]))
|
|
85
99
|
assert(!isArray(''))
|
|
86
100
|
assert(!isArrayLike(''))
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('isFunc', () => {
|
|
87
104
|
assert(isFunc(cb))
|
|
88
105
|
assert(!isFunc([]))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('isAsync', () => {
|
|
89
109
|
assert(isAsync(async function () {}))
|
|
90
110
|
assert(isAsync(async () => {}))
|
|
91
111
|
assert(!isAsync(cb))
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('isNaN', () => {
|
|
92
115
|
assert(isNaN(NaN))
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('isNumber', () => {
|
|
93
119
|
assert(!isNumber(NaN))
|
|
94
120
|
assert(isNumber(42))
|
|
95
121
|
assert(isNumber(Number(42)))
|
|
96
122
|
assert(isNumber(Number('42')))
|
|
97
123
|
assert(isNumber(parseFloat('42.0')))
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('isBigInt', () => {
|
|
98
127
|
assert(isBigint(42n))
|
|
99
128
|
assert(isBigint(BigInt('42')))
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('isPromise', () => {
|
|
100
132
|
assert(isPromise(new Promise(cb)))
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('isSymbol', () => {
|
|
101
136
|
assert(isSymbol(Symbol()))
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('isNull', () => {
|
|
102
140
|
assert(isNull(null))
|
|
103
141
|
assert(isNull(undefined))
|
|
104
142
|
assert(!isNull(''))
|
|
@@ -109,6 +147,8 @@ describe('globals', () => {
|
|
|
109
147
|
it('str', () => {
|
|
110
148
|
assert(str(undefined) === '')
|
|
111
149
|
assert(str(null) === '')
|
|
150
|
+
assert(str(true) === 'true')
|
|
151
|
+
assert(str(false) === 'false')
|
|
112
152
|
assert(str(NaN) === 'NaN')
|
|
113
153
|
assert(str(42) === '42')
|
|
114
154
|
assert(str([]) === '')
|
package/tests/itertools.spec.ts
CHANGED
|
@@ -15,17 +15,21 @@ describe('operators', () => {
|
|
|
15
15
|
it('sort', () => {
|
|
16
16
|
let arr = [3, 2, 1]
|
|
17
17
|
assert.deepEqual(sort(arr), [1, 2, 3])
|
|
18
|
-
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('sort reversed', () => {
|
|
21
|
+
let arr = [3, 1, 2]
|
|
19
22
|
assert.deepEqual(sort(arr, true), [3, 2, 1])
|
|
20
23
|
})
|
|
21
24
|
|
|
22
25
|
it('sorted', () => {
|
|
23
26
|
let arr = [3, 2, 1]
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
assert.deepEqual(sort, arr)
|
|
27
|
-
// reversed order
|
|
27
|
+
assert.deepEqual(sorted(arr), arr.sort())
|
|
28
|
+
// sorted in reverse
|
|
28
29
|
assert.deepEqual(sorted([3, 1, 2], true), [3, 2, 1])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('sorted by key', () => {
|
|
29
33
|
let users = [
|
|
30
34
|
{ user: 'fred', age: 40 },
|
|
31
35
|
{ user: 'barney', age: 36 }
|
|
@@ -46,7 +50,13 @@ describe('operators', () => {
|
|
|
46
50
|
{ user: 'barney', age: 36 }
|
|
47
51
|
]
|
|
48
52
|
)
|
|
49
|
-
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('sorted with custom compare', () => {
|
|
56
|
+
let users = [
|
|
57
|
+
{ user: 'fred', age: 40 },
|
|
58
|
+
{ user: 'barney', age: 36 }
|
|
59
|
+
]
|
|
50
60
|
assert.deepEqual(
|
|
51
61
|
sorted(users, false, (x, y) => (x.user <= y.user ? -1 : 1)),
|
|
52
62
|
[
|
|
@@ -71,10 +81,7 @@ describe('operators', () => {
|
|
|
71
81
|
{ user: 'barney', age: 36 }
|
|
72
82
|
]
|
|
73
83
|
|
|
74
|
-
let results = []
|
|
75
|
-
for (const t of zip(users1, users2)) {
|
|
76
|
-
results.push(t)
|
|
77
|
-
}
|
|
84
|
+
let results = [...zip(users1, users2)]
|
|
78
85
|
|
|
79
86
|
let [first1, first2] = results[0]
|
|
80
87
|
assert(first1 === users1[0])
|