All files / applicators flatMap.js

100% Statements 28/28
100% Branches 10/10
100% Functions 2/2
100% Lines 24/24

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 381x     600x 600x 600x   600x 2962x 2962x 2962x 2962x 2962x 2962x 2962x   1747x   1215x 1215x 1215x 1215x   2962x 1330x 920x 4556x 4556x     410x         600x    
module.exports = {
  name: 'flatMap',
  desc: 'applies f to each element, but acts differently depending on f\'s result: On undefined return nothing. On [...] return every array item individually or nothing for empty arrays. Otherwise act like map.',
  func: (fs, {verbose}) => (jsons, lines) => {
    const jsons2 = []
    const err    = []
 
    for (let index = 0; index < jsons.length; index++) {
      const obj = jsons[index]
      let objs  = undefined
      try {
        let acc = obj
        for (let jndex = 0; jndex < fs.length; jndex++) {
          const f = fs[jndex]
          if (typeof acc !== 'undefined') acc = f(acc)
        }
        objs = acc
      } catch (e) {
        const msg  =               {msg: e.message}
        const line = verbose > 0 ? {line: lines[index]}                 : {}
        const info = verbose > 1 ? {info: JSON.stringify(obj, null, 0)} : {}
        err.push(Object.assign(msg, line, info))
      }
      if (typeof objs !== 'undefined') {
        if (Array.isArray(objs)) {
          for (let undex = 0; undex < objs.length; undex++) {
            const obj = objs[undex]
            jsons2.push(obj)
          }
        } else {
          jsons2.push(objs)
        }
      }
    }
 
    return {err, jsons: jsons2}
  }
}