-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Check.js
150 lines (136 loc) · 4.61 KB
/
Check.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*global define*/
define([
'./defaultValue',
'./defined',
'./DeveloperError',
'./isArray'
], function(
defaultValue,
defined,
DeveloperError,
isArray) {
'use strict';
/**
* Contains functions for checking that supplied arguments are of a specified type
* or meet specified conditions
* @private
*/
var Check = {};
/**
* Contains type checking functions, all using the typeof operator
*/
Check.typeOf = {};
/**
* Contains functions for checking numeric conditions such as minimum and maximum values
*/
Check.numeric = {};
function getUndefinedErrorMessage(name) {
return name + ' was required but undefined.';
}
function getFailedTypeErrorMessage(actual, expected, name) {
return 'Expected ' + name + ' to be typeof ' + expected + ', got ' + actual;
}
/**
* Throws if test is not defined
*
* @param {*} test The value that is to be checked
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be defined
*/
Check.defined = function (test, name) {
if (!defined(test)) {
throw new DeveloperError(getUndefinedErrorMessage(name));
}
};
/**
* Throws if test is greater than maximum
*
* @param {Number} test The value to test
* @param {Number} maximum The maximum allowed value
* @exception {DeveloperError} test must not be greater than maximum
* @exception {DeveloperError} Both test and maximum must be typeof 'number'
*/
Check.numeric.maximum = function (test, maximum) {
Check.typeOf.number(test);
Check.typeOf.number(maximum);
if (test > maximum) {
throw new DeveloperError('Expected ' + test + ' to be at most ' + maximum);
}
};
/**
* Throws if test is less than minimum
*
* @param {Number} test The value to test
* @param {Number} minimum The minimum allowed value
* @exception {DeveloperError} test must not be less than mininum
* @exception {DeveloperError} Both test and maximum must be typeof 'number'
*/
Check.numeric.minimum = function (test, minimum) {
Check.typeOf.number(test);
Check.typeOf.number(minimum);
if (test < minimum) {
throw new DeveloperError('Expected ' + test + ' to be at least ' + minimum);
}
};
/**
* Throws if test is not typeof 'function'
*
* @param {*} test The value to test
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be typeof 'function'
*/
Check.typeOf.function = function (test, name) {
if (typeof test !== 'function') {
throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
}
};
/**
* Throws if test is not typeof 'string'
*
* @param {*} test The value to test
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be typeof 'string'
*/
Check.typeOf.string = function (test, name) {
if (typeof test !== 'string') {
throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
}
};
/**
* Throws if test is not typeof 'number'
*
* @param {*} test The value to test
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be typeof 'number'
*/
Check.typeOf.number = function (test, name) {
if (typeof test !== 'number') {
throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
}
};
/**
* Throws if test is not typeof 'object'
*
* @param {*} test The value to test
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be typeof 'object'
*/
Check.typeOf.object = function (test, name) {
if (typeof test !== 'object') {
throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
}
};
/**
* Throws if test is not typeof 'boolean'
*
* @param {*} test The value to test
* @param {String} name The name of the variable being tested
* @exception {DeveloperError} test must be typeof 'boolean'
*/
Check.typeOf.boolean = function (test, name) {
if (typeof test !== 'boolean') {
throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
}
};
return Check;
});