All files / src/toArgs convertNonCommands.js

100% Statements 32/32
100% Branches 43/43
100% Functions 11/11
100% Lines 30/30

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 63 64 65 66 67 68 69 70 71 72 73 744x 72x 72x   72x 226x 226x   226x 113x 24x       89x 52x 37x 34x 22x 12x 9x 3x 2x           72x     4x         89x       3x       34x       12x       113x       124x       226x       286x       237x       73x  
const convertNonCommands = ({errs = [], opts: OPTS = []} = {}) => {
  let args  = {_: []}
  let errs2 = []
 
  for (let i = 0; i < OPTS.length; i++) {
    const opt = OPTS[i]
    const {key, values} = opt
 
    if (hasValues(opt) && !isCommandOption(opt)) {
      if (isFlagOption(opt)) {
        args[key] = {
          type: 'flag',
          count: typeof args[key] === 'undefined' ? values[0] : args[key].count + values[0]
        }
      } else if (isRest(opt)) {
        if (values[0] !== '--') args['_'] = args['_'].concat(values)
      } else if (typeof args[key] === 'undefined') {
        if (isPrimitiveVariable(opt)) {
          args[key] = values[0]
        } else if (isArrayVariable(opt)) {
          args[key] = values
        } else if (isVariadicVariable(opt)) {
          args[key] = values
        }
      }
    }
  }
 
  return {errs: errs.concat(errs2), args}
}
 
module.exports = {
  convertNonCommands
}
 
function isRest ({key, values}) {
  return typeof key === 'undefined' && Array.isArray(values) && values.length === 1 && typeof values[0] === 'string'
}
 
function isVariadicVariable (opt) {
  return isVariable(opt) && isVariadic(opt)
}
 
function isPrimitiveVariable ({key, types}) {
  return isVariable({key}) && Array.isArray(types) && types.length === 1
}
 
function isArrayVariable ({key, types}) {
  return isVariable({key}) && Array.isArray(types) && types.length > 1
}
 
function isFlagOption ({key, args, types}) {
  return isOption({key, args}) && Array.isArray(types) && types.length === 0
}
 
function isCommandOption ({key, args, opts, types}) {
  return isOption({key, args}) && isVariadic({types}) && Array.isArray(opts)
}
 
function hasValues ({values}) {
  return Array.isArray(values)
}
 
function isVariable ({key}) {
  return typeof key !== 'undefined'
}
 
function isOption ({key, args}) {
  return isVariable({key}) && Array.isArray(args)
}
 
function isVariadic ({types}) {
  return typeof types === 'undefined'
}