@tim-code/my-util 0.5.12 → 0.5.14
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/package.json +1 -1
- package/src/array.js +20 -13
- package/src/array.test.js +45 -5
- package/src/math.js +1 -1
package/package.json
CHANGED
package/src/array.js
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Converts an
|
|
2
|
+
* Converts an iterable into an array of arrays, where each subarray has a maximum size of chunkSize.
|
|
3
3
|
* Note the last subarray may have a length less than chunkSize.
|
|
4
|
-
*
|
|
5
|
-
* @param {
|
|
4
|
+
* If the iterable has no elements, returns an empty array since there are no chunks.
|
|
5
|
+
* @param {Iterable} iterable
|
|
6
|
+
* @param {number=} chunkSize If not provided, returns an array consisting of one chunk, which has all the elements of input iterable.
|
|
7
|
+
* This has the same effect as passing a chunk size that is greater than the number of elements in the iterable.
|
|
6
8
|
* @returns {Array}
|
|
7
9
|
*/
|
|
8
|
-
export function chunk(
|
|
9
|
-
if (!
|
|
10
|
-
|
|
10
|
+
export function chunk(iterable, chunkSize = Infinity) {
|
|
11
|
+
if (chunkSize !== Infinity && (chunkSize <= 0 || !Number.isInteger(chunkSize))) {
|
|
12
|
+
throw new Error("chunkSize must be a positive integer or Infinity")
|
|
11
13
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
const chunks = []
|
|
15
|
+
let current = []
|
|
16
|
+
for (const element of iterable) {
|
|
17
|
+
current.push(element)
|
|
18
|
+
if (current.length >= chunkSize) {
|
|
19
|
+
chunks.push(current)
|
|
20
|
+
current = []
|
|
21
|
+
}
|
|
14
22
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
chunked.push(array.slice(i, i + chunkSize))
|
|
23
|
+
if (current.length) {
|
|
24
|
+
chunks.push(current)
|
|
18
25
|
}
|
|
19
|
-
return
|
|
26
|
+
return chunks
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
/**
|
|
@@ -234,7 +241,7 @@ export function sortN(
|
|
|
234
241
|
array,
|
|
235
242
|
{ N, compare = ascending(), unsorted = false, force = false, mutate = false }
|
|
236
243
|
) {
|
|
237
|
-
if (!(N >= 0) || N
|
|
244
|
+
if (!(N >= 0) || !Number.isInteger(N)) {
|
|
238
245
|
throw new Error("N must be a nonnegative integer")
|
|
239
246
|
}
|
|
240
247
|
if (N === 0) {
|
package/src/array.test.js
CHANGED
|
@@ -22,14 +22,20 @@ describe("chunk", () => {
|
|
|
22
22
|
expect(chunk([1, 2], 2)).toEqual([[1, 2]])
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
it("returns the entire
|
|
25
|
+
it("returns the entire iterable as one chunk if chunkSize is omitted", () => {
|
|
26
26
|
expect(chunk([1, 2, 3])).toEqual([[1, 2, 3]])
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
-
it("throws if chunkSize is not a positive integer", () => {
|
|
30
|
-
expect(() => chunk([1, 2, 3], 0)).toThrow(
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
it("throws if chunkSize is not a positive integer or Infinity", () => {
|
|
30
|
+
expect(() => chunk([1, 2, 3], 0)).toThrow(
|
|
31
|
+
/chunkSize must be a positive integer or Infinity/u
|
|
32
|
+
)
|
|
33
|
+
expect(() => chunk([1, 2, 3], -1)).toThrow(
|
|
34
|
+
/chunkSize must be a positive integer or Infinity/u
|
|
35
|
+
)
|
|
36
|
+
expect(() => chunk([1, 2, 3], 1.5)).toThrow(
|
|
37
|
+
/chunkSize must be a positive integer or Infinity/u
|
|
38
|
+
)
|
|
33
39
|
})
|
|
34
40
|
|
|
35
41
|
it("handles chunkSize of 1 (each element in its own chunk)", () => {
|
|
@@ -43,6 +49,40 @@ describe("chunk", () => {
|
|
|
43
49
|
it("returns one chunk if chunkSize is much larger than array length", () => {
|
|
44
50
|
expect(chunk([1, 2, 3], 100)).toEqual([[1, 2, 3]])
|
|
45
51
|
})
|
|
52
|
+
|
|
53
|
+
it("works with Map iterables and chunks entries", () => {
|
|
54
|
+
const m = new Map([
|
|
55
|
+
["a", 1],
|
|
56
|
+
["b", 2],
|
|
57
|
+
["c", 3],
|
|
58
|
+
["d", 4],
|
|
59
|
+
["e", 5],
|
|
60
|
+
])
|
|
61
|
+
expect(chunk(m, 2)).toEqual([
|
|
62
|
+
[
|
|
63
|
+
["a", 1],
|
|
64
|
+
["b", 2],
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
["c", 3],
|
|
68
|
+
["d", 4],
|
|
69
|
+
],
|
|
70
|
+
[["e", 5]],
|
|
71
|
+
])
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it("works with non-array iterables when chunkSize is omitted (single chunk of entries)", () => {
|
|
75
|
+
const m = new Map([
|
|
76
|
+
["a", 1],
|
|
77
|
+
["b", 2],
|
|
78
|
+
])
|
|
79
|
+
expect(chunk(m)).toEqual([
|
|
80
|
+
[
|
|
81
|
+
["a", 1],
|
|
82
|
+
["b", 2],
|
|
83
|
+
],
|
|
84
|
+
])
|
|
85
|
+
})
|
|
46
86
|
})
|
|
47
87
|
|
|
48
88
|
describe("unique", () => {
|
package/src/math.js
CHANGED
|
@@ -166,7 +166,7 @@ export function isNumber(number) {
|
|
|
166
166
|
* @returns {Object|undefined} Returns undefined is array is empty
|
|
167
167
|
*/
|
|
168
168
|
export function quantiles(array, { N, key, method = Math.round }) {
|
|
169
|
-
if (!(N > 0) || N
|
|
169
|
+
if (!(N > 0) || !Number.isInteger(N)) {
|
|
170
170
|
throw new Error("N must be a positive integer")
|
|
171
171
|
}
|
|
172
172
|
if (!array.length) {
|