Skip to content

Commit

Permalink
code: interprets single item Type Arrays as one
Browse files Browse the repository at this point in the history
Fixes: #66
Credit: @samjonester
Reviewed-By: @othiym23
PR-URL: #68
  • Loading branch information
samjonester authored and othiym23 committed Dec 13, 2016
1 parent c89d31a commit 0d95e90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,15 @@ function parse (args, data, remain, types, shorthands) {

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

var isTypeArray = Array.isArray(types[arg])
, isArray = types[arg] === Array ||
isTypeArray && types[arg].indexOf(Array) !== -1
var argType = types[arg]
var isTypeArray = Array.isArray(argType)
if (isTypeArray && argType.length === 1) {
isTypeArray = false
argType = argType[0]
}

var isArray = argType === Array ||
isTypeArray && argType.indexOf(Array) !== -1

// allow unknown things to be arrays if specified multiple times.
if (!types.hasOwnProperty(arg) && data.hasOwnProperty(arg)) {
Expand All @@ -298,12 +304,12 @@ function parse (args, data, remain, types, shorthands) {
, la = args[i + 1]

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

if (isBool) {
// just set and move along
Expand All @@ -318,21 +324,21 @@ function parse (args, data, remain, types, shorthands) {

// also support "foo":[Boolean, "bar"] and "--foo bar"
if (isTypeArray && la) {
if (~types[arg].indexOf(la)) {
if (~argType.indexOf(la)) {
// an explicit type
val = la
i ++
} else if ( la === "null" && ~types[arg].indexOf(null) ) {
} else if ( la === "null" && ~argType.indexOf(null) ) {
// null allowed
val = null
i ++
} else if ( !la.match(/^-{2,}[^-]/) &&
!isNaN(la) &&
~types[arg].indexOf(Number) ) {
~argType.indexOf(Number) ) {
// number
val = +la
i ++
} else if ( !la.match(/^-[^-]/) && ~types[arg].indexOf(String) ) {
} else if ( !la.match(/^-[^-]/) && ~argType.indexOf(String) ) {
// string
val = la
i ++
Expand All @@ -345,7 +351,7 @@ function parse (args, data, remain, types, shorthands) {
continue
}

if (types[arg] === String && la === undefined)
if (argType === String && la === undefined)
la = ""

if (la && la.match(/^-{2,}$/)) {
Expand Down
7 changes: 7 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ test("Empty String results in empty string, not true", function (t) {
t.end()
})

// https://github.com/npm/nopt/issues/66
test("Empty String should not be true when type is single item Array", function (t) {
var parsed = nopt({ 'foo': [String] }, {}, ["--foo"], 0)
t.same(parsed.foo, "")
t.end()
})

test("~ path is resolved to " + (isWin ? '%USERPROFILE%' : '$HOME'), function (t) {
var path = require("path")
, the
Expand Down

0 comments on commit 0d95e90

Please sign in to comment.