All files / src/toOpts assignOptsAndPosArgs.js

100% Statements 138/138
100% Branches 55/55
100% Functions 33/33
100% Lines 112/112

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 2366x   165x 165x   165x 165x 165x   165x 455x   355x   355x   355x 299x   299x 42x 45x   42x 42x 257x 112x   112x 112x   112x 109x 105x   7x 7x     112x 145x 114x 114x   114x 114x 114x 31x 30x   30x   30x 72x   72x 63x     63x 32x 30x 30x   1x 1x 1x     56x 56x 56x 56x     355x     165x     6x         355x       523x   523x   523x 254x   269x 269x 168x         101x             523x             91x       142x       299x       257x       418x       31x       355x       355x       355x 355x   355x 455x   455x   455x 299x 299x       355x         100x   86x 86x   86x 100x   100x   100x 44x 44x             44x       86x       355x   341x 341x   341x 393x   393x   393x 255x 255x 255x       341x       393x       536x       393x   255x 255x   141x 141x 158x   141x  
const {pipe} = require('../pipe')
 
const assignOptsAndPosArgs = opt => ({errs, argv}) => {
  let opts = []
 
  let optStack = [opt]
  let at  = 0
  let arg = argv[at]
 
  while (arg) {
    const firstOptsFrom = firstWithOr([optStack, []], ([_, opts]) => opts.length > 0)
 
    const [optStack2, opts2] = firstOptsFrom([findOpts, findPosArg])(optStack, arg)
 
    optStack = optStack2
 
    if (opts2.length > 0) {
      const opt2 = opts2[0]
 
      if (isFlag(opt2)) {
        const values = [1]
        const opts3  = opts2.map(opt => ({...opt, values}))
        
        opts = mergeOptsUsingStack(opts, opts3, optStack)
        at   = at + 1
      } else if (isPrimitiveOrArray(opt2)) {
        if (isOption(opt2)) at = at + 1
 
        const length = opt2.types.length
        const values = argv.slice(at, at + length)
 
        if (values.length === length) {
          const opts3 = opts2.map(opt => ({...opt, values}))
          opts = mergeOptsUsingStack(opts, opts3, optStack)
        } else {
          const opts3 = [{values: [arg]}, ...values.map(value => ({values: [value]}))]
          opts = mergeOptsUsingStack(opts, opts3, optStack)
        }
 
        at = at + length
      } else if (isCommand(opt2)) {
        const values = []
        const opts3  = opts2.map(opt => ({...opt, values}))
 
        opts = mergeOptsUsingStack(opts, opts3, optStack)
        at   = at + 1
        optStack = [...optStack, opt2]
      } else if (isVariadic(opt2)) {
        if (isOption(opt2)) at = at + 1
 
        const values = []
 
        for (let at2 = at; at2 < argv.length; at2++) {
          const arg2 = argv[at2]
 
          if (arg2 === '--') break
          else values.push(arg2)
        }
 
        const types = Array.from({length: values.length}, () => 'string')
        const opts3 = opts2.map(opt => ({...opt, types, values}))
        opts = mergeOptsUsingStack(opts, opts3, optStack)
        at   = at + values.length
      } else {
        const opts3 = [{values: [arg]}]
        opts = mergeOptsUsingStack(opts, opts3, optStack)
        at = at + 1
      }
    } else {
      const opts3 = [{values: [arg]}]
      opts = mergeOptsUsingStack(opts, opts3, optStack)
      at = at + 1
      if (arg === '--') optStack = optStack.slice(0, 1)
    }
 
    arg = argv[at]
  }
 
  return {errs, opts}
}
 
module.exports = {
  assignOptsAndPosArgs
}
 
function mergeOptsUsingStack (opts, opts2, stack) {
  return mergeOpts(opts, opts2, stack.slice(1))
}
 
function mergeOpts (opts, opts2, stack) {
  const [cmd, ...tail] = stack
 
  let opts3 = undefined
  
  if (opts.length === 0) {
    opts3 = opts2
  } else {
    const opt = opts[opts.length - 1]
    if (typeof cmd !== 'undefined' && opt.key === cmd.key) {
      opts3 = [{
        ...opt,
        values: mergeOpts(opt.values, opts2, tail)
      }]
    } else {
      opts3 = [
        opt,
        ...opts2
      ]
    }
  }
  
  return [
    ...opts.slice(0, -1),
    ...opts3
  ]
}
 
function isPosArg ({args}) {
  return typeof args === 'undefined'
}
 
function isOption ({args}) {
  return typeof args !== 'undefined'
}
 
function isFlag ({types}) {
  return Array.isArray(types) && types.length === 0
}
 
function isPrimitiveOrArray ({types}) {
  return Array.isArray(types) && types.length > 0
}
 
function isCommand ({types, args, opts}) {
  return Array.isArray(args) && typeof types === 'undefined' && Array.isArray(opts)
}
 
function isVariadic ({key, types, opts}) {
  return typeof key === 'string' && typeof types === 'undefined' && typeof opts === 'undefined'
}
 
function firstWithOr (defaultRes, p) {
  return fs => (...params) => defaultTo(defaultRes)(firstWith(p)(fs)(...params))
}
 
function defaultTo (def) {
  return a => typeof a === 'undefined' ? def : a
}
 
function firstWith (p) {
  return fs => (...params) => {
    let res = undefined
 
    for (let i = 0; i < fs.length; i++) {
      const f = fs[i]
 
      const res2 = f(...params)
 
      if (p(res2)) {
        res = res2
        break
      }
    }
 
    return res
  }
}
 
function findPosArg (optStack, arg) {
  if (arg === '--') return [optStack, []]
 
  let posArgs   = []
  let optStack2 = optStack
 
  for (let i = optStack.length - 1; i >= 0; i--) {
    const opt = optStack[i]
    
    const posArgIndex = opt.opts.findIndex(isPosArg)
 
    if (posArgIndex > -1) {
      posArgs   = opt.opts.slice(posArgIndex, posArgIndex + 1)
      optStack2 = [
        ...optStack.slice(0, i),
        {...opt, opts: [
          ...opt.opts.slice(0, posArgIndex),
          ...opt.opts.slice(posArgIndex + 1)
        ]}
      ]
      break
    }
  }
 
  return [optStack2, posArgs]
}
 
function findOpts (optStack, arg) {
  if (arg === '--') return [optStack, []]
 
  let opts      = []
  let optStack2 = optStack
 
  for (let i = optStack.length - 1; i >= 0; i--) {
    const opt = optStack[i]
 
    const opts2 = getOpts(arg, opt.opts)
 
    if (opts2.length > 0) {
      opts      = opts2
      optStack2 = optStack.slice(0, i + 1)
      break
    }
  }
 
  return [optStack2, opts]
}
 
function getOpts (arg, opts) {
  return pipe(filterOpts(arg), reduceOpts)(opts)
}
 
function filterOpts (arg) {
  return opts => opts.filter(({args = []}) => args.indexOf(arg) > -1)
}
 
function reduceOpts (opts) {
  if (opts.length === 0) return opts
 
  const cmd = opts.find(isCommand)
  if (typeof cmd !== 'undefined') return [cmd]
 
  const fst   = opts[0]
  const opts2 = opts.filter(
    ({types}) => typeof fst.types === 'undefined' ? typeof types === 'undefined' : types.length === fst.types.length
  )
  return opts2
}