All files / serializers json.js

100% Statements 17/17
100% Branches 10/10
100% Functions 2/2
100% Lines 15/15

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 271x       300x 300x 300x   300x 300x 300x   300x 1386x 1386x 1386x 1000x   386x 386x       300x      
module.exports = {
  name: 'json',
  desc: 'uses JSON.stringify to build a string:\n\n--spaces, -S [number]\nThe number of spaces used to format JSON. If it is set to 0 (default), the JSON is printed in a single line.\n\n--keep, -K [string]\nDetermines which JSON fields are kept. If it is left out (default), all fields remain. If it is a string formatted as a JSON array, all fields in the array are kept. See the documentation of JSON.stringify for details.\n',
  func: ({spaces, S, keep, K}) => {
    const _spaces = spaces || S || 0
    const keepStr = keep   || K || null
    const _keep   = JSON.parse(keepStr) || null
 
    return jsons => {
      let str   = ''
      const err = []
 
      for (let index = 0; index < jsons.length; index++) {
        try {
          const obj = jsons[index]
          const foo = JSON.stringify(obj, _keep, _spaces)
          if (typeof foo !== 'undefined') str += foo + '\n'
        } catch (e) {
          const msg = {msg: e.message}
          err.push(msg)
        }
      }
 
      return {err, str}
    }
  }
}