All files / src/toOpts addRemainingOptsAndPosArgs.js

100% Statements 26/26
100% Branches 27/27
100% Functions 9/9
100% Lines 22/22

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 63130x         6x         210x   210x   210x 261x   261x 216x 216x 216x   45x       210x 237x   237x 41x       210x       498x       714x 830x               958x       829x           216x  
const addRemainingOptsAndPosArgs = opt => ({errs, opts}) => ({
  errs,
  opts: addRemaining(opt, opts)
})
 
module.exports = {
  addRemainingOptsAndPosArgs
}
 
function addRemaining (opt, opts) {
  const opts3 = []
 
  const opts2 = opt.opts
 
  for (let i = 0; i < opts.length; i++) {
    const opt3 = opts[i]
 
    if (isIn(opts2, opt3) > -1) {
      const opt4 = opts2.find(equals(opt3))
      const opt5 = isSubcommand(opt3) ? {...opt3, values: addRemaining(opt4, (opt3.values))} : opt3
      opts3.push(opt5)
    } else {
      opts3.push(opt3)
    }
  }
 
  for (let i = 0; i < opts2.length; i++) {
    const opt3 = opts2[i]
 
    if (isIn(opts, opt3) === -1) {
      opts3.push(opt3)
    }
  }
 
  return opts3
}
 
function isIn (opts, opt) {
  return opts.findIndex(equals(opt))
}
 
function equals (opt1) {
  return opt2 => (
    opt1.key === opt2.key &&
    arrayEquals(opt1.args, opt2.args) &&
    (typeof opt1.types === 'undefined' || typeof opt2.types === 'undefined' || arrayEquals(opt1.types, opt2.types)) &&
    (opt1.opts || []).length === (opt2.opts || []).length
  )
}
 
function arrayEquals (arr1, arr2) {
  return (
    (typeof arr1 === 'undefined' && typeof arr2 === 'undefined') || (
      Array.isArray(arr1) && Array.isArray(arr2) && 
      arr1.length === arr2.length &&
      arr1.reduce((bool, elem, i) => bool && elem === arr2[i], true)
    )
  )
}
 
function isSubcommand ({types, args, opts}) {
  return Array.isArray(args) && typeof types === 'undefined' && Array.isArray(opts)
}