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
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
import { call, isNumber, isObject } from '../globals/index.js';
|
|
7
|
+
export const id = (x) => x;
|
|
8
|
+
export const bool = (x) => !!x;
|
|
9
|
+
export const not = (x) => !x;
|
|
10
|
+
export const isinstance = (x, y) => x instanceof y;
|
|
11
|
+
export function comp(x, y) {
|
|
12
|
+
const op = call(x, 'compare', y);
|
|
13
|
+
if (isNumber(op))
|
|
14
|
+
return op;
|
|
15
|
+
if (eq(x, y))
|
|
16
|
+
return 0;
|
|
17
|
+
if (le(x, y))
|
|
18
|
+
return -1;
|
|
19
|
+
if (ge(x, y))
|
|
20
|
+
return 1;
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
export function eq(x, y) {
|
|
24
|
+
const op = call(x, 'eq', y);
|
|
25
|
+
if (op != null)
|
|
26
|
+
return op;
|
|
27
|
+
return x === y;
|
|
28
|
+
}
|
|
29
|
+
export function lt(x, y) {
|
|
30
|
+
const op = call(x, 'lt', y);
|
|
31
|
+
if (op != null)
|
|
32
|
+
return op;
|
|
33
|
+
return x < y;
|
|
34
|
+
}
|
|
35
|
+
export function le(x, y) {
|
|
36
|
+
let op = call(x, 'le', y);
|
|
37
|
+
if (op != null)
|
|
38
|
+
return op;
|
|
39
|
+
op = call(x, 'eq', y);
|
|
40
|
+
if (op === true)
|
|
41
|
+
return op;
|
|
42
|
+
op = call(x, 'lt', y);
|
|
43
|
+
if (op != null)
|
|
44
|
+
return op;
|
|
45
|
+
return x <= y;
|
|
46
|
+
}
|
|
47
|
+
export function gt(x, y) {
|
|
48
|
+
const op = call(x, 'gt', y);
|
|
49
|
+
if (op != null)
|
|
50
|
+
return op;
|
|
51
|
+
return x > y;
|
|
52
|
+
}
|
|
53
|
+
export function ge(x, y) {
|
|
54
|
+
let op = call(x, 'ge', y);
|
|
55
|
+
if (op != null)
|
|
56
|
+
return op;
|
|
57
|
+
op = call(x, 'eq', y);
|
|
58
|
+
if (op === true)
|
|
59
|
+
return op;
|
|
60
|
+
op = call(x, 'gt', y);
|
|
61
|
+
if (op != null)
|
|
62
|
+
return op;
|
|
63
|
+
return x >= y;
|
|
64
|
+
}
|
|
65
|
+
export function add(x, y) {
|
|
66
|
+
const op = call(x, 'add', y);
|
|
67
|
+
if (op != null)
|
|
68
|
+
return op;
|
|
69
|
+
return x + y;
|
|
70
|
+
}
|
|
71
|
+
export function sub(x, y) {
|
|
72
|
+
const op = call(x, 'sub', y);
|
|
73
|
+
if (op != null)
|
|
74
|
+
return op;
|
|
75
|
+
return x - y;
|
|
76
|
+
}
|
|
77
|
+
export function mult(x, y) {
|
|
78
|
+
const op = call(x, 'mult', y);
|
|
79
|
+
if (op != null)
|
|
80
|
+
return op;
|
|
81
|
+
return x * y;
|
|
82
|
+
}
|
|
83
|
+
export function div(x, y) {
|
|
84
|
+
const op = call(x, 'div', y);
|
|
85
|
+
if (op != null)
|
|
86
|
+
return op;
|
|
87
|
+
return x / y;
|
|
88
|
+
}
|
|
89
|
+
export function mod(x, y) {
|
|
90
|
+
const op = call(x, 'mod', y);
|
|
91
|
+
if (op != null)
|
|
92
|
+
return op;
|
|
93
|
+
return x % y;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
|
|
97
|
+
* it will return `x.pow(y)`.
|
|
98
|
+
*
|
|
99
|
+
* @param {*} x
|
|
100
|
+
* @param {*} y
|
|
101
|
+
* @return `x` raised to the power `y`
|
|
102
|
+
*/
|
|
103
|
+
export function pow(x, y) {
|
|
104
|
+
const op = call(x, 'pow', y);
|
|
105
|
+
if (op != null)
|
|
106
|
+
return op;
|
|
107
|
+
return x ** y;
|
|
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,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
import { call, isNumber, isObject } from '../globals/index.js'
|
|
6
8
|
|
|
7
9
|
export const id = (x) => x
|
|
8
10
|
|
|
@@ -12,17 +14,9 @@ export const not = (x) => !x
|
|
|
12
14
|
|
|
13
15
|
export const isinstance = (x, y) => x instanceof y
|
|
14
16
|
|
|
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
17
|
export function comp(x, y) {
|
|
24
18
|
const op = call(x, 'compare', y)
|
|
25
|
-
if (isNumber(op)) return op
|
|
19
|
+
if (isNumber(op)) return op as 0 | 1 | -1
|
|
26
20
|
if (eq(x, y)) return 0
|
|
27
21
|
if (le(x, y)) return -1
|
|
28
22
|
if (ge(x, y)) return 1
|
|
@@ -35,24 +29,6 @@ export function eq(x, y) {
|
|
|
35
29
|
return x === y
|
|
36
30
|
}
|
|
37
31
|
|
|
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
32
|
export function lt(x, y) {
|
|
57
33
|
const op = call(x, 'lt', y)
|
|
58
34
|
if (op != null) return op
|
|
@@ -128,3 +104,72 @@ export function pow(x, y) {
|
|
|
128
104
|
if (op != null) return op
|
|
129
105
|
return x ** y
|
|
130
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,28 +1,36 @@
|
|
|
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
36
|
it('clone array', () => {
|
|
@@ -141,8 +149,7 @@ describe('collections', () => {
|
|
|
141
149
|
{ user: 'barney', age: 36, active: true },
|
|
142
150
|
{ user: 'fred', age: 40, active: false }
|
|
143
151
|
]
|
|
144
|
-
let result = []
|
|
145
|
-
// @ts-expect-error
|
|
152
|
+
let result = [] as any
|
|
146
153
|
forEach(users, (value, key) => result.push([key, value]))
|
|
147
154
|
assert.deepEqual(
|
|
148
155
|
result,
|
|
@@ -158,17 +165,17 @@ describe('collections', () => {
|
|
|
158
165
|
assert.deepEqual(objs, Object.entries(target))
|
|
159
166
|
})
|
|
160
167
|
|
|
161
|
-
it('flatten', () => {
|
|
168
|
+
it('flatten Array', () => {
|
|
162
169
|
let data = flatten([1, 2, [3, 4], 5])
|
|
163
170
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
164
171
|
})
|
|
165
172
|
|
|
166
|
-
it('
|
|
173
|
+
it('flatten Array deep', () => {
|
|
167
174
|
let data = flatten([1, 2, [3, [4]], [5]], true)
|
|
168
175
|
assert.deepEqual(data, [1, 2, 3, 4, 5])
|
|
169
176
|
})
|
|
170
177
|
|
|
171
|
-
it('
|
|
178
|
+
it('flatten Object', () => {
|
|
172
179
|
const data = {
|
|
173
180
|
dates: {
|
|
174
181
|
expiry_date: '30 sep 2018',
|
|
@@ -185,7 +192,7 @@ describe('collections', () => {
|
|
|
185
192
|
value: 1500
|
|
186
193
|
}
|
|
187
194
|
}
|
|
188
|
-
assert.deepEqual(
|
|
195
|
+
assert.deepEqual(flatten(data), {
|
|
189
196
|
'dates.expiry_date': '30 sep 2018',
|
|
190
197
|
'dates.available': '30 sep 2017',
|
|
191
198
|
'dates.min_contract_period[0].id': 1,
|
|
@@ -195,10 +202,8 @@ describe('collections', () => {
|
|
|
195
202
|
})
|
|
196
203
|
})
|
|
197
204
|
|
|
198
|
-
it('
|
|
199
|
-
|
|
200
|
-
let getEntries = (value, key) => [key, value]
|
|
201
|
-
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] })
|
|
202
207
|
})
|
|
203
208
|
|
|
204
209
|
it('merge', () => {
|
|
@@ -251,6 +256,18 @@ describe('collections', () => {
|
|
|
251
256
|
)
|
|
252
257
|
})
|
|
253
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
|
+
|
|
254
271
|
it('sortedUniq', () => {
|
|
255
272
|
let arr = [4, 7, 1, 9, 1, 7, 4]
|
|
256
273
|
let sort = sortedUniq(arr)
|