Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Last active October 7, 2015 10:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save punmechanic/b3f6ef78765e4a3cdc4d to your computer and use it in GitHub Desktop.
Save punmechanic/b3f6ef78765e4a3cdc4d to your computer and use it in GitHub Desktop.
'use strict'
/**
* Partition the given list into separate groups which may be, at most, the size given.
* @param {Array<T>} list This may be any array-like object that supports indexing and has a length property
* @param {Number} size Any positive integer that is not NaN
* @return {Array<Array<T>>} A set of arrays that are sized based upon the given size. The nested arrays may be
* no larger than the given size, but **may** be smaller; there will be no empty arrays
* @throws {TypeError} If list is falsey (i.e, undefined/null)
* @throws {TypeError} If list does not have a length property
* @throws {TypeError} If size is falsey or NaN
* @throws {TypeError} If size is less than or equal to zero
*/
function partition(list, size) {
if (!list) {
throw new TypeError('list must exist')
}
if (!list.length) {
throw new TypeError('list is not an array-like object')
}
if (size === null || size === undefined || isNaN(size)) {
throw new TypeError('size is not a number')
}
if (size < 0 || size === 0) {
throw new Error('size must be greater than zero')
}
const groups = []
let currentGroup = []
for (let index = 0; index < list.length; index++) {
currentGroup.push(list[index])
if (currentGroup.length >= size || index === list.length - 1) {
groups.push(currentGroup)
currentGroup = []
}
}
return groups
}
module.exports = partition
'use strict'
const test = require('tape')
const partition = require('./partition')
const inputList = [1, 2, 3, 4, 5]
runTestCase('example 1', inputList, 2, [[1,2],[3,4],[5]])
runTestCase('example 2', inputList, 3, [[1,2,3],[4,5]])
runTestCase('example 3', inputList, 1, [[1],[2],[3],[4],[5]])
function runTestCase(name, inputList, arraySize, expectedOutput) {
test(name, function (t) {
t.plan(1)
const output = partition(inputList, arraySize)
t.deepEqual(output, expectedOutput)
})
}
test('throw if size is null', function (t) {
const boundFun = partition.bind(undefined, inputList, null)
t.throws(boundFun, TypeError)
t.end()
})
test('throw if size is undefined', function (t) {
const boundFun = partition.bind(undefined, inputList, undefined)
t.throws(boundFun, TypeError)
t.end()
})
test('throw if size is NaN', function (t) {
const boundFun = partition.bind(undefined, inputList, NaN)
t.throws(boundFun, TypeError)
t.end()
})
test('throw if size is negative', function (t) {
const boundFun = partition.bind(undefined, inputList, -5)
t.throws(boundFun, Error)
t.end()
})
test('throw if size is zero', function (t) {
// This is because it would not make sense to have a list partitioned
// into an array of zero elements. If someone wants something like that,
// they likely want to simply create empty arrays from existing elements,
// which could be done using Array.prototype.map
const boundFun = partition.bind(undefined, inputList, 0)
t.throws(boundFun, Error)
t.end()
})
test('throw if list is null', function (t) {
const boundFun = partition.bind(undefined, null, 1)
t.throws(boundFun, TypeError)
t.end()
})
test('throw if list is undefined', function (t) {
const boundFun = partition.bind(undefined, undefined, 1)
t.throws(boundFun, TypeError)
t.end()
})
test('throw if list is not an array-like object', function (t) {
const list = {
length: undefined
}
const boundFun = partition.bind(undefined, list, 1)
t.throws(boundFun, TypeError)
t.end()
})
@punmechanic
Copy link
Author

exception handling might be a bit overkill, and honestly, the tests aren't great because some of these will throw errors anyway. For example, if we didn't check that the list was an array-like object, the code would still error when you compare list.length to the size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment