Functional Weave
Code in TypeScript

collections.chunk@1.0.0

vectors.json

1,378 bytes · view raw

[
  { "name": "seven items in threes leaves a short last batch", "args": [[1, 2, 3, 4, 5, 6, 7], 3], "expect": [[1, 2, 3], [4, 5, 6], [7]] },
  { "name": "a size that divides exactly leaves no short batch", "args": [[1, 2, 3, 4, 5, 6], 3], "expect": [[1, 2, 3], [4, 5, 6]] },
  { "name": "size one gives one batch per item", "args": [["a", "b", "c"], 1], "expect": [["a"], ["b"], ["c"]] },
  { "name": "a size larger than the list is one batch holding everything", "args": [["a", "b"], 10], "expect": [["a", "b"]] },
  { "name": "a size equal to the list is one full batch", "args": [["a", "b", "c"], 3], "expect": [["a", "b", "c"]] },
  { "name": "an empty list gives no batches, not one empty batch", "args": [[], 5], "expect": [] },
  { "name": "records pass through untouched and in order", "args": [[{ "id": 1 }, { "id": 2 }, { "id": 3 }], 2], "expect": [[{ "id": 1 }, { "id": 2 }], [{ "id": 3 }]] },
  { "name": "one short of a whole number of batches", "args": [[1, 2, 3, 4, 5], 2], "expect": [[1, 2], [3, 4], [5]] },
  { "name": "size zero is an error, not 'no limit'", "args": [[1, 2, 3], 0], "expectError": "size must be at least 1" },
  { "name": "a negative size is an error", "args": [[1, 2, 3], -2], "expectError": "size must be at least 1" },
  { "name": "a fractional size is an error", "args": [[1, 2, 3], 1.5], "expectError": "size must be a whole number" }
]