Skip to content

Commit

Permalink
readline: refactor to use validateNumber
Browse files Browse the repository at this point in the history
`validateNumber` throws more proper error code and
error name.
  • Loading branch information
deokjinkim committed Dec 9, 2022
1 parent 7d80ca3 commit 2379621
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
9 changes: 2 additions & 7 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
const {
validateAbortSignal,
validateArray,
validateNumber,
validateString,
validateUint32,
} = require('internal/validators');
Expand Down Expand Up @@ -199,13 +200,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
historySize = kHistorySize;
}

if (
typeof historySize !== 'number' ||
NumberIsNaN(historySize) ||
historySize < 0
) {
throw new ERR_INVALID_ARG_VALUE.RangeError('historySize', historySize);
}
validateNumber(historySize, 'historySize', 0);

// Backwards compat; check the isTTY prop of the output stream
// when `terminal` was not specified
Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
});

// Constructor throws if historySize is not a positive number
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
[-1, NaN].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
code: 'ERR_OUT_OF_RANGE'
});
});

// Constructor throws if type of historySize is not a number
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
});
});

Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-readline-promises-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
});

// Constructor throws if historySize is not a positive number
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
[-1, NaN].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
code: 'ERR_OUT_OF_RANGE'
});
});

// Constructor throws if type of historySize is not a number
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
});
});

Expand Down

0 comments on commit 2379621

Please sign in to comment.