Skip to content

Commit 4934b78

Browse files
committed
docs: add docs for rangeCheck function.
1 parent 78aa3bf commit 4934b78

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/utils.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import { DynamicBuffer } from './dynamicBuffer';
22

3-
export const rangeCheck = (field: string, value: number, min?: number, max?: number) => {
4-
if (min !== undefined && value < min) {
5-
throw RangeError(`The value of '${field}' is out of range. It must be >= ${min}. Received ${value}`);
6-
}
7-
8-
if (max !== undefined && value > max) {
9-
throw RangeError(`The value of '${field}' is out of range. It must be <= ${max}. Received ${value}`);
10-
}
11-
};
12-
133
/**
144
* Returns true if obj is a DynamicBuffer, false otherwise.
155
*
@@ -24,6 +14,24 @@ export const rangeCheck = (field: string, value: number, min?: number, max?: num
2414
*/
2515
export const isDynamicBuffer = (val: any) => val instanceof DynamicBuffer;
2616

17+
/**
18+
* Check the value in the required range, and throw an error if not satisfy the range requirement.
19+
*
20+
* @param field The field name.
21+
* @param value The value to check.
22+
* @param min The allowed minimum value (included) of this value.
23+
* @param max The allowed maximum value (included) of this value.
24+
*/
25+
export const rangeCheck = (field: string, value: number, min?: number, max?: number) => {
26+
if (min !== undefined && value < min) {
27+
throw RangeError(`The value of '${field}' is out of range. It must be >= ${min}. Received ${value}`);
28+
}
29+
30+
if (max !== undefined && value > max) {
31+
throw RangeError(`The value of '${field}' is out of range. It must be <= ${max}. Received ${value}`);
32+
}
33+
};
34+
2735
/**
2836
* Swap two element in the buffer.
2937
*/

0 commit comments

Comments
 (0)