All files / src/foo/toOpts assignOptsAndPosArgs.js

100% Statements 134/134
100% Branches 53/53
100% Functions 32/32
100% Lines 109/109

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 2303x   93x 93x   93x 93x 93x   93x 229x   167x   167x   167x 134x   134x 26x 29x   26x 26x 108x 43x   43x 43x 47x   43x 43x 65x 45x 45x   45x 45x 45x 20x 19x   19x   19x 51x   51x 45x     45x 21x 19x 19x   1x 1x 1x     33x 33x 33x 33x     167x     93x     3x         167x       221x   221x   221x 121x   100x 100x 54x         46x             221x             59x       62x       134x       108x       188x       20x       167x       167x       167x 167x   167x 229x   229x   229x 134x 134x       167x         62x   52x 52x   52x 59x   59x   59x 29x 29x             29x       52x       167x   157x 157x   157x 176x   176x   176x 105x 105x 105x       157x       176x       245x       176x   105x 105x   60x 60x 77x   60x  
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)
        const opts3  = opts2.map(opt => ({...opt, values}))
 
        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 - 2),
        {...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
}