Skip to content

Commit e3f239d

Browse files
jimmyololukeed
authored andcommitted
support zero as flag value (lukeed#6)
* add isNum function * simplify zero-value fix
1 parent 22df50c commit e3f239d

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function toBool(any) {
2121
}
2222

2323
function toNum(any) {
24-
return (!isBool(any) && Number(any)) || any;
24+
let x = Number(any);
25+
return !isBool(any) && (x * 0 === 0) ? x : any;
2526
}
2627

2728
function getAlibi(names, arr) {

test/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ test('comprehensive', t => {
2525
'--bool',
2626
'--no-meep',
2727
'--multi=baz',
28+
'--number=-123',
29+
'--zeronum=0',
2830
'--',
2931
'--not-a-flag',
3032
'eek'
@@ -41,6 +43,8 @@ test('comprehensive', t => {
4143
multi: ['quux', 'baz'],
4244
meep: false,
4345
name: 'meowmers',
46+
number: -123,
47+
zeronum: 0,
4448
_: ['bare', '--not-a-flag', 'eek']
4549
}
4650
);

0 commit comments

Comments
 (0)