@everymundo/array-helpers
Advanced tools
Comparing version 1.5.2 to 1.6.0
@@ -1,2 +0,2 @@ | ||
function * chunksGenerator (a, _chunkLen) { | ||
function * chunksGenerator (a, _chunkLen, constructor = a.constructor) { | ||
const chunkLen = _chunkLen | 0 | ||
@@ -8,12 +8,13 @@ | ||
// const { constructor } = a | ||
if (chunkLen >= a.length) { | ||
return yield a.constructor.from(a) | ||
return yield constructor.from(a) | ||
} | ||
const inputLength = a.length | ||
const numberOfChunks = Math.ceil(a.length / chunkLen) | ||
// const res = new constructor(numberOfChunks) | ||
let chunkSizeDiff | ||
for (let i = 0; i < numberOfChunks; i++) { | ||
// const chunk = new constructor(chunkLen) | ||
const chunk = [] | ||
chunkSizeDiff = inputLength - i * chunkLen | ||
const chunk = new constructor(chunkSizeDiff > chunkLen ? chunkLen : chunkSizeDiff) | ||
@@ -20,0 +21,0 @@ for (let ix, j = 0; j < chunkLen && (ix = i * chunkLen + j) < a.length; j++) { |
@@ -1,29 +0,6 @@ | ||
function inChunks (a, chunkLen) { | ||
if (chunkLen < 1) { | ||
throw new Error(`n[${chunkLen}] < 1`) | ||
} | ||
const { constructor } = a | ||
if (chunkLen >= a.length) { | ||
return [constructor.from(a)] | ||
} | ||
const numberOfChunks = Math.ceil(a.length / chunkLen) | ||
const res = new constructor(numberOfChunks) | ||
for (let i = 0; i < numberOfChunks; i++) { | ||
let currentChunkLength = chunkLen | ||
if ((i * chunkLen + chunkLen) > a.length) { | ||
currentChunkLength = (i * chunkLen + chunkLen) - a.length | ||
} | ||
res[i] = new constructor(+currentChunkLength) | ||
for (let ix, j = 0; j < currentChunkLength && (ix = i * chunkLen + j) < a.length; j++) { | ||
res[i][j] = a[ix] | ||
} | ||
} | ||
return res | ||
const chunksGenerator = require('./chunks-generator') | ||
function inChunks (a, _chunkLen, constructor = a.constructor) { | ||
return constructor.from(chunksGenerator(a, _chunkLen, constructor)) | ||
} | ||
module.exports = inChunks |
{ | ||
"name": "@everymundo/array-helpers", | ||
"version": "1.5.2", | ||
"version": "1.6.0", | ||
"description": "Helpers to handle array operations in a more elegant way", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,2 +28,29 @@ 'use strict' | ||
context('input type is a Int32Array', () => { | ||
it('should return 2 chunk with a similar array but not the same array', () => { | ||
const list = new Int32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11]) | ||
const result = Array.from(chunksGenerator(list, 5)) | ||
const expected = [new Int32Array([1, 2, 3, 4, 5]), new Int32Array([6, 7, 8, 9, 0]), new Int32Array([11])] | ||
expect(result).to.deep.equal(expected) | ||
expect(result[0]).to.be.instanceof(Int32Array) | ||
expect(result[1]).to.be.instanceof(Int32Array) | ||
expect(result[2]).to.be.instanceof(Int32Array) | ||
}) | ||
}) | ||
context('input type is a List', () => { | ||
it('should return 2 chunk with a similar array but not the same array', () => { | ||
const List = require('../classes/list') | ||
const list = new List(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) | ||
const result = List.from(chunksGenerator(list, 5)) | ||
const expected = new List(new List(1, 2, 3, 4, 5), new List(6, 7, 8, 9, 0)) | ||
expect(result).to.deep.equal(expected) | ||
expect(result[0]).to.not.equal(list) | ||
expect(result[0]).to.be.instanceof(List) | ||
expect(result[1]).to.be.instanceof(List) | ||
}) | ||
}) | ||
context('array.length < chunk size', () => { | ||
@@ -30,0 +57,0 @@ it('should return the proper chunks', () => { |
@@ -13,3 +13,3 @@ 'use strict' | ||
expect(caller).to.throw(Error, 'n[0] < 1') | ||
expect(caller).to.throw(Error, 'Chunk Length has to be a valid Number greater than 0') | ||
}) | ||
@@ -16,0 +16,0 @@ }) |
19810
418