Skip to content

Commit f508c25

Browse files
committed
fix: check type and NaN in checkBounds and checkRange.
1 parent e7c276e commit f508c25

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/utils.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export const isDynamicBuffer = (val: any) => val instanceof DynamicBuffer;
2424
* @param max The allowed maximum value (included) of this value.
2525
*/
2626
export const checkBounds = (field: string, value: number, min: number, max: number) => {
27-
if (value < min || value > max) {
27+
if (typeof value !== 'number') {
28+
throw new TypeError(`${field} must be a number`);
29+
}
30+
31+
if (Number.isNaN(value) || value < min || value > max) {
2832
throw new RangeError(`${field} is out of bounds`);
2933
}
3034
};
@@ -38,12 +42,35 @@ export const checkBounds = (field: string, value: number, min: number, max: numb
3842
* @param max The allowed maximum value (included) of this value.
3943
*/
4044
export const checkRange = (field: string, value: number, min?: number, max?: number) => {
41-
if (min !== undefined && value < min) {
42-
throw new RangeError(`The value of '${field}' is out of range. It must be >= ${min}. Received ${value}`);
45+
if (typeof value !== 'number') {
46+
throw new TypeError(`${field} must be a number`);
47+
}
48+
49+
const ranges: string[] = [];
50+
let isValid: boolean = true;
51+
52+
if (min !== undefined) {
53+
ranges.push(`>= ${min}`);
54+
if (value < min) {
55+
isValid = false;
56+
}
57+
}
58+
59+
if (max !== undefined) {
60+
ranges.push(`<= ${max}`);
61+
if (isValid && value > max) {
62+
isValid = false;
63+
}
4364
}
4465

45-
if (max !== undefined && value > max) {
46-
throw new RangeError(`The value of '${field}' is out of range. It must be <= ${max}. Received ${value}`);
66+
if (!isValid || Number.isNaN(value)) {
67+
throw new RangeError(`The value of '${
68+
field
69+
}' is out of range. It must be ${
70+
ranges.join(' and ')
71+
}. Received ${
72+
value
73+
}`);
4774
}
4875
};
4976

test/util.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ describe('Method isDynamicBuffer test', () => {
1818

1919
describe('Method checkBounds test', () => {
2020
it('Test checkBounds', () => {
21+
assert.throws(() => {
22+
// @ts-ignore
23+
checkBounds('notNumber', 'test', 0, 5);
24+
});
25+
2126
assert.doesNotThrow(() => {
2227
checkBounds('valid', 0, 0, 5);
2328
});
@@ -29,11 +34,20 @@ describe('Method checkBounds test', () => {
2934
assert.throws(() => {
3035
checkBounds('greater', 10, 0, 5);
3136
});
37+
38+
assert.throws(() => {
39+
checkBounds('NaN', NaN, 0, 5);
40+
});
3241
});
3342
});
3443

3544
describe('Method checkRange test', () => {
3645
it('Test checkRange', () => {
46+
assert.throws(() => {
47+
// @ts-ignore
48+
checkRange('notNum', 'test');
49+
});
50+
3751
assert.doesNotThrow(() => {
3852
checkRange('noLimit', 0);
3953
});
@@ -49,6 +63,10 @@ describe('Method checkRange test', () => {
4963
assert.throws(() => {
5064
checkRange('greater', 10, 0, 5);
5165
});
66+
67+
assert.throws(() => {
68+
checkRange('NaN', NaN, 0, 5);
69+
});
5270
});
5371
});
5472

0 commit comments

Comments
 (0)