You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
835 B

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import slice from "./slice.js";
  3. /**
  4. * Splits a collection into slices of the specified length.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.16.0
  9. * @category List
  10. * @sig Number -> [a] -> [[a]]
  11. * @sig Number -> String -> [String]
  12. * @param {Number} n
  13. * @param {Array} list
  14. * @return {Array}
  15. * @example
  16. *
  17. * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
  18. * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
  19. */
  20. var splitEvery =
  21. /*#__PURE__*/
  22. _curry2(function splitEvery(n, list) {
  23. if (n <= 0) {
  24. throw new Error('First argument to splitEvery must be a positive integer');
  25. }
  26. var result = [];
  27. var idx = 0;
  28. while (idx < list.length) {
  29. result.push(slice(idx, idx += n, list));
  30. }
  31. return result;
  32. });
  33. export default splitEvery;