All files / src/util array.js

100% Statements 21/21
100% Branches 2/2
100% Functions 7/7
100% Lines 21/21
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    1x     1x 1x 1x 1x         31x 31x 78x         13x 13x 29x   13x       7x   7x 16x     7x      
// user-land array functions for perf
// tested by testing all of the above functions
 
export function forEach (arr, fn) {
  const l = arr.length
  for (let i = 0; i < l; ++i) {
    fn(arr[i], i)
  }
}
 
export function map (arr, fn) {
  const newArr = []
  forEach(arr, (x, i) => { newArr[i] = fn(x, i) })
  return newArr
}
 
export function reduce (arr, seed, fn) {
  let r = seed
 
  forEach(arr, (x) => {
    r = fn(r, x)
  })
 
  return r
}
 
export function filter (arr, predicate) {
  const newArr = []
 
  forEach(arr, (x, i) => {
    if (predicate(x, i)) {
      newArr[newArr.length] = x
    }
  })
 
  return newArr
}