@tim-code/my-util 0.5.13 → 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 +10 -9
- package/src/math.js +1 -1
package/package.json
CHANGED
package/src/array.js
CHANGED
|
@@ -4,23 +4,24 @@
|
|
|
4
4
|
* If the iterable has no elements, returns an empty array since there are no chunks.
|
|
5
5
|
* @param {Iterable} iterable
|
|
6
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.
|
|
7
8
|
* @returns {Array}
|
|
8
9
|
*/
|
|
9
10
|
export function chunk(iterable, chunkSize = Infinity) {
|
|
10
|
-
if (chunkSize !== Infinity && (chunkSize <= 0 || chunkSize
|
|
11
|
+
if (chunkSize !== Infinity && (chunkSize <= 0 || !Number.isInteger(chunkSize))) {
|
|
11
12
|
throw new Error("chunkSize must be a positive integer or Infinity")
|
|
12
13
|
}
|
|
13
14
|
const chunks = []
|
|
14
|
-
let
|
|
15
|
+
let current = []
|
|
15
16
|
for (const element of iterable) {
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
chunks.push(
|
|
19
|
-
|
|
17
|
+
current.push(element)
|
|
18
|
+
if (current.length >= chunkSize) {
|
|
19
|
+
chunks.push(current)
|
|
20
|
+
current = []
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
if (
|
|
23
|
-
chunks.push(
|
|
23
|
+
if (current.length) {
|
|
24
|
+
chunks.push(current)
|
|
24
25
|
}
|
|
25
26
|
return chunks
|
|
26
27
|
}
|
|
@@ -240,7 +241,7 @@ export function sortN(
|
|
|
240
241
|
array,
|
|
241
242
|
{ N, compare = ascending(), unsorted = false, force = false, mutate = false }
|
|
242
243
|
) {
|
|
243
|
-
if (!(N >= 0) || N
|
|
244
|
+
if (!(N >= 0) || !Number.isInteger(N)) {
|
|
244
245
|
throw new Error("N must be a nonnegative integer")
|
|
245
246
|
}
|
|
246
247
|
if (N === 0) {
|
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) {
|