-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { getChunk, chunkify } = require('../../src/chunk') | ||
|
||
describe('getChunk', () => { | ||
it('splits 4 specs across 4 machines', () => { | ||
const specs = [1, 2, 3, 4] | ||
let chunk = getChunk(specs, 4, 0) | ||
expect(chunk, 'chunk 0').to.deep.equal([1]) | ||
chunk = getChunk(specs, 4, 1) | ||
expect(chunk, 'chunk 1').to.deep.equal([2]) | ||
chunk = getChunk(specs, 4, 2) | ||
expect(chunk, 'chunk 2').to.deep.equal([3]) | ||
chunk = getChunk(specs, 4, 3) | ||
expect(chunk, 'chunk 3').to.deep.equal([4]) | ||
}) | ||
|
||
it('splits 5 specs across 4 machines', () => { | ||
const specs = [1, 2, 3, 4, 5] | ||
let chunk = getChunk(specs, 4, 0) | ||
expect(chunk, 'chunk 0').to.deep.equal([1, 2]) | ||
chunk = getChunk(specs, 4, 1) | ||
expect(chunk, 'chunk 1').to.deep.equal([3]) | ||
chunk = getChunk(specs, 4, 2) | ||
expect(chunk, 'chunk 2').to.deep.equal([4]) | ||
chunk = getChunk(specs, 4, 3) | ||
expect(chunk, 'chunk 3').to.deep.equal([5]) | ||
}) | ||
|
||
it('splits 1 spec across 2 machines', () => { | ||
const specs = [1] | ||
let chunk = getChunk(specs, 2, 0) | ||
expect(chunk, 'chunk 0').to.deep.equal([1]) | ||
chunk = getChunk(specs, 2, 1) | ||
expect(chunk, 'chunk 1').to.deep.equal(undefined) | ||
}) | ||
}) | ||
|
||
describe('chunkify', () => { | ||
it('splits 5 specs across 1 machine', () => { | ||
const specs = [1, 2, 3, 4, 5] | ||
const chunks = chunkify(specs, 1, true) | ||
expect(chunks).to.deep.equal([[1, 2, 3, 4, 5]]) | ||
}) | ||
|
||
it('splits 5 specs across 2 machines', () => { | ||
const specs = [1, 2, 3, 4, 5] | ||
const chunks = chunkify(specs, 2, true) | ||
expect(chunks).to.deep.equal([ | ||
[1, 2, 3], | ||
[4, 5], | ||
]) | ||
}) | ||
|
||
it('splits 5 specs across 6 machines', () => { | ||
const specs = [1, 2, 3, 4, 5] | ||
const chunks = chunkify(specs, 6, true) | ||
expect(chunks).to.deep.equal([[1], [2], [3], [4], [5]]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// https://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays | ||
function chunkify(a, n, balanced) { | ||
if (n < 2) return [a] | ||
|
||
var len = a.length, | ||
out = [], | ||
i = 0, | ||
size | ||
|
||
if (len % n === 0) { | ||
size = Math.floor(len / n) | ||
while (i < len) { | ||
out.push(a.slice(i, (i += size))) | ||
} | ||
} else if (balanced) { | ||
while (i < len) { | ||
size = Math.ceil((len - i) / n--) | ||
out.push(a.slice(i, (i += size))) | ||
} | ||
} else { | ||
n-- | ||
size = Math.floor(len / n) | ||
if (len % size === 0) size-- | ||
while (i < size * n) { | ||
out.push(a.slice(i, (i += size))) | ||
} | ||
out.push(a.slice(size * n)) | ||
} | ||
|
||
return out | ||
} | ||
|
||
function getChunk(values, totalChunks, chunkIndex) { | ||
// split all items into N chunks and take just a single chunk | ||
if (totalChunks < 0) { | ||
throw new Error('totalChunks must be >= 0') | ||
} | ||
|
||
if (chunkIndex < 0 || chunkIndex >= totalChunks) { | ||
throw new Error( | ||
`Invalid chunk index ${chunkIndex} vs all chunks ${totalChunks}`, | ||
) | ||
} | ||
|
||
const chunks = chunkify(values, totalChunks, true) | ||
return chunks[chunkIndex] | ||
} | ||
|
||
module.exports = { getChunk, chunkify } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters