name:
src/Utilities/ArgsChecker.ts
-rw-r--r--
2080
1import {
2 NAError
3} from "../Errors";
4
5/**
6 * Static class to check argument length within expected ranges when calling functions.
7 */
8class ArgsChecker {
9 /**
10 * Checks to see if the arguments are of the correct length.
11 * @param args to check length of
12 * @param length expected length
13 * @param caller name of the function calling this function, for use in error message formatting
14 */
15 static checkLength(args: Array<any> | IArguments, length: number, caller?: string) {
16 if (args.length !== length) {
17 let functionName = caller !== undefined ? " to " + caller : "";
18 throw new NAError("Wrong number of arguments" + functionName + ". Expected " + length
19 + " arguments, but got " + args.length + " arguments.");
20 }
21 }
22
23 /**
24 * Checks to see if the arguments are at least a certain length.
25 * @param args to check length of
26 * @param length expected length
27 * @param caller name of the function calling this function, for use in error message formatting
28 */
29 static checkAtLeastLength(args: any, length: number, caller?: string) {
30 if (args.length < length) {
31 let functionName = caller !== undefined ? " to " + caller : "";
32 throw new NAError("Wrong number of arguments" + functionName + ". Expected " + length
33 + " arguments, but got " + args.length + " arguments.");
34 }
35 }
36
37 /**
38 * Checks to see if the arguments are within a max and min, inclusively
39 * @param args to check length of
40 * @param low least number of arguments
41 * @param high max number of arguments
42 * @param caller name of the function calling this function, for use in error message formatting
43 */
44 static checkLengthWithin(args: any, low: number, high: number, caller?: string) {
45 if (args.length > high || args.length < low) {
46 let functionName = caller !== undefined ? " to " + caller : "";
47 throw new NAError("Wrong number of arguments" + functionName + ". Expected between " + low
48 + " and " + high + " arguments, but got " + args.length + " arguments.");
49 }
50 }
51}
52
53export {
54 ArgsChecker
55}