All files / src/toOpts combine.js

100% Statements 40/40
100% Branches 26/26
100% Functions 1/1
100% Lines 35/35

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 636x   6x 495x 495x   495x 2878x   2878x 494x   2384x   2384x 2503x 2503x   2503x 2503x 2376x 1952x 1953x   1953x 1953x 1441x 1441x   512x       424x     127x 127x 127x 127x   127x 127x 126x 125x   1x     1x               495x     6x    
const {invalidTypes, nonMatchingArgumentTypes, invalidOptionsListInCombine} = require('../errors')
 
const combine = (...ARGUMENTS) => {
  let errs   = []
  const args = {}
 
  for (let i = 0; i < ARGUMENTS.length; i++) {
    const {errs: ERRS = [], args: ARGS = []} = ARGUMENTS[i]
 
    if (ERRS.length > 0) {
      errs = errs.concat(ERRS)
    } else {
      const keys = Object.keys(ARGS)
 
      for (let j = 0; j < keys.length; j++) {
        const arg     = keys[j]
        const options = ARGS[arg]
 
        const firstTimeArg = typeof args[arg] === 'undefined'
        if (firstTimeArg) {
          if (Array.isArray(options) && options.length > 0) {
            for (let k = 0; k < options.length; k++) {
              const option = options[k]
              
              const optionHasValidType = Array.isArray(option.types) || typeof option.types === 'undefined'
              if (optionHasValidType) {
                if (typeof args[arg] === 'undefined') args[arg] = []
                args[arg].push(option)
              } else {
                errs.push(invalidTypes({types: option.types, option}))
              }
            }
          } else {
            errs.push(invalidOptionsListInCombine({options, arg, argument: ARGUMENTS[i]}))
          }
        } else {
          const ref   = args[arg][0]
          const types = ref.types
          for (let k = 0; k < options.length; k++) {
            const option = options[k]
 
            const optionHasValidType = Array.isArray(option.types) || typeof option.types === 'undefined'
            if (optionHasValidType) {
              if ((option.types || []).length === (types || []).length) {
                args[arg].push(option)
              } else {
                errs.push(nonMatchingArgumentTypes({arg, ref, option}))
              }
            } else {
              errs.push(invalidTypes({types: option.types, option}))
            }
          }
        }
      }
    }
  }
 
  return {errs, args}
}
 
module.exports = {
  combine
}