UNPKG

@pfx/base

Version:

Plugin with basic operations for pf, the fast and extensible command-line data (e.g. JSON) processor and transformer

38 lines (36 loc) 1.27 kB
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} } }