All files / src parser.js

100% Statements 17/17
100% Branches 8/8
100% Functions 9/9
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 465x 5x 5x 5x 5x   5x             10x         98x   98x                   98x 98x 165x     98x 98x 165x                
const {Async}               = require('./Async')
const {fromArgs: FROM_ARGS} = require('./fromArgs')
const {lexerF}              = require('./lexer')
const {toArgs:   TO_ARGS}   = require('./toArgs')
const {Sync}                = require('./Sync')
 
module.exports = {
  parserF,
  parser:     parserF(Async),
  parserSync: parserF(Sync)
}
 
function parserF (Mode) {
  return (stages = {}, substages = {}) => {
    const {
      toArgs   = TO_ARGS,
      args     = [],
      fromArgs = FROM_ARGS
    } = stages
  
    return opt => Mode.then(
      lexerF(Mode)(stages, substages)(opt),
      toArgs,
      transformArgs(Mode)(args),
      fromArgs
    )
  }
}
 
function transformArgs (Mode) {
  return (argsStages) => ({errs = [], args = []} = {errs: [], args: []}) => {
    const promises = args.map(arg => {
      return Mode.then(...argsStages)({errs: [], args: arg})
    })
 
    return Mode.then(results => {
      return results.reduce(
        ({errs, args}, {errs: errs2, args: args2}) => ({
          errs: [...errs, ...errs2],
          args: [...args, args2]
        }),
        {errs, args: []}
      )
    })(Mode.all(promises))
  }
}