All files / src/old/toOpts addPositionalArguments.js

100% Statements 36/36
100% Branches 26/26
100% Functions 6/6
100% Lines 32/32

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 6982x 82x 82x   82x   82x   82x   82x 145x   145x 16x   16x 5x   11x 8x 8x 8x   3x       129x       82x 2x 5x   2x 2x     82x 2x 2x       82x     5x         145x 145x                   82x 251x    
const addPositionalArguments = (opts = []) => ({errs = [], opts: opts2 = []} = {}) => {
  const errs2 = []
  const opts3 = []
 
  const posArgs = positionalArguments(opts)
 
  let at = 0
 
  let vals = []
 
  for (let i = 0; i < opts2.length; i++) {
    const opt = opts2[i]
 
    if (isRest(opt) && posArgs.length > at) {
      const posArg = posArgs[at]
 
      if (typeof posArg.types === 'undefined') {
        vals = vals.concat(opt.values)
      } else {
        if (posArg.types.length === vals.length + 1) {
          opts3.push({...posArg, values: vals.concat(opt.values)})
          at++
          vals = []
        } else {
          vals = vals.concat(opt.values)
        }
      }
    } else {
      opts3.push(opt)
    }
  }
 
  if (vals.length > 0 && posArgs.length > at && typeof posArgs[at].types === 'undefined') {
    const posArg = posArgs[at]
    const types = Array.from({length: vals.length}, () => 'string')
 
    opts3.push({...posArg, types, values: vals})
    vals = []
  }
 
  if (vals.length > 0) {
    for (let i = 0; i < vals.length; i++) {
      opts3.push({values: [vals[i]]})
    }
  }
 
  return {errs: errs.concat(errs2), opts: opts3}
}
 
module.exports = {
  addPositionalArguments
}
 
function isRest (opt) {
  const keys = Object.keys(opt)
  return (
    keys.length === 1 &&
    keys[0] === 'values' &&
    Array.isArray(opt.values) &&
    opt.values.length === 1 &&
    opt.values[0] !== '--'
  )
}
 
function positionalArguments (opts) {
  return opts.filter(
    ({key, args}) => typeof key === 'string' && typeof args === 'undefined'
  )
}