All files / src/foo/toOpts verifyCommand.js

100% Statements 56/56
100% Branches 47/47
100% Functions 17/17
100% Lines 48/48

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                      3x     68x   68x 65x   65x 65x   65x   65x 2x 63x 9x         54x     3x       3x         65x 65x   65x 1x   64x 73x                   73x 31x 23x 17x 1x 1x         31x   31x 31x 31x   31x             23x   23x   23x             17x   17x 17x   17x             48x       96x       82x       54x       70x             19x             73x             42x             378x       262x  
const {
  CommandExpected,
  InvalidArgs,
  InvalidKey,
  InvalidNestedCommand,
  InvalidOpts,
  InvalidTypes,
  OptionExpected,
  PosArgExpected,
  SubcommandExpected,
  UnknownCommandLineOptionType
} = require('../errors')
 
function verifyCommand (opt) {
  const err = CommandExpected({opt})
 
  if (isCommand(opt)) {
    const errs = []
 
    if (invalidKey(opt))  errs.push(InvalidKey({opt}))
    if (invalidOpts(opt)) errs.push(InvalidOpts({opt}))
 
    const {errs: errs2, opts} = verifyOpts({errs, opts: [opt]})
 
    if (errs.length > 0) {
      return {errs: [err, ...errs]}
    } else if (errs2.length > 0) {
      return {
        errs: errs2,
        opt: {...opt, opts}
      }
    } else {
      return {errs: [], opt}
    }
  } else {
    return {errs: [err]}
  }
}
 
module.exports = {
  verifyCommand
}
 
function verifyOpts ({errs, opts}) {
  const [opt] = opts
  const {opts: opts2} = opt
 
  if (!Array.isArray(opts2)) {
    return {errs: [...errs, InvalidOpts({opt})], opts: []}
  } else {
    return opts2.map(verifyOpt).reduce(
      ({errs, opts}, {errs: errs2, opts: opts2}) => ({
        errs: [...errs, ...errs2],
        opts: [...opts, ...opts2]
      }),
      {errs, opts: []}
    )
  }
}
 
function verifyOpt (opt) {
  switch (true) {
    case isOption(opt):     return verifyOption(opt)
    case isPosArg(opt):     return verifyPosArg(opt)
    case isSubcommand(opt): return verifySubcommand(opt)
    case isCommand(opt):    return {errs: [InvalidNestedCommand({opt})],         opts: []}
    default:                return {errs: [UnknownCommandLineOptionType({opt})], opts: []}
  }
}
 
function verifyOption (opt) {
  const errs = []
 
  if (invalidKey(opt))   errs.push(InvalidKey({opt}))
  if (invalidArgs(opt))  errs.push(InvalidArgs({opt}))
  if (invalidTypes(opt)) errs.push(InvalidTypes({opt}))
 
  return (
    errs.length > 0 ? {errs: [OptionExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                               opts: [opt]}
  )
}
 
function verifyPosArg (opt) {
  const errs = []
 
  if (invalidTypes(opt)) errs.push(InvalidTypes({opt}))
 
  return (
    errs.length > 0 ? {errs: [PosArgExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                               opts: [opt]}
  )
}
 
function verifySubcommand (opt) {
  const errs = []
 
  if (invalidArgs(opt)) errs.push(InvalidArgs({opt}))
  if (invalidOpts(opt)) errs.push(InvalidOpts({opt}))
 
  return (
    errs.length > 0 ? {errs: [SubcommandExpected({opt}), ...errs], opts: []   }
                    : {errs: [],                                   opts: [opt]}
  )
}
 
function invalidArgs ({args}) {
  return !Array.isArray(args) || args.length === 0
}
 
function invalidKey ({key}) {
  return typeof key !== 'string'
}
 
function invalidOpts ({opts}) {
  return !Array.isArray(opts)
}
 
function invalidTypes ({types}) {
  return !Array.isArray(types) && typeof types !== 'undefined'
}
 
function isCommand ({key, opts, args, types}) {
  return (
    [key, opts].every(isDefined) &&
    [args, types].every(isUndefined)
  )
}
 
function isSubcommand ({key, opts, args, types}) {
  return (
    [key, args, opts].every(isDefined) &&
    [types].every(isUndefined)
  )
}
 
function isOption ({key, args, opts}) {
  return (
    [key, args].every(isDefined) &&
    [opts].every(isUndefined)
  )
}
 
function isPosArg ({key, args, opts}) {
  return (
    [key].every(isDefined) &&
    [args, opts].every(isUndefined)
  )
}
 
function isDefined (val) {
  return typeof val !== 'undefined'
}
 
function isUndefined (val) {
  return typeof val === 'undefined'
}