Skip to content

Commit f0f272b

Browse files
committed
readline: refactor to use validateNumber
`validateNumber` throws more proper error code and error name.
1 parent 7d80ca3 commit f0f272b

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/internal/readline/interface.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
MathMax,
2020
MathMaxApply,
2121
NumberIsFinite,
22-
NumberIsNaN,
2322
ObjectSetPrototypeOf,
2423
RegExpPrototypeExec,
2524
RegExpPrototypeSymbolReplace,
@@ -45,6 +44,7 @@ const {
4544
const {
4645
validateAbortSignal,
4746
validateArray,
47+
validateNumber,
4848
validateString,
4949
validateUint32,
5050
} = require('internal/validators');
@@ -199,13 +199,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
199199
historySize = kHistorySize;
200200
}
201201

202-
if (
203-
typeof historySize !== 'number' ||
204-
NumberIsNaN(historySize) ||
205-
historySize < 0
206-
) {
207-
throw new ERR_INVALID_ARG_VALUE.RangeError('historySize', historySize);
208-
}
202+
validateNumber(historySize, 'historySize', 0);
209203

210204
// Backwards compat; check the isTTY prop of the output stream
211205
// when `terminal` was not specified

test/parallel/test-readline-interface.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
126126
});
127127

128128
// Constructor throws if historySize is not a positive number
129-
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
129+
[-1, NaN].forEach((historySize) => {
130130
assert.throws(() => {
131131
readline.createInterface({
132132
input,
133133
historySize,
134134
});
135135
}, {
136136
name: 'RangeError',
137-
code: 'ERR_INVALID_ARG_VALUE'
137+
code: 'ERR_OUT_OF_RANGE'
138+
});
139+
});
140+
141+
// Constructor throws if type of historySize is not a number
142+
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
143+
assert.throws(() => {
144+
readline.createInterface({
145+
input,
146+
historySize,
147+
});
148+
}, {
149+
name: 'TypeError',
150+
code: 'ERR_INVALID_ARG_TYPE'
138151
});
139152
});
140153

test/parallel/test-readline-promises-interface.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
103103
});
104104

105105
// Constructor throws if historySize is not a positive number
106-
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
106+
[-1, NaN].forEach((historySize) => {
107107
assert.throws(() => {
108108
readline.createInterface({
109109
input,
110110
historySize,
111111
});
112112
}, {
113113
name: 'RangeError',
114-
code: 'ERR_INVALID_ARG_VALUE'
114+
code: 'ERR_OUT_OF_RANGE'
115+
});
116+
});
117+
118+
// Constructor throws if type of historySize is not a number
119+
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
120+
assert.throws(() => {
121+
readline.createInterface({
122+
input,
123+
historySize,
124+
});
125+
}, {
126+
name: 'TypeError',
127+
code: 'ERR_INVALID_ARG_TYPE'
115128
});
116129
});
117130

0 commit comments

Comments
 (0)