All files / latest/src/handlers/basic chop.js

100% Statements 39/39
100% Branches 7/7
100% Functions 3/3
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 2x 2x 20x 20x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 2x 2x 1x 1x  
const parse = require('./parse.js');
 
/**
 * Chops array into smaller arrays
 */
const chopArray = (toChop, chopSize) => {
  const result = [];
  for (let i = 0; i < toChop.length; i += chopSize) {
    result.push(toChop.slice(i, i + chopSize));
  }
  return result;
};
 
/**
 * Chops object into smaller objects
 */
const chopObject = (toChop, chopSize) => {
  const result = [];
  for (let i = 0; i < Object.entries(toChop).length; i += chopSize) {
    result.push(Object.fromEntries(Object.entries(toChop).slice(i, i + chopSize)));
  }
  return result;
};
 
/**
 * Chops an array or object into smaller pieces
 * @param {object} value - object or array
 * @param {number} chopSize - size of pieces.
 * @returns {Array} array of chopped pieces.
 */
const chop = (value, chopSize) => {
  const toChop = parse(value);
  if (Array.isArray(toChop)) {
    return chopArray(toChop, chopSize);
  }
  return chopObject(toChop, chopSize);
};
 
module.exports = chop;