Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class ArgError extends Error {
}
}

function isNumericType(type) {
return (
type === Number ||
type === parseInt ||
type === parseFloat ||
// eslint-disable-next-line no-undef
(typeof BigInt !== 'undefined' && type === BigInt)
);
}

function arg(
opts,
{
Expand Down Expand Up @@ -59,19 +69,22 @@ function arg(

let type = opts[key];
let isFlag = false;
let isNumeric = false;

if (
Array.isArray(type) &&
type.length === 1 &&
typeof type[0] === 'function'
) {
const [fn] = type;
isNumeric = isNumericType(fn);
type = (value, name, prev = []) => {
prev.push(fn(value, name, prev[prev.length - 1]));
return prev;
};
isFlag = fn === Boolean || fn[flagSymbol] === true;
} else if (typeof type === 'function') {
isNumeric = isNumericType(type);
isFlag = type === Boolean || type[flagSymbol] === true;
} else {
throw new ArgError(
Expand All @@ -87,7 +100,7 @@ function arg(
);
}

handlers[key] = [type, isFlag];
handlers[key] = [type, isFlag, isNumeric];
}

for (let i = 0, len = argv.length; i < len; i++) {
Expand Down Expand Up @@ -136,7 +149,7 @@ function arg(
}
}

const [type, isFlag] = handlers[argName];
const [type, isFlag, isNumeric] = handlers[argName];

if (!isFlag && j + 1 < separatedArguments.length) {
throw new ArgError(
Expand All @@ -152,12 +165,7 @@ function arg(
argv.length < i + 2 ||
(argv[i + 1].length > 1 &&
argv[i + 1][0] === '-' &&
!(
argv[i + 1].match(/^-?\d*(\.(?=\d))?\d*$/) &&
(type === Number ||
// eslint-disable-next-line no-undef
(typeof BigInt !== 'undefined' && type === BigInt))
))
!(argv[i + 1].match(/^-?\d*(\.(?=\d))?\d*$/) && isNumeric))
) {
const extended =
originalArgName === argName ? '' : ` (alias for ${argName})`;
Expand Down
56 changes: 56 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,62 @@ test('should parse negative numbers (separate argument form)', () => {
});
});

test('should parse negative numbers with parseInt', () => {
const argv = ['--int', '-100'];

const result = arg(
{
'--int': parseInt
},
{
argv
}
);

expect(result).to.deep.equal({
_: [],
'--int': -100
});
});

test('should parse negative numbers with parseFloat', () => {
const argv = ['--float', '-0.1'];

const result = arg(
{
'--float': parseFloat
},
{
argv
}
);

expect(result).to.deep.equal({
_: [],
'--float': -0.1
});
});

test('should parse negative numbers for numeric array types', () => {
const argv = ['--int', '-5', '--custom-int', '-10'];

const result = arg(
{
'--int': [Number],
'--custom-int': [parseInt]
},
{
argv
}
);

expect(result).to.deep.equal({
_: [],
'--int': [-5],
'--custom-int': [-10]
});
});

test('should error if numeric type is followed by non-negative, non-argument', () => {
const argv = ['--int', '-abc'];

Expand Down