All files / src/foo/toArgs index.js

100% Statements 42/42
100% Branches 41/41
100% Functions 17/17
100% Lines 33/33

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 1082x 72x 72x   72x   72x 106x   106x 101x   18x 13x               18x     15x                     15x     25x       25x     16x       16x     17x   17x   17x 18x   18x             72x     2x         106x       101x       83x       68x       43x       27x       274x       285x       221x  
const toArgs = ({errs = [], opts = []} = {errs: [], opts: []}) => {
  let errs2 = []
  let args  = []
  
  args.push({_: []})
 
  for (let i = 0; i < opts.length; i++) {
    const opt = opts[i]
 
    if (isObject(opt)) {
      switch (true) {
        case isRest(opt): {
          if (opt.values[0] !== '--') {
            args[0] = {
              ...args[0],
              _: [
                ...args[0]._,
                ...opt.values
              ]
            }
          }
          break
        }
        case isFlag(opt): {
          args[0] = {
            ...args[0],
            [opt.key]: (
              isUndefined(args[0][opt.key])
                ? {type: 'flag', count: opt.values[0]}
                : {
                  ...args[0][opt.key],
                  count: args[0][opt.key].count + opt.values[0]
                }
            )
          }
          break
        }
        case isPrimitive(opt): {
          args[0] = {
            ...args[0],
            ...(isUndefined(args[0][opt.key]) ? {[opt.key]: opt.values[0]} : {})
          }
          break
        }
        case isArray(opt): {
          args[0] = {
            ...args[0],
            ...(isUndefined(args[0][opt.key]) ? {[opt.key]: opt.values} : {})
          }
          break
        }
        case isSubcommand(opt): {
          const {errs: errs3, args: args2} = toArgs({opts: opt.values})
  
          errs2 = [...errs2, ...errs3]
  
          for (let j = 0; j < args2.length; j++) {
            const arg = args2[j]
  
            args.push({[opt.key]: arg})
          }
        }
      }
    }
  }
 
  return {errs, args}
}
 
module.exports = {
  toArgs
}
 
function isObject (a) {
  return !Array.isArray(a) && a !== null && typeof a === 'object'
}
 
function isRest ({key, types, opts, values}) {
  return [key, types, opts].every(isUndefined) && arrLen(values, _ => _ > 0)
}
 
function isFlag ({key, types, opts, values}) {
  return isString(key) && arrLen(types, _ => _ === 0) && isUndefined(opts) && arrLen(values, _ => _ > 0)
}
 
function isPrimitive ({key, types, opts, values}) {
  return isString(key) && arrLen(types, _ => _ === 1) && isUndefined(opts) && arrLen(values, _ => _ === 1)
}
 
function isArray ({key, types, opts, values}) {
  return isString(key) && arrLen(types, _ => _ > 1) && isUndefined(opts) && arrLen(values, _ => _ === types.length)
}
 
function isSubcommand ({key, types, opts, values}) {
  return isString(key) && isUndefined(types) && Array.isArray(opts) && Array.isArray(values)
}
 
function arrLen (a, p) {
  return Array.isArray(a) && p(a.length)
}
 
function isUndefined (a) {
  return typeof a === 'undefined'
}
 
function isString (a) {
  return typeof a === 'string'
}