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.
- package/.firebaserc +5 -0
- package/README.md +9 -4
- package/firebase.json +10 -0
- package/package.json +9 -9
- package/src/collections/index.d.ts +672 -0
- package/src/collections/index.js +1091 -0
- package/src/collections/index.ts +1186 -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/index.d.ts +107 -0
- package/src/functools/index.js +219 -0
- package/src/functools/index.ts +255 -0
- package/src/globals/index.d.ts +286 -0
- package/src/globals/index.js +388 -0
- package/src/globals/index.ts +451 -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 +225 -0
- package/src/itertools/index.js +449 -0
- package/src/itertools/index.ts +471 -0
- package/src/math/index.d.ts +97 -0
- package/src/math/index.js +92 -0
- package/src/math/index.ts +152 -0
- package/src/{operators.d.ts → operators/index.d.ts} +11 -9
- package/src/operators/index.js +171 -0
- package/src/operators/index.ts +175 -0
- 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 +56 -60
- package/tests/itertools.spec.ts +176 -4
- package/tests/math.spec.ts +25 -0
- 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.d.ts +0 -40
- package/src/functools.js +0 -78
- package/src/functools.ts +0 -93
- 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/operators.ts +0 -130
- 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
|
@@ -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)
|
package/tests/globals.spec.ts
CHANGED
|
@@ -1,45 +1,42 @@
|
|
|
1
1
|
import assert from 'assert'
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
bin,
|
|
4
|
+
call,
|
|
5
|
+
chr,
|
|
5
6
|
CustomError,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
False,
|
|
8
|
+
get,
|
|
9
|
+
hash,
|
|
10
|
+
hashCode,
|
|
11
|
+
HASH_KEY,
|
|
12
|
+
hex,
|
|
11
13
|
isArray,
|
|
12
14
|
isArrayLike,
|
|
13
|
-
|
|
14
|
-
isNaN,
|
|
15
|
-
isNull,
|
|
15
|
+
isAsync,
|
|
16
16
|
isBigint,
|
|
17
|
+
isBool,
|
|
18
|
+
isEmpty,
|
|
17
19
|
isFunc,
|
|
20
|
+
isNaN,
|
|
21
|
+
isNull,
|
|
22
|
+
isNumber,
|
|
23
|
+
isObject,
|
|
18
24
|
isPromise,
|
|
25
|
+
isString,
|
|
19
26
|
isSymbol,
|
|
20
|
-
isAsync,
|
|
21
|
-
uniqueId,
|
|
22
|
-
call,
|
|
23
|
-
chr,
|
|
24
|
-
ord,
|
|
25
27
|
keys,
|
|
26
|
-
values,
|
|
27
|
-
round,
|
|
28
|
-
enumerate,
|
|
29
|
-
isEmpty,
|
|
30
|
-
isGenerator,
|
|
31
|
-
hash,
|
|
32
|
-
hashCode,
|
|
33
|
-
HASH_KEY,
|
|
34
|
-
bin,
|
|
35
|
-
hex,
|
|
36
|
-
oct,
|
|
37
28
|
list,
|
|
38
29
|
next,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
oct,
|
|
31
|
+
ord,
|
|
32
|
+
str,
|
|
33
|
+
True,
|
|
34
|
+
type,
|
|
35
|
+
uniqueId,
|
|
36
|
+
values,
|
|
37
|
+
_
|
|
38
|
+
} from '../src/globals/index.js'
|
|
39
|
+
import { enumerate } from '../src/itertools/index.js'
|
|
43
40
|
|
|
44
41
|
describe('globals', () => {
|
|
45
42
|
it('CustomError', () => {
|
|
@@ -70,8 +67,25 @@ describe('globals', () => {
|
|
|
70
67
|
assert(type([]) === 'array')
|
|
71
68
|
assert(type({}) === 'object')
|
|
72
69
|
assert(type(type) === 'function')
|
|
70
|
+
assert(type(Error()) === 'error')
|
|
71
|
+
assert(type(new CustomError()) === 'error')
|
|
73
72
|
assert(type(Symbol()) === 'symbol')
|
|
74
|
-
assert(type(new
|
|
73
|
+
assert(type(new Map()) === 'Map')
|
|
74
|
+
assert(type(new WeakMap()) === 'WeakMap')
|
|
75
|
+
assert(type(new Set()) === 'Set')
|
|
76
|
+
assert(type(new WeakSet()) === 'WeakSet')
|
|
77
|
+
assert(type(new Promise(_)) === 'Promise')
|
|
78
|
+
assert(type(new Int8Array()) === 'Int8Array')
|
|
79
|
+
assert(type(new Uint8Array()) === 'Uint8Array')
|
|
80
|
+
assert(type(new Uint8ClampedArray()) === 'Uint8ClampedArray')
|
|
81
|
+
assert(type(new Int16Array()) === 'Int16Array')
|
|
82
|
+
assert(type(new Uint16Array()) === 'Uint16Array')
|
|
83
|
+
assert(type(new Int32Array()) === 'Int32Array')
|
|
84
|
+
assert(type(new Uint32Array()) === 'Uint32Array')
|
|
85
|
+
assert(type(new Float32Array()) === 'Float32Array')
|
|
86
|
+
assert(type(new Float64Array()) === 'Float64Array')
|
|
87
|
+
assert(type(new ArrayBuffer(8)) === 'ArrayBuffer')
|
|
88
|
+
assert(type(new DataView(new ArrayBuffer(8))) === 'DataView')
|
|
75
89
|
})
|
|
76
90
|
|
|
77
91
|
it('isBool', () => {
|
|
@@ -101,14 +115,14 @@ describe('globals', () => {
|
|
|
101
115
|
})
|
|
102
116
|
|
|
103
117
|
it('isFunc', () => {
|
|
104
|
-
assert(isFunc(
|
|
118
|
+
assert(isFunc(_))
|
|
105
119
|
assert(!isFunc([]))
|
|
106
120
|
})
|
|
107
121
|
|
|
108
122
|
it('isAsync', () => {
|
|
109
123
|
assert(isAsync(async function () {}))
|
|
110
124
|
assert(isAsync(async () => {}))
|
|
111
|
-
assert(!isAsync(
|
|
125
|
+
assert(!isAsync(_))
|
|
112
126
|
})
|
|
113
127
|
|
|
114
128
|
it('isNaN', () => {
|
|
@@ -129,7 +143,7 @@ describe('globals', () => {
|
|
|
129
143
|
})
|
|
130
144
|
|
|
131
145
|
it('isPromise', () => {
|
|
132
|
-
assert(isPromise(new Promise(
|
|
146
|
+
assert(isPromise(new Promise(_)))
|
|
133
147
|
})
|
|
134
148
|
|
|
135
149
|
it('isSymbol', () => {
|
|
@@ -193,25 +207,6 @@ describe('globals', () => {
|
|
|
193
207
|
assert.deepEqual(values(obj), Object.values(obj))
|
|
194
208
|
})
|
|
195
209
|
|
|
196
|
-
it('round', () => {
|
|
197
|
-
assert(round(0.5) === 1)
|
|
198
|
-
assert(round(-0.5) === 0)
|
|
199
|
-
assert(round(1.5) === 2)
|
|
200
|
-
assert(round(1.54, 2) === 1.54)
|
|
201
|
-
assert(round(1.545, 3) === 1.545)
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
it('enumerate', () => {
|
|
205
|
-
let iter = enumerate(['foo', 'bar', 'baz'])
|
|
206
|
-
assert(isGenerator(iter))
|
|
207
|
-
let values = [...iter]
|
|
208
|
-
assert.deepEqual(values, [
|
|
209
|
-
[0, 'foo'],
|
|
210
|
-
[1, 'bar'],
|
|
211
|
-
[2, 'baz']
|
|
212
|
-
])
|
|
213
|
-
})
|
|
214
|
-
|
|
215
210
|
it('next', () => {
|
|
216
211
|
const iter = enumerate([1, 2, 3])
|
|
217
212
|
assert.deepEqual(next(iter), [0, 1])
|
|
@@ -251,15 +246,15 @@ describe('globals', () => {
|
|
|
251
246
|
|
|
252
247
|
it('get', () => {
|
|
253
248
|
const arr = [1, 2, 3]
|
|
254
|
-
assert(get(
|
|
255
|
-
assert(get(
|
|
256
|
-
assert(get(
|
|
249
|
+
assert(get('2', arr) === 3)
|
|
250
|
+
assert(get('3', arr, false) === false)
|
|
251
|
+
assert(get('3', arr, false) === false)
|
|
257
252
|
let obj = { foo: { bar: true } }
|
|
258
253
|
let h = hash(obj)
|
|
259
|
-
assert(get(
|
|
260
|
-
assert(get(
|
|
261
|
-
assert(get(
|
|
262
|
-
assert(get(
|
|
254
|
+
assert(get('foo.bar', obj))
|
|
255
|
+
assert(get(['foo', 'bar'], obj))
|
|
256
|
+
assert(get(HASH_KEY, obj) === h)
|
|
257
|
+
assert(get([HASH_KEY], obj) === h)
|
|
263
258
|
})
|
|
264
259
|
|
|
265
260
|
it('isEmpty', () => {
|
|
@@ -269,6 +264,7 @@ describe('globals', () => {
|
|
|
269
264
|
assert(isEmpty(''))
|
|
270
265
|
assert(isEmpty({}))
|
|
271
266
|
assert(isEmpty(0))
|
|
267
|
+
assert(isEmpty(false))
|
|
272
268
|
assert(!isEmpty(1))
|
|
273
269
|
})
|
|
274
270
|
})
|
package/tests/itertools.spec.ts
CHANGED
|
@@ -1,10 +1,175 @@
|
|
|
1
1
|
import assert from 'assert'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
accumulate,
|
|
4
|
+
all,
|
|
5
|
+
any,
|
|
6
|
+
chain,
|
|
7
|
+
contains,
|
|
8
|
+
dropWhile,
|
|
9
|
+
enumerate,
|
|
10
|
+
first,
|
|
11
|
+
flatMap,
|
|
12
|
+
icompact,
|
|
13
|
+
ifilter,
|
|
14
|
+
iflatten,
|
|
15
|
+
imap,
|
|
16
|
+
islice,
|
|
17
|
+
itake,
|
|
18
|
+
partition,
|
|
19
|
+
range,
|
|
20
|
+
reduce,
|
|
21
|
+
reversed,
|
|
22
|
+
sort,
|
|
23
|
+
sorted,
|
|
24
|
+
take,
|
|
25
|
+
takeWhile,
|
|
26
|
+
times,
|
|
27
|
+
zip
|
|
28
|
+
} from '../src/itertools/index.js'
|
|
29
|
+
import { max } from '../src/math/index.js'
|
|
4
30
|
|
|
5
|
-
|
|
31
|
+
const Falsy = (x) => !x
|
|
6
32
|
|
|
7
33
|
describe('operators', () => {
|
|
34
|
+
it('accumulate', () => {
|
|
35
|
+
assert.deepEqual([...accumulate([1, 2, 3, 4])], [1, 3, 6, 10])
|
|
36
|
+
assert.deepEqual(
|
|
37
|
+
[...accumulate([3, 4, 6, 2, 1, 9, 0, 7, 5, 8], (x, y) => max([x, y]))],
|
|
38
|
+
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('all', () => {
|
|
43
|
+
assert(all([1, true, [], 'test', {}]))
|
|
44
|
+
assert(!all([[], {}, '']))
|
|
45
|
+
assert(all<any>([false, 0, '', null, undefined, NaN], Falsy))
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('any', () => {
|
|
49
|
+
assert(any([1, true, [], 'test', {}]))
|
|
50
|
+
assert(!any([0, false, '']))
|
|
51
|
+
assert(any([0, false, ''], Falsy))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('chain', () => {
|
|
55
|
+
let it = chain([1, 3, 5], [2, 4, 6])
|
|
56
|
+
assert.deepEqual([...it], [1, 3, 5, 2, 4, 6])
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('contains', () => {
|
|
60
|
+
assert(contains([4, 7, 1, 9], 1))
|
|
61
|
+
assert(!contains([4, 7, 1, 9], NaN))
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('dropWhile', () => {
|
|
65
|
+
let it = dropWhile([false, 0, '', true, '', []], Falsy)
|
|
66
|
+
assert.deepEqual([...it], [true, '', []])
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('enumerate', () => {
|
|
70
|
+
let iter = enumerate(['foo', 'bar', 'baz'])
|
|
71
|
+
let values = [...iter]
|
|
72
|
+
assert.deepEqual(values, [
|
|
73
|
+
[0, 'foo'],
|
|
74
|
+
[1, 'bar'],
|
|
75
|
+
[2, 'baz']
|
|
76
|
+
])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('first', () => {
|
|
80
|
+
assert(first(['foo', 'bar']) === 'foo')
|
|
81
|
+
assert(first([0, 1]) === 0)
|
|
82
|
+
assert(first([1, 1]) === 1)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('flatMap', () => {
|
|
86
|
+
assert.deepEqual(
|
|
87
|
+
[
|
|
88
|
+
...flatMap([
|
|
89
|
+
[1, 2],
|
|
90
|
+
[3, 4]
|
|
91
|
+
])
|
|
92
|
+
],
|
|
93
|
+
[1, 2, 3, 4]
|
|
94
|
+
)
|
|
95
|
+
assert.deepEqual(
|
|
96
|
+
[
|
|
97
|
+
...flatMap(
|
|
98
|
+
[
|
|
99
|
+
[1, 2],
|
|
100
|
+
[3, 4]
|
|
101
|
+
],
|
|
102
|
+
([x, y]) => [x * 2, y * 2]
|
|
103
|
+
)
|
|
104
|
+
],
|
|
105
|
+
[2, 4, 6, 8]
|
|
106
|
+
)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('icompact', () => {
|
|
110
|
+
let it = icompact([false, 0, '', true, '', []])
|
|
111
|
+
assert.deepEqual([...it], [true])
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('ifilter', () => {
|
|
115
|
+
let users = [
|
|
116
|
+
{ user: 'barney', age: 36, active: true },
|
|
117
|
+
{ user: 'fred', age: 40, active: false }
|
|
118
|
+
]
|
|
119
|
+
let it = ifilter(users, (x) => !x.active)
|
|
120
|
+
assert.deepEqual([...it], [users[1]])
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('iflatten', () => {
|
|
124
|
+
let it = iflatten([
|
|
125
|
+
[0, 1],
|
|
126
|
+
[2, 3]
|
|
127
|
+
])
|
|
128
|
+
assert.deepEqual([...it], [0, 1, 2, 3])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('imap', () => {
|
|
132
|
+
let it = imap([0, 1, 2, 3], (x) => 2 ** x)
|
|
133
|
+
assert.deepEqual([...it], [1, 2, 4, 8])
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('islice', () => {
|
|
137
|
+
assert.deepEqual([...islice([1, 2, 3], 1)], [2, 3])
|
|
138
|
+
assert.deepEqual([...islice([1, 2, 3, 4], 2)], [3, 4])
|
|
139
|
+
assert.deepEqual([...islice([1, 2, 3, 4], 1, 3)], [2, 3])
|
|
140
|
+
assert.deepEqual([...islice([1, 2, 3, 4, 5], 0, 4)], [1, 2, 3, 4])
|
|
141
|
+
assert.deepEqual([...islice([1, 2, 3, 4, 5], 0, 4, 2)], [1, 3])
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('itake', () => {
|
|
145
|
+
let it = itake(3, [false, 0, '', true, '', []])
|
|
146
|
+
assert.deepEqual([...it], [false, 0, ''])
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
it('take', () => {
|
|
150
|
+
let it = take(3, [false, 0, '', true, '', []])
|
|
151
|
+
assert.deepEqual(it, [false, 0, ''])
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('partition', () => {
|
|
155
|
+
let [A, B] = partition([false, 0, '', true, '', []], Falsy)
|
|
156
|
+
assert.deepEqual(A, [false, 0, '', ''])
|
|
157
|
+
assert.deepEqual(B, [true, []])
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('range', () => {
|
|
161
|
+
assert.deepEqual([...range(0)], [])
|
|
162
|
+
assert.deepEqual([...range(4)], [0, 1, 2, 3])
|
|
163
|
+
assert.deepEqual([...range(-4)], [0, -1, -2, -3])
|
|
164
|
+
assert.deepEqual([...range(0, -4, -1)], [0, -1, -2, -3])
|
|
165
|
+
assert.deepEqual([...range(1, 5)], [1, 2, 3, 4])
|
|
166
|
+
assert.deepEqual([...range(0, 20, 5)], [0, 5, 10, 15])
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('reduce', () => {
|
|
170
|
+
assert(reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0) === 15)
|
|
171
|
+
})
|
|
172
|
+
|
|
8
173
|
it('reversed', () => {
|
|
9
174
|
let arr = [3, 2, 1]
|
|
10
175
|
let reverse = reversed(arr)
|
|
@@ -67,10 +232,17 @@ describe('operators', () => {
|
|
|
67
232
|
})
|
|
68
233
|
|
|
69
234
|
it('times', () => {
|
|
70
|
-
let arr =
|
|
235
|
+
let arr = [...times(5, (x) => x)]
|
|
71
236
|
assert.deepEqual(arr, [0, 1, 2, 3, 4])
|
|
72
237
|
})
|
|
73
238
|
|
|
239
|
+
it('takeWhile', () => {
|
|
240
|
+
let arr = [...takeWhile([1, 2, 3, 4], (x) => x < 4)]
|
|
241
|
+
assert.deepEqual(arr, [1, 2, 3])
|
|
242
|
+
arr = [...takeWhile([1, 2, 3, 4, 5], (x) => x)]
|
|
243
|
+
assert.deepEqual(arr, [1, 2, 3, 4, 5])
|
|
244
|
+
})
|
|
245
|
+
|
|
74
246
|
it('zip', () => {
|
|
75
247
|
let users1 = [
|
|
76
248
|
{ user: 'barney', age: 36 },
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import { max, min, round, sum } from '../src/math/index.js'
|
|
3
|
+
|
|
4
|
+
describe('math', () => {
|
|
5
|
+
it('min', () => {
|
|
6
|
+
assert(min([5, 4, 3, 1, 2]) === 1)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('max', () => {
|
|
10
|
+
assert(max([7, 9, 3, 11]) === 11)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('round', () => {
|
|
14
|
+
assert(round(0.5) === 1)
|
|
15
|
+
assert(round(-0.5) === 0)
|
|
16
|
+
assert(round(1.5) === 2)
|
|
17
|
+
assert(round(1.54, 2) === 1.54)
|
|
18
|
+
assert(round(1.545, 3) === 1.545)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('sum', () => {
|
|
22
|
+
assert(sum([]) === 0)
|
|
23
|
+
assert(sum([1, 2, 3, 4]) === 10)
|
|
24
|
+
})
|
|
25
|
+
})
|
package/tests/operators.spec.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import assert from 'assert'
|
|
2
|
-
import {
|
|
3
|
-
import { bool, id, isinstance, not, all, any, comp, shallowEqual } from '../src/operators.js'
|
|
2
|
+
import { bool, id, isinstance, not, comp, shallowEqual } from '../src/operators/index.js'
|
|
4
3
|
|
|
5
4
|
describe('operators', () => {
|
|
6
5
|
it('id', () => {
|
|
@@ -11,18 +10,6 @@ describe('operators', () => {
|
|
|
11
10
|
assert(id(Object) === Object)
|
|
12
11
|
})
|
|
13
12
|
|
|
14
|
-
it('all', () => {
|
|
15
|
-
assert(all([1, true, [], 'test', {}]))
|
|
16
|
-
assert(!all([[], {}, '']))
|
|
17
|
-
assert(all([[], {}, ''], isEmpty))
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('any', () => {
|
|
21
|
-
assert(any([1, true, [], 'test', {}]))
|
|
22
|
-
assert(!any([0, false, '']))
|
|
23
|
-
assert(any([0, false, ''], (x) => !x))
|
|
24
|
-
})
|
|
25
|
-
|
|
26
13
|
it('bool', () => {
|
|
27
14
|
assert(bool(1))
|
|
28
15
|
assert(bool(true))
|
|
@@ -49,17 +36,5 @@ describe('operators', () => {
|
|
|
49
36
|
assert(shallowEqual({}, {}))
|
|
50
37
|
assert(shallowEqual([], []))
|
|
51
38
|
assert(shallowEqual({ test: true }, { test: true }))
|
|
52
|
-
assert(
|
|
53
|
-
shallowEqual(
|
|
54
|
-
{
|
|
55
|
-
value: 42,
|
|
56
|
-
// custom equals implementation will be used
|
|
57
|
-
eq(other) {
|
|
58
|
-
return this.value === other.value
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
{ value: 42 }
|
|
62
|
-
)
|
|
63
|
-
)
|
|
64
39
|
})
|
|
65
40
|
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import { any, times } from '../src/itertools/index.js'
|
|
3
|
+
import { choice, random, randomInt, sample, shuffle } from '../src/random/index.js'
|
|
4
|
+
|
|
5
|
+
const randomArr = () => [...times(10, (i) => randomInt(i, 71))]
|
|
6
|
+
|
|
7
|
+
describe('random', () => {
|
|
8
|
+
it('choice', () => {
|
|
9
|
+
let arr = randomArr()
|
|
10
|
+
for (let i = 0; i < 10; i++) {
|
|
11
|
+
assert(arr.includes(choice(arr)))
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('random', () => {
|
|
16
|
+
for (let i = 1; i <= 10; i++) {
|
|
17
|
+
let r = random(0, i)
|
|
18
|
+
assert(0 <= r && r < i)
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('randomInt', () => {
|
|
23
|
+
for (let i = 1; i <= 10; i++) {
|
|
24
|
+
let r = randomInt(0, i)
|
|
25
|
+
assert(0 <= r && r <= i)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('shuffle', () => {
|
|
30
|
+
let arr = randomArr()
|
|
31
|
+
let copy = arr.slice()
|
|
32
|
+
for (let i = 0; i < 10; i++) {
|
|
33
|
+
assert(shuffle(copy) === copy)
|
|
34
|
+
assert(copy.some((x, i) => x !== arr[i]))
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('sample', () => {
|
|
39
|
+
let arr = randomArr()
|
|
40
|
+
for (let i = 0; i < 10; i++) {
|
|
41
|
+
let s = sample(arr, 3)
|
|
42
|
+
assert(s.length === 3)
|
|
43
|
+
assert(s.some((x, i) => x !== arr[i]))
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
})
|
package/tsconfig.json
CHANGED
|
@@ -106,12 +106,20 @@
|
|
|
106
106
|
],
|
|
107
107
|
"typedocOptions": {
|
|
108
108
|
"entryPoints": [
|
|
109
|
-
"src/index.ts"
|
|
109
|
+
"./src/globals/index.ts",
|
|
110
|
+
"./src/collections/index.ts",
|
|
111
|
+
"./src/datetime/index.ts",
|
|
112
|
+
"./src/functools/index.ts",
|
|
113
|
+
"./src/itertools/index.ts",
|
|
114
|
+
"./src/math/index.ts",
|
|
115
|
+
"./src/operators/index.ts",
|
|
116
|
+
"./src/random/index.ts",
|
|
110
117
|
],
|
|
111
118
|
"out": "docs",
|
|
112
119
|
"exclude": [
|
|
113
|
-
"node_modules"
|
|
114
|
-
|
|
115
|
-
|
|
120
|
+
"node_modules"
|
|
121
|
+
],
|
|
122
|
+
"excludeExternals": true,
|
|
123
|
+
"excludePrivate": true,
|
|
116
124
|
}
|
|
117
125
|
}
|