All files / applicators filter.js

100% Statements 23/23
100% Branches 10/10
100% Functions 2/2
100% Lines 20/20

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 311x     800x 800x 800x   800x 5960x 5960x 5960x 5960x 6893x 6893x 2829x 2829x     4642x 4642x   1318x 1318x 1318x 1318x       800x    
module.exports = {
  name: 'filter',
  desc: 'expects f to be a predicate and keeps all JSON elements for which f yields true.',
  func: (ps, {verbose}) => (jsons, lines) => {
    let jsons2 = []
    const err  = []
 
    for (let index = 0; index < jsons.length; index++) {
      let obj = jsons[index]
      try {
        let acc = obj
        for (let jndex = 0; jndex < ps.length; jndex++) {
          const p = ps[jndex]
          if (typeof acc === 'undefined' || p(acc) === false) {
            acc = undefined
            break
          }
        }
        obj = acc
        if (typeof obj !== 'undefined') jsons2.push(obj)
      } 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))
      }
    }
 
    return {err, jsons: jsons2}
  }
}