Skip to content

Commit

Permalink
code: store Array.isArray(types[arg]) for reuse
Browse files Browse the repository at this point in the history
Credit: @elidoran
Reviewed-By: @othiym23
PR-URL: #75
  • Loading branch information
elidoran authored and othiym23 committed Dec 13, 2016
1 parent 3370ce8 commit 356e58e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ function parse (args, data, remain, types, shorthands) {

if (abbrevs[arg]) arg = abbrevs[arg]

var isArray = types[arg] === Array ||
Array.isArray(types[arg]) && types[arg].indexOf(Array) !== -1
var isTypeArray = Array.isArray(types[arg])
, isArray = types[arg] === Array ||
isTypeArray && types[arg].indexOf(Array) !== -1

// allow unknown things to be arrays if specified multiple times.
if (!types.hasOwnProperty(arg) && data.hasOwnProperty(arg)) {
Expand All @@ -292,11 +293,11 @@ function parse (args, data, remain, types, shorthands) {

var isBool = typeof no === 'boolean' ||
types[arg] === Boolean ||
Array.isArray(types[arg]) && types[arg].indexOf(Boolean) !== -1 ||
isTypeArray && types[arg].indexOf(Boolean) !== -1 ||
(typeof types[arg] === 'undefined' && !hadEq) ||
(la === "false" &&
(types[arg] === null ||
Array.isArray(types[arg]) && ~types[arg].indexOf(null)))
isTypeArray && ~types[arg].indexOf(null)))

if (isBool) {
// just set and move along
Expand All @@ -310,7 +311,7 @@ function parse (args, data, remain, types, shorthands) {
}

// also support "foo":[Boolean, "bar"] and "--foo bar"
if (Array.isArray(types[arg]) && la) {
if (isTypeArray && la) {
if (~types[arg].indexOf(la)) {
// an explicit type
val = la
Expand Down

0 comments on commit 356e58e

Please # to comment.