All files / lib/helpers numbers.js

100% Statements 29/29
87.5% Branches 14/16
100% Functions 5/5
100% Lines 19/19

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 40 41 42  21x 7x     1x 1x 1x     1x 1x 1x 3x 3x   1x     1x     8x 4x 4x 1x   3x     1x 1x                    
 
const randomBetween = ([min, max]) => {
  return Math.floor(Math.random()*(max-min+1)+min)
}
 
const randomElementInArray = function randomElementInArray(value) {
  const ele = Math.floor(Math.random() * (value.length - 1))
  return value[ele];
}
 
const randomElementsInArray = (value) => {
  const subArray = [...value]
  for (let i = subArray.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [subArray[i], subArray[j]] = [subArray[j], subArray[i]];
  }
  return subArray.slice(0, Math.floor(Math.random() * subArray.length) || 1);
}
 
const randomBetweenWithString = (value, {
  prefix = '',
  suffix = ''
} = {}) => {
  const randomNumber = randomBetween(value)
  if (!prefix && !suffix) {
    return `${randomNumber}`
  }
  return `${prefix}${randomNumber}${suffix}`
}
 
const incrementNumber = (value, { index, from = 0 }) => {
  return from + index
}
 
export {
  incrementNumber,
  randomBetween,
  randomBetweenWithString,
  randomElementInArray,
  randomElementsInArray,
}