0

I have this routine which will group an array, groups of x/chunkSize items.

const createGroupedArray = function (arr, chunkSize) {

    if(!Number.isInteger(chunkSize)){
        throw 'Chunk size must be an integer.';
    }

    if(chunkSize < 1){
        throw 'Chunk size must be greater than 0.';
    }

    const groups = [];
    let i = 0;
    while (i < arr.length) {
        groups.push(arr.slice(i, i += chunkSize));
    }
    return groups;
};

so if I pass it:

createGroupedArray([1,2,3,4,5],2)

I get:

[ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]

and if I do:

createGroupedArray([1,2,3],1)

I get:

[ [ 1 ], [ 2 ], [ 3 ] ]

Does anyone know how I can test this? I am using it in a db migration script and it can't fail lol.

The reason I am doing this, is because I want to write to the db, but only ~10,000 records at a time.

  • What do you mean by "test"? Figure out what you want to test and test it. –  Oct 04 '18 at 00:55
  • I want 2 test the function to test the functionality –  Oct 04 '18 at 00:56
  • Well obviously, but what about it do you want to verify? Once you figure out what you want to verify, generate a dataset and verify it. –  Oct 04 '18 at 00:58
  • The only way to know for sure if it is going to work in your case without error would be to [generate an execution plan](https://stackoverflow.com/q/7359702/4639281) –  Oct 04 '18 at 02:28

1 Answers1

0

Reduce the array into another array where if the last item in the array's length is less than the size then add it to the last array if not then add a new array to the array with the item.

const createGroupedArray = (arr, size) => arr.reduce((result, item) => {
  if (result.length && result[result.length - 1].length < size) {
    result[result.length - 1].push(item);
  } else {
    result.push([item]);
  }
  return result;
}, []);

console.log(createGroupedArray([1,2,3,4,5],2));

console.log(createGroupedArray([1,2,3],1));

const testCreateGroupedArray = (arr, size) => {
  const result = createGroupedArray(arr, size);
  const resultSizeSuccess = result.length === (Math.floor(arr.length / size) + arr.length % size);
  const resultLastItemSizeSuccess = result[result.length - 1].length === (arr.length % size);
  return resultSizeSuccess && resultLastItemSizeSuccess;
}

console.log(testCreateGroupedArray([1,2,3,4,5],2));

console.log(testCreateGroupedArray([1,2,3],1));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • I added a simple test to make sure the arrays have the correct length, you could also iterate the arrays to make sure the items match. – Adrian Brand Oct 04 '18 at 01:17
  • Re: your edit: now you're testing code that wasn't even in the question. "Question: How do I test this code? Answer: by testing this other piece of code that is completely irrelevant." –  Oct 04 '18 at 01:17
  • The test calls a function with the same signature as yours, it is completely relevant to any function with that signature. – Adrian Brand Oct 04 '18 at 01:19
  • It isn't my function, I'm not the OP; but the question asked how to test the code in the question, not how to test this random bit of code that you fabricated from thin air. It just doesn't look like you've read the question. –  Oct 04 '18 at 01:20