All files / src/toArgs index.js

100% Statements 47/47
100% Branches 48/48
100% Functions 20/20
100% Lines 36/36

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 108 109 110 111 112 113 114 115 116 117 118 1195x 199x 199x   199x   199x 295x   295x 290x   46x 37x               46x     31x                     31x     83x       83x     27x       27x     5x       5x     77x   77x   77x 106x   106x             199x     5x         295x       290x       244x       213x       130x       103x       98x       905x       799x       788x  
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 isEmptyArray(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 isEmptyArray ({key, types, opts, values}) {
  return isString(key) && arrLen(types, _ => _ === 0) && 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'
}