All files mappers.js

70% Statements 35/50
100% Branches 3/3
100% Functions 0/0
70% Lines 35/50

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 43 44 45 46 47 48 49 501x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x        
// takes a function that evaluates to true or false and static if and else responses. return function that evaluates condition and returns appropriate response
export function showIfElse (condition, a, b = []) {
  return () => condition() ? a : b
}
 
// takes an object and a mapping-function. returns a function that maps the object through the function and returns previously calculated mappings on future calls
export function mapEntries (object, f) {
  const map = new Map()
  return () => {
    const o = (typeof object === 'function' ? object() : object) || {}
    map.forEach(indexedValues => {
      indexedValues.index = 0
    })
    const mappedEntries = Object.entries(o).map(([name, value]) => {
      if (!map.has(value)) {
        map.set(value, { index: 0, values: [] })
      }
      const indexedValues = map.get(value)
      if (indexedValues.values.length <= indexedValues.index) {
        indexedValues.values.push(f(value, name))
      }
      return indexedValues.values[indexedValues.index++]
    })
    // cleanup any unmapped-to values
    map.forEach((indexedValues, key) => {
      if (indexedValues.index) {
        indexedValues.values.splice(indexedValues.index)
      } else {
        map.delete(key)
      }
    })
    return mappedEntries
  }
}
 
export function mapSwitch (expression, f) {
  const map = new Map()
  return () => {
    const expr = expression()
    if (!map.has(expr)) {
      map.set(expr, f(expr))
    }
    return map.get(expr)
  }
}
 
export function objToDeclarations (obj = {}) {
  return Object.entries(obj).map(([name, value]) => `${name}: ${value};`).join('')
}