@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.5.12",
3
+ "version": "0.5.14",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/array.js CHANGED
@@ -1,22 +1,29 @@
1
1
  /**
2
- * Converts an array into an array of arrays, where each subarray has a maximum size of chunkSize.
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
- * @param {Array} array
5
- * @param {number=} chunkSize If not provided, defaults to the length of array, returning the input array as one chunk.
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(array, chunkSize = array.length) {
9
- if (!array.length) {
10
- return []
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
- if (chunkSize <= 0 || chunkSize % 1 !== 0) {
13
- throw new Error("chunkSize must be a positive integer")
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
- const chunked = []
16
- for (let i = 0; i < array.length; i += chunkSize) {
17
- chunked.push(array.slice(i, i + chunkSize))
23
+ if (current.length) {
24
+ chunks.push(current)
18
25
  }
19
- return chunked
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 % 1 !== 0) {
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 array as one chunk if chunkSize is omitted", () => {
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("chunkSize must be a positive integer")
31
- expect(() => chunk([1, 2, 3], -1)).toThrow("chunkSize must be a positive integer")
32
- expect(() => chunk([1, 2, 3], 1.5)).toThrow("chunkSize must be a positive integer")
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 % 1 !== 0) {
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) {