Skip to content

Commit 7bd3360

Browse files
committed
feat: add checkBounds util function.
1 parent 3407824 commit 7bd3360

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ import { DynamicBuffer } from './dynamicBuffer';
1414
*/
1515
export const isDynamicBuffer = (val: any) => val instanceof DynamicBuffer;
1616

17+
/**
18+
* Check the value is greater or equals to the minimum value, and less or equals to the maximum
19+
* value. It'll throw an error if the value is not in the range.
20+
*
21+
* @param field The field name.
22+
* @param value The value to check.
23+
* @param min The allowed minimum value (included) of this value.
24+
* @param max The allowed maximum value (included) of this value.
25+
*/
26+
export const checkBounds = (field: string, value: number, min: number, max: number) => {
27+
if (value < min || value > max) {
28+
throw new RangeError(`${field} is out of bounds`);
29+
}
30+
};
31+
1732
/**
1833
* Check the value in the required range, and throw an error if not satisfy the range requirement.
1934
*
@@ -24,11 +39,11 @@ export const isDynamicBuffer = (val: any) => val instanceof DynamicBuffer;
2439
*/
2540
export const checkRange = (field: string, value: number, min?: number, max?: number) => {
2641
if (min !== undefined && value < min) {
27-
throw RangeError(`The value of '${field}' is out of range. It must be >= ${min}. Received ${value}`);
42+
throw new RangeError(`The value of '${field}' is out of range. It must be >= ${min}. Received ${value}`);
2843
}
2944

3045
if (max !== undefined && value > max) {
31-
throw RangeError(`The value of '${field}' is out of range. It must be <= ${max}. Received ${value}`);
46+
throw new RangeError(`The value of '${field}' is out of range. It must be <= ${max}. Received ${value}`);
3247
}
3348
};
3449

test/util.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import assert from 'assert';
22
import { describe, it } from 'mocha';
33

44
import { DynamicBuffer } from '../src';
5-
import { isDynamicBuffer, checkRange, swap } from '../src/utils';
5+
import {
6+
isDynamicBuffer,
7+
checkBounds,
8+
checkRange,
9+
swap,
10+
} from '../src/utils';
611

712
describe('Method isDynamicBuffer test', () => {
813
it('Test isDynamicBuffer util method', () => {
@@ -11,6 +16,22 @@ describe('Method isDynamicBuffer test', () => {
1116
});
1217
});
1318

19+
describe('Method checkBounds test', () => {
20+
it('Test checkBounds', () => {
21+
assert.doesNotThrow(() => {
22+
checkBounds('valid', 0, 0, 5);
23+
});
24+
25+
assert.throws(() => {
26+
checkBounds('less', -5, 0, 5);
27+
});
28+
29+
assert.throws(() => {
30+
checkBounds('greater', 10, 0, 5);
31+
});
32+
});
33+
});
34+
1435
describe('Method checkRange test', () => {
1536
it('Test checkRange', () => {
1637
assert.doesNotThrow(() => {

0 commit comments

Comments
 (0)