@tim-code/my-util 0.5.12 → 0.5.13
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 +18 -12
- package/src/array.test.js +45 -5
package/package.json
CHANGED
package/src/array.js
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
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.
|
|
6
7
|
* @returns {Array}
|
|
7
8
|
*/
|
|
8
|
-
export function chunk(
|
|
9
|
-
if (
|
|
10
|
-
|
|
9
|
+
export function chunk(iterable, chunkSize = Infinity) {
|
|
10
|
+
if (chunkSize !== Infinity && (chunkSize <= 0 || chunkSize % 1 !== 0)) {
|
|
11
|
+
throw new Error("chunkSize must be a positive integer or Infinity")
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const chunks = []
|
|
14
|
+
let currentChunk = []
|
|
15
|
+
for (const element of iterable) {
|
|
16
|
+
currentChunk.push(element)
|
|
17
|
+
if (currentChunk.length >= chunkSize) {
|
|
18
|
+
chunks.push(currentChunk)
|
|
19
|
+
currentChunk = []
|
|
20
|
+
}
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
chunked.push(array.slice(i, i + chunkSize))
|
|
22
|
+
if (currentChunk.length) {
|
|
23
|
+
chunks.push(currentChunk)
|
|
18
24
|
}
|
|
19
|
-
return
|
|
25
|
+
return chunks
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
/**
|
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", () => {
|