Skip to content

Commit

Permalink
chore: Minor refactor (#1396)
Browse files Browse the repository at this point in the history
* Minor refactor

* Minor fix

* tweak

* fix: lint

* minor fix

* remove eslint disable line
  • Loading branch information
jamesgeorge007 authored and mleguen committed Sep 18, 2019
1 parent 236e24e commit afaf6d3
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions lib/validation.js
Expand Up @@ -224,43 +224,36 @@ module.exports = function validation (yargs, usage, y18n) {
return implied
}

function keyExists (argv, val) {
// convert string '1' to number 1
let num = Number(val)
val = isNaN(num) ? val : num

if (typeof val === 'number') {
// check length of argv._
val = argv._.length >= val
} else if (val.match(/^--no-.+/)) {
// check if key/value doesn't exist
val = val.match(/^--no-(.+)/)[1]
val = !argv[val]
} else {
// check if key/value exists
val = argv[val]
}
return val
}

self.implications = function implications (argv) {
const implyFail = []

Object.keys(implied).forEach((key) => {
const origKey = key
;(implied[key] || []).forEach((value) => {
let num
let key = origKey
const origValue = value
key = keyExists(argv, key)
value = keyExists(argv, value)

// convert string '1' to number 1
num = Number(key)
key = isNaN(num) ? key : num

if (typeof key === 'number') {
// check length of argv._
key = argv._.length >= key
} else if (key.match(/^--no-.+/)) {
// check if key doesn't exist
key = key.match(/^--no-(.+)/)[1]
key = !argv[key]
} else {
// check if key exists
key = argv[key]
}

num = Number(value)
value = isNaN(num) ? value : num

if (typeof value === 'number') {
value = argv._.length >= value
} else if (value.match(/^--no-.+/)) {
value = value.match(/^--no-(.+)/)[1]
value = !argv[value]
} else {
value = argv[value]
}
if (key && !value) {
implyFail.push(` ${origKey} -> ${origValue}`)
}
Expand Down

0 comments on commit afaf6d3

Please sign in to comment.