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.
- package/.esdoc.json +19 -1
- package/.eslintrc.json +1 -0
- package/README.md +8 -9
- package/package.json +9 -9
- 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} +10 -5
- package/src/{functools.js → functools/index.js} +9 -4
- package/src/{functools.ts → functools/index.ts} +11 -5
- 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.js → operators/index.js} +69 -26
- package/src/{operators.ts → operators/index.ts} +77 -28
- 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 +119 -30
- package/tests/globals.spec.ts +91 -49
- package/tests/itertools.spec.ts +207 -14
- package/tests/operators.spec.ts +7 -29
- package/tests/random.spec.ts +46 -0
- package/tsconfig.json +19 -3
- package/src/collections.d.ts +0 -224
- package/src/collections.js +0 -570
- package/src/collections.ts +0 -637
- package/src/datetime.d.ts +0 -1
- package/src/datetime.js +0 -1
- package/src/datetime.ts +0 -1
- package/src/globals.d.ts +0 -175
- package/src/globals.js +0 -329
- package/src/globals.ts +0 -373
- package/src/itertools.d.ts +0 -16
- package/src/itertools.js +0 -101
- package/src/itertools.ts +0 -112
- package/src/math.d.ts +0 -16
- package/src/math.js +0 -67
- package/src/math.ts +0 -73
- package/src/objects.d.ts +0 -0
- package/src/objects.js +0 -1
- package/src/objects.ts +0 -0
- package/src/random.d.ts +0 -22
- package/src/random.js +0 -48
- package/src/random.ts +0 -49
- 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
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
import { call, isNumber, isObject } from '../globals/index.js';
|
|
2
7
|
export const id = (x) => x;
|
|
3
8
|
export const bool = (x) => !!x;
|
|
4
9
|
export const not = (x) => !x;
|
|
5
10
|
export const isinstance = (x, y) => x instanceof y;
|
|
6
|
-
export const all = (arr, fn = bool) => arr.every(fn);
|
|
7
|
-
export const any = (arr, fn = bool) => arr.some(fn);
|
|
8
|
-
export const and = all;
|
|
9
|
-
export const or = any;
|
|
10
11
|
export function comp(x, y) {
|
|
11
12
|
const op = call(x, 'compare', y);
|
|
12
13
|
if (isNumber(op))
|
|
@@ -25,27 +26,6 @@ export function eq(x, y) {
|
|
|
25
26
|
return op;
|
|
26
27
|
return x === y;
|
|
27
28
|
}
|
|
28
|
-
export function shallowEqual(x, y) {
|
|
29
|
-
if (!x || !y)
|
|
30
|
-
return x === y;
|
|
31
|
-
if (Array.isArray(x)) {
|
|
32
|
-
for (let i = 0; i < x.length; i++) {
|
|
33
|
-
if (!eq(x[i], y[i]))
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
if (isObject(x)) {
|
|
39
|
-
if (eq(x, y))
|
|
40
|
-
return true;
|
|
41
|
-
for (const key in x) {
|
|
42
|
-
if (!eq(x[key], y[key]))
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return eq(x, y);
|
|
48
|
-
}
|
|
49
29
|
export function lt(x, y) {
|
|
50
30
|
const op = call(x, 'lt', y);
|
|
51
31
|
if (op != null)
|
|
@@ -126,3 +106,66 @@ export function pow(x, y) {
|
|
|
126
106
|
return op;
|
|
127
107
|
return x ** y;
|
|
128
108
|
}
|
|
109
|
+
export function shallowEqual(obj, other) {
|
|
110
|
+
if (obj === other)
|
|
111
|
+
return true;
|
|
112
|
+
if (!obj || !other)
|
|
113
|
+
return false;
|
|
114
|
+
if (Array.isArray(obj)) {
|
|
115
|
+
// compare the arrays
|
|
116
|
+
if (obj.length !== other.length)
|
|
117
|
+
return false;
|
|
118
|
+
for (let i = 0; i < obj.length; i++) {
|
|
119
|
+
if (obj[i] !== other[i]) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
else if (isObject(obj)) {
|
|
126
|
+
// compare the object keys
|
|
127
|
+
const objKeys = Object.keys(obj);
|
|
128
|
+
const otherKeys = Object.keys(other);
|
|
129
|
+
if (objKeys.length !== otherKeys.length)
|
|
130
|
+
return false;
|
|
131
|
+
for (const key of objKeys) {
|
|
132
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
export function deepEqual(obj, other, checker = eq) {
|
|
141
|
+
if (checker(obj, other))
|
|
142
|
+
return true;
|
|
143
|
+
if (!obj || !other)
|
|
144
|
+
return false;
|
|
145
|
+
if (Array.isArray(obj)) {
|
|
146
|
+
// compare the arrays
|
|
147
|
+
if (obj.length !== other.length)
|
|
148
|
+
return false;
|
|
149
|
+
for (let i = 0; i < obj.length; i++) {
|
|
150
|
+
if (!deepEqual(obj[i], other[i], checker)) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
else if (isObject(obj)) {
|
|
157
|
+
// compare the object keys
|
|
158
|
+
const objKeys = Object.keys(obj);
|
|
159
|
+
const otherKeys = Object.keys(other);
|
|
160
|
+
if (objKeys.length !== otherKeys.length)
|
|
161
|
+
return false;
|
|
162
|
+
for (const key of objKeys) {
|
|
163
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) ||
|
|
164
|
+
!deepEqual(obj[key], other[key], checker)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { call, isNumber, isObject } from '../globals/index.js'
|
|
2
8
|
|
|
3
9
|
export const id = (x) => x
|
|
4
10
|
|
|
@@ -8,17 +14,9 @@ export const not = (x) => !x
|
|
|
8
14
|
|
|
9
15
|
export const isinstance = (x, y) => x instanceof y
|
|
10
16
|
|
|
11
|
-
export const all = (arr: any[], fn = bool) => arr.every(fn)
|
|
12
|
-
|
|
13
|
-
export const any = (arr: any[], fn = bool) => arr.some(fn)
|
|
14
|
-
|
|
15
|
-
export const and = all
|
|
16
|
-
|
|
17
|
-
export const or = any
|
|
18
|
-
|
|
19
17
|
export function comp(x, y) {
|
|
20
18
|
const op = call(x, 'compare', y)
|
|
21
|
-
if (isNumber(op)) return op
|
|
19
|
+
if (isNumber(op)) return op as 0 | 1 | -1
|
|
22
20
|
if (eq(x, y)) return 0
|
|
23
21
|
if (le(x, y)) return -1
|
|
24
22
|
if (ge(x, y)) return 1
|
|
@@ -31,24 +29,6 @@ export function eq(x, y) {
|
|
|
31
29
|
return x === y
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
export function shallowEqual(x, y) {
|
|
35
|
-
if (!x || !y) return x === y
|
|
36
|
-
if (Array.isArray(x)) {
|
|
37
|
-
for (let i = 0; i < x.length; i++) {
|
|
38
|
-
if (!eq(x[i], y[i])) return false
|
|
39
|
-
}
|
|
40
|
-
return true
|
|
41
|
-
}
|
|
42
|
-
if (isObject(x)) {
|
|
43
|
-
if (eq(x, y)) return true
|
|
44
|
-
for (const key in x) {
|
|
45
|
-
if (!eq(x[key], y[key])) return false
|
|
46
|
-
}
|
|
47
|
-
return true
|
|
48
|
-
}
|
|
49
|
-
return eq(x, y)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
32
|
export function lt(x, y) {
|
|
53
33
|
const op = call(x, 'lt', y)
|
|
54
34
|
if (op != null) return op
|
|
@@ -124,3 +104,72 @@ export function pow(x, y) {
|
|
|
124
104
|
if (op != null) return op
|
|
125
105
|
return x ** y
|
|
126
106
|
}
|
|
107
|
+
|
|
108
|
+
export function shallowEqual(obj, other) {
|
|
109
|
+
if (obj === other) return true
|
|
110
|
+
|
|
111
|
+
if (!obj || !other) return false
|
|
112
|
+
|
|
113
|
+
if (Array.isArray(obj)) {
|
|
114
|
+
// compare the arrays
|
|
115
|
+
if (obj.length !== other.length) return false
|
|
116
|
+
|
|
117
|
+
for (let i = 0; i < obj.length; i++) {
|
|
118
|
+
if (obj[i] !== other[i]) {
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return true
|
|
123
|
+
} else if (isObject(obj)) {
|
|
124
|
+
// compare the object keys
|
|
125
|
+
const objKeys = Object.keys(obj)
|
|
126
|
+
const otherKeys = Object.keys(other)
|
|
127
|
+
|
|
128
|
+
if (objKeys.length !== otherKeys.length) return false
|
|
129
|
+
|
|
130
|
+
for (const key of objKeys) {
|
|
131
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
|
|
132
|
+
return false
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function deepEqual(obj, other, checker = eq) {
|
|
142
|
+
if (checker(obj, other)) return true
|
|
143
|
+
|
|
144
|
+
if (!obj || !other) return false
|
|
145
|
+
|
|
146
|
+
if (Array.isArray(obj)) {
|
|
147
|
+
// compare the arrays
|
|
148
|
+
if (obj.length !== other.length) return false
|
|
149
|
+
|
|
150
|
+
for (let i = 0; i < obj.length; i++) {
|
|
151
|
+
if (!deepEqual(obj[i], other[i], checker)) {
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return true
|
|
156
|
+
} else if (isObject(obj)) {
|
|
157
|
+
// compare the object keys
|
|
158
|
+
const objKeys = Object.keys(obj)
|
|
159
|
+
const otherKeys = Object.keys(other)
|
|
160
|
+
|
|
161
|
+
if (objKeys.length !== otherKeys.length) return false
|
|
162
|
+
|
|
163
|
+
for (const key of objKeys) {
|
|
164
|
+
if (
|
|
165
|
+
!Object.prototype.hasOwnProperty.call(other, key) ||
|
|
166
|
+
!deepEqual(obj[key], other[key], checker)
|
|
167
|
+
) {
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return true
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module implements pseudo-random generators for various distributions.
|
|
3
|
+
*
|
|
4
|
+
* @module Random
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Returns a random float between a [min, max) value (max exclusive).
|
|
8
|
+
* @param {number} min
|
|
9
|
+
* @param {number} max
|
|
10
|
+
* @returns {number}
|
|
11
|
+
*/
|
|
12
|
+
export declare const random: (min?: number, max?: number) => number;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a random integer between a [min, max] value (inclusive).
|
|
15
|
+
* @param {number} min
|
|
16
|
+
* @param {number} max
|
|
17
|
+
* @returns {number}
|
|
18
|
+
*/
|
|
19
|
+
export declare const randomInt: (min?: number, max?: number) => number;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the item at some random index in a sequence.
|
|
22
|
+
* @returns {T}
|
|
23
|
+
* @template T
|
|
24
|
+
*/
|
|
25
|
+
export declare const choice: <T>(arr: T[]) => T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Returns a random sample of size k from a list of items.
|
|
28
|
+
* @param {Array} arr
|
|
29
|
+
* @param {number} size
|
|
30
|
+
* @returns A new array with `size` random elements from `arr`.
|
|
31
|
+
* @template T
|
|
32
|
+
*/
|
|
33
|
+
export declare function sample<T>(arr: T[], size: number): T[];
|
|
34
|
+
/**
|
|
35
|
+
* Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
|
|
36
|
+
*
|
|
37
|
+
* @param {Array} arr
|
|
38
|
+
* @param {number} size
|
|
39
|
+
* @returns The shuffled array.
|
|
40
|
+
* @template T
|
|
41
|
+
*/
|
|
42
|
+
export declare function shuffle<T>(arr: T[], size?: number): T[];
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module implements pseudo-random generators for various distributions.
|
|
3
|
+
*
|
|
4
|
+
* @module Random
|
|
5
|
+
*/
|
|
6
|
+
import { isObject } from '../globals/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Returns a random float between a [min, max) value (max exclusive).
|
|
9
|
+
* @param {number} min
|
|
10
|
+
* @param {number} max
|
|
11
|
+
* @returns {number}
|
|
12
|
+
*/
|
|
13
|
+
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a random integer between a [min, max] value (inclusive).
|
|
16
|
+
* @param {number} min
|
|
17
|
+
* @param {number} max
|
|
18
|
+
* @returns {number}
|
|
19
|
+
*/
|
|
20
|
+
export const randomInt = (min = 0, max = 1) => Math.round(random(min, max));
|
|
21
|
+
/**
|
|
22
|
+
* Returns the item at some random index in a sequence.
|
|
23
|
+
* @returns {T}
|
|
24
|
+
* @template T
|
|
25
|
+
*/
|
|
26
|
+
export const choice = (arr) => {
|
|
27
|
+
if (!arr || !arr.length)
|
|
28
|
+
return;
|
|
29
|
+
return arr[randomInt(0, arr.length - 1)];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Returns a random sample of size k from a list of items.
|
|
33
|
+
* @param {Array} arr
|
|
34
|
+
* @param {number} size
|
|
35
|
+
* @returns A new array with `size` random elements from `arr`.
|
|
36
|
+
* @template T
|
|
37
|
+
*/
|
|
38
|
+
export function sample(arr, size) {
|
|
39
|
+
if (!arr || !arr.length || arr.length < size)
|
|
40
|
+
return [];
|
|
41
|
+
return shuffle(arr.slice()).slice(-size);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
|
|
45
|
+
*
|
|
46
|
+
* @param {Array} arr
|
|
47
|
+
* @param {number} size
|
|
48
|
+
* @returns The shuffled array.
|
|
49
|
+
* @template T
|
|
50
|
+
*/
|
|
51
|
+
export function shuffle(arr, size) {
|
|
52
|
+
if (!arr || !arr.length)
|
|
53
|
+
return arr;
|
|
54
|
+
if (isObject(arr))
|
|
55
|
+
arr = Object.values(arr);
|
|
56
|
+
if (size == null)
|
|
57
|
+
size = arr.length - 1;
|
|
58
|
+
for (let i = 0; i <= size; i++) {
|
|
59
|
+
const j = randomInt(i, size);
|
|
60
|
+
const temp = arr[j];
|
|
61
|
+
arr[j] = arr[i];
|
|
62
|
+
arr[i] = temp;
|
|
63
|
+
}
|
|
64
|
+
return arr;
|
|
65
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module implements pseudo-random generators for various distributions.
|
|
3
|
+
*
|
|
4
|
+
* @module Random
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isObject } from '../globals/index.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns a random float between a [min, max) value (max exclusive).
|
|
11
|
+
* @param {number} min
|
|
12
|
+
* @param {number} max
|
|
13
|
+
* @returns {number}
|
|
14
|
+
*/
|
|
15
|
+
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns a random integer between a [min, max] value (inclusive).
|
|
19
|
+
* @param {number} min
|
|
20
|
+
* @param {number} max
|
|
21
|
+
* @returns {number}
|
|
22
|
+
*/
|
|
23
|
+
export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the item at some random index in a sequence.
|
|
27
|
+
* @returns {T}
|
|
28
|
+
* @template T
|
|
29
|
+
*/
|
|
30
|
+
export const choice = <T>(arr: T[]) => {
|
|
31
|
+
if (!arr || !arr.length) return
|
|
32
|
+
return arr[randomInt(0, arr.length - 1)]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a random sample of size k from a list of items.
|
|
37
|
+
* @param {Array} arr
|
|
38
|
+
* @param {number} size
|
|
39
|
+
* @returns A new array with `size` random elements from `arr`.
|
|
40
|
+
* @template T
|
|
41
|
+
*/
|
|
42
|
+
export function sample<T>(arr: T[], size: number) {
|
|
43
|
+
if (!arr || !arr.length || arr.length < size) return []
|
|
44
|
+
return shuffle(arr.slice()).slice(-size)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
|
|
49
|
+
*
|
|
50
|
+
* @param {Array} arr
|
|
51
|
+
* @param {number} size
|
|
52
|
+
* @returns The shuffled array.
|
|
53
|
+
* @template T
|
|
54
|
+
*/
|
|
55
|
+
export function shuffle<T>(arr: T[], size?: number) {
|
|
56
|
+
if (!arr || !arr.length) return arr
|
|
57
|
+
if (isObject(arr)) arr = Object.values(arr)
|
|
58
|
+
if (size == null) size = arr.length - 1
|
|
59
|
+
for (let i = 0; i <= size; i++) {
|
|
60
|
+
const j = randomInt(i, size)
|
|
61
|
+
const temp = arr[j]
|
|
62
|
+
arr[j] = arr[i]
|
|
63
|
+
arr[i] = temp
|
|
64
|
+
}
|
|
65
|
+
return arr
|
|
66
|
+
}
|
|
@@ -1,37 +1,50 @@
|
|
|
1
1
|
import assert from 'assert'
|
|
2
2
|
import {
|
|
3
|
+
clone,
|
|
4
|
+
compact,
|
|
5
|
+
count,
|
|
6
|
+
difference,
|
|
3
7
|
filter,
|
|
4
8
|
find,
|
|
5
|
-
forEach,
|
|
6
9
|
findRight,
|
|
7
10
|
flatten,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
pick,
|
|
11
|
+
forEach,
|
|
12
|
+
groupBy,
|
|
13
|
+
intersection,
|
|
14
|
+
merge,
|
|
13
15
|
omit,
|
|
14
|
-
|
|
16
|
+
pick,
|
|
17
|
+
remove,
|
|
15
18
|
sortedUniq,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
union
|
|
20
|
-
} from '../src/collections.js'
|
|
19
|
+
union,
|
|
20
|
+
uniq
|
|
21
|
+
} from '../src/collections/index.js'
|
|
21
22
|
|
|
22
23
|
describe('collections', () => {
|
|
23
|
-
it('
|
|
24
|
-
|
|
25
|
-
assert(
|
|
24
|
+
it('compact', () => {
|
|
25
|
+
let filtered = compact([0, 1, false, 2, '', 3])
|
|
26
|
+
assert.deepEqual(filtered, [1, 2, 3])
|
|
27
|
+
assert.deepEqual(compact({ a: 1, b: undefined, c: false, d: [] }), { a: 1 })
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('count', () => {
|
|
31
|
+
let counters = count([0, 1, false, 2, '', 3], (x) => !!x)
|
|
32
|
+
assert(counters['true'] === 3)
|
|
33
|
+
assert(counters['false'] === 3)
|
|
26
34
|
})
|
|
27
35
|
|
|
28
|
-
it('clone', () => {
|
|
36
|
+
it('clone array', () => {
|
|
29
37
|
let arr = [4, 7, 1, 9]
|
|
30
38
|
assert.deepEqual(clone(arr), arr)
|
|
31
39
|
let target = { user: 'fred', age: 40, active: false }
|
|
32
40
|
assert.deepEqual(clone(target), target)
|
|
33
41
|
})
|
|
34
42
|
|
|
43
|
+
it('clone object', () => {
|
|
44
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
45
|
+
assert.deepEqual(clone(target), target)
|
|
46
|
+
})
|
|
47
|
+
|
|
35
48
|
it('difference', () => {
|
|
36
49
|
let a = [1, 2, 5]
|
|
37
50
|
let b = [3, 4, 5]
|
|
@@ -56,11 +69,27 @@ describe('collections', () => {
|
|
|
56
69
|
{ user: 'barney', age: 36, active: true },
|
|
57
70
|
{ user: 'fred', age: 40, active: false }
|
|
58
71
|
]
|
|
59
|
-
assert.deepEqual(filter(users, { age: 36 }), [users[0]])
|
|
60
72
|
assert.deepEqual(
|
|
61
73
|
filter(users, (o) => o.active),
|
|
62
74
|
[users[0]]
|
|
63
75
|
)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('filter matches', () => {
|
|
79
|
+
assert(filter(null) == undefined)
|
|
80
|
+
let users = [
|
|
81
|
+
{ user: 'barney', age: 36, active: true },
|
|
82
|
+
{ user: 'fred', age: 40, active: false }
|
|
83
|
+
]
|
|
84
|
+
assert.deepEqual(filter(users, { age: 36 }), [users[0]])
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('filter property', () => {
|
|
88
|
+
assert(filter(null) == undefined)
|
|
89
|
+
let users = [
|
|
90
|
+
{ user: 'barney', age: 36, active: true },
|
|
91
|
+
{ user: 'fred', age: 40, active: false }
|
|
92
|
+
]
|
|
64
93
|
assert.deepEqual(filter(users, 'active'), [users[0]])
|
|
65
94
|
})
|
|
66
95
|
|
|
@@ -69,8 +98,22 @@ describe('collections', () => {
|
|
|
69
98
|
{ user: 'barney', age: 36, active: true },
|
|
70
99
|
{ user: 'fred', age: 40, active: false }
|
|
71
100
|
]
|
|
72
|
-
assert(find(users, { age: 36 }) === users[0])
|
|
73
101
|
assert(find(users, (o) => o.active) === users[0])
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('find matches', () => {
|
|
105
|
+
let users = [
|
|
106
|
+
{ user: 'barney', age: 36, active: true },
|
|
107
|
+
{ user: 'fred', age: 40, active: false }
|
|
108
|
+
]
|
|
109
|
+
assert(find(users, { age: 36 }) === users[0])
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('find property', () => {
|
|
113
|
+
let users = [
|
|
114
|
+
{ user: 'barney', age: 36, active: true },
|
|
115
|
+
{ user: 'fred', age: 40, active: false }
|
|
116
|
+
]
|
|
74
117
|
assert(find(users, 'active') === users[0])
|
|
75
118
|
})
|
|
76
119
|
|
|
@@ -81,7 +124,23 @@ describe('collections', () => {
|
|
|
81
124
|
{ user: 'barney', age: 36, active: true }
|
|
82
125
|
]
|
|
83
126
|
assert(findRight(users, { age: 36 }) === users[2])
|
|
84
|
-
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('findRight matches', () => {
|
|
130
|
+
let users = [
|
|
131
|
+
{ user: 'barney', age: 36, active: true },
|
|
132
|
+
{ user: 'fred', age: 40, active: false },
|
|
133
|
+
{ user: 'barney', age: 36, active: true }
|
|
134
|
+
]
|
|
135
|
+
assert(findRight(users, { age: 36 }) === users[2])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('findRight property', () => {
|
|
139
|
+
let users = [
|
|
140
|
+
{ user: 'barney', age: 36, active: true },
|
|
141
|
+
{ user: 'fred', age: 40, active: false },
|
|
142
|
+
{ user: 'barney', age: 36, active: true }
|
|
143
|
+
]
|
|
85
144
|
assert(findRight(users, 'active') === users[2])
|
|
86
145
|
})
|
|
87
146
|
|
|
@@ -90,7 +149,7 @@ describe('collections', () => {
|
|
|
90
149
|
{ user: 'barney', age: 36, active: true },
|
|
91
150
|
{ user: 'fred', age: 40, active: false }
|
|
92
151
|
]
|
|
93
|
-
let result = [] as any
|
|
152
|
+
let result = [] as any
|
|
94
153
|
forEach(users, (value, key) => result.push([key, value]))
|
|
95
154
|
assert.deepEqual(
|
|
96
155
|
result,
|
|
@@ -100,19 +159,23 @@ describe('collections', () => {
|
|
|
100
159
|
|
|
101
160
|
it('forEach object key', () => {
|
|
102
161
|
let target = { user: 'fred', age: 40, active: false }
|
|
103
|
-
let objs = []
|
|
162
|
+
let objs = []
|
|
163
|
+
// @ts-expect-error
|
|
104
164
|
forEach(target, (value, key) => objs.push([key, value]))
|
|
105
165
|
assert.deepEqual(objs, Object.entries(target))
|
|
106
166
|
})
|
|
107
167
|
|
|
108
|
-
it('
|
|
168
|
+
it('flatten Array', () => {
|
|
109
169
|
let data = flatten([1, 2, [3, 4], 5])
|
|
110
170
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
111
|
-
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('flatten Array deep', () => {
|
|
174
|
+
let data = flatten([1, 2, [3, [4]], [5]], true)
|
|
112
175
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
113
176
|
})
|
|
114
177
|
|
|
115
|
-
it('
|
|
178
|
+
it('flatten Object', () => {
|
|
116
179
|
const data = {
|
|
117
180
|
dates: {
|
|
118
181
|
expiry_date: '30 sep 2018',
|
|
@@ -129,7 +192,7 @@ describe('collections', () => {
|
|
|
129
192
|
value: 1500
|
|
130
193
|
}
|
|
131
194
|
}
|
|
132
|
-
assert.deepEqual(
|
|
195
|
+
assert.deepEqual(flatten(data), {
|
|
133
196
|
'dates.expiry_date': '30 sep 2018',
|
|
134
197
|
'dates.available': '30 sep 2017',
|
|
135
198
|
'dates.min_contract_period[0].id': 1,
|
|
@@ -139,10 +202,8 @@ describe('collections', () => {
|
|
|
139
202
|
})
|
|
140
203
|
})
|
|
141
204
|
|
|
142
|
-
it('
|
|
143
|
-
|
|
144
|
-
let getEntries = (value, key) => [key, value]
|
|
145
|
-
assert.deepEqual(map(target, getEntries), Object.entries(target))
|
|
205
|
+
it('groupBy', () => {
|
|
206
|
+
assert.deepEqual(groupBy([6.1, 4.2, 6.3], Math.floor), { '4': [4.2], '6': [6.1, 6.3] })
|
|
146
207
|
})
|
|
147
208
|
|
|
148
209
|
it('merge', () => {
|
|
@@ -174,15 +235,39 @@ describe('collections', () => {
|
|
|
174
235
|
)
|
|
175
236
|
})
|
|
176
237
|
|
|
238
|
+
it('pickBy', () => {
|
|
239
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
240
|
+
assert.deepEqual(
|
|
241
|
+
pick(target, (x) => x >= 40),
|
|
242
|
+
{ age: 40 }
|
|
243
|
+
)
|
|
244
|
+
})
|
|
245
|
+
|
|
177
246
|
it('omit', () => {
|
|
178
247
|
let target = { user: 'fred', age: 40, active: false }
|
|
179
248
|
assert.deepEqual(omit(target, ['age', 'active']), { user: 'fred' })
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('omitBy', () => {
|
|
252
|
+
let target = { user: 'fred', age: 40, active: false }
|
|
180
253
|
assert.deepEqual(
|
|
181
254
|
omit(target, (x) => !x),
|
|
182
255
|
{ user: 'fred', age: 40 }
|
|
183
256
|
)
|
|
184
257
|
})
|
|
185
258
|
|
|
259
|
+
it('remove', () => {
|
|
260
|
+
assert.deepEqual(remove([1, 2, 3, 4], 1), [2, 3, 4])
|
|
261
|
+
assert.deepEqual(remove([1, 2, 3, 4], [3, 4]), [1, 2])
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it('removeBy', () => {
|
|
265
|
+
assert.deepEqual(
|
|
266
|
+
remove([1, 2, 3, 4], (x) => x % 2 === 0),
|
|
267
|
+
[1, 3]
|
|
268
|
+
)
|
|
269
|
+
})
|
|
270
|
+
|
|
186
271
|
it('sortedUniq', () => {
|
|
187
272
|
let arr = [4, 7, 1, 9, 1, 7, 4]
|
|
188
273
|
let sort = sortedUniq(arr)
|
|
@@ -193,7 +278,11 @@ describe('collections', () => {
|
|
|
193
278
|
let arr = [4, 7, 1, 1, 9, 4, 7]
|
|
194
279
|
let distinct = uniq(arr)
|
|
195
280
|
assert.deepEqual(distinct, [4, 7, 1, 9])
|
|
196
|
-
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it('uniqBy', () => {
|
|
284
|
+
let arr = [4, 7, 1, 1, 9, 4, 7]
|
|
285
|
+
let distinct = uniq(arr, (x) => x * 2)
|
|
197
286
|
assert.deepEqual(distinct, [8, 14, 2, 18])
|
|
198
287
|
})
|
|
199
288
|
})
|