| 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 |
1×
1×
1×
1×
36×
36×
2×
34×
4×
30×
30×
1×
29×
4×
25×
25×
1×
24×
4×
1×
12×
1×
11×
2×
9×
1×
8×
2×
6×
1×
5×
2×
1×
6×
3×
1×
20×
19×
19×
1×
18×
3×
1×
32×
4×
1×
23×
10×
1×
37×
7×
1×
3×
1×
1×
43×
43×
6×
37×
3×
1×
6×
6×
6×
1×
5×
1×
4×
1×
16×
1×
15×
6×
9×
1×
5×
1×
5×
1×
5×
1×
38×
1×
26×
| 'use strict';
var _require = require('../lifx'),
constants = _require.constants;
var _require2 = require('util'),
format = _require2.format;
var validate = exports;
/**
* Checks validity of given color hue, saturation and brightness values
* @param {any} hue value to validate
* @param {any} saturation value to validate
* @param {any} brightness brightness value to validate
* @param {String} context validation context
*/
validate.colorHsb = function (hue, saturation, brightness, context) {
var hueMessage = 'LIFX %s expects hue to be a number between ' + constants.HSBK_MINIMUM_HUE + ' and ' + constants.HSBK_MAXIMUM_HUE;
if (typeof hue !== 'number') {
throwTypeError(hueMessage, context);
} else if (hue < constants.HSBK_MINIMUM_HUE || hue > constants.HSBK_MAXIMUM_HUE) {
throwRangeError(hueMessage, context);
}
var saturationMessage = 'LIFX %s expects saturation to be a number between ' + constants.HSBK_MINIMUM_SATURATION + ' and ' + constants.HSBK_MAXIMUM_SATURATION;
if (typeof saturation !== 'number') {
throwTypeError(saturationMessage, context);
} else if (saturation < constants.HSBK_MINIMUM_SATURATION || saturation > constants.HSBK_MAXIMUM_SATURATION) {
throwRangeError(saturationMessage, context);
}
var brightnessMessage = 'LIFX %s expects brightness to be a number between ' + constants.HSBK_MINIMUM_BRIGHTNESS + ' and ' + constants.HSBK_MAXIMUM_BRIGHTNESS;
if (typeof brightness !== 'number') {
throwTypeError(brightnessMessage, context);
} else if (brightness < constants.HSBK_MINIMUM_BRIGHTNESS || brightness > constants.HSBK_MAXIMUM_BRIGHTNESS) {
throwRangeError(brightnessMessage, context);
}
};
/**
* Checks validity of color RGB values
* @param {any} red Red value to validate
* @param {any} green Green value to validate
* @param {any} blue Blue value to validate
* @param {String} context validation context
*/
validate.colorRgb = function (red, green, blue, context) {
if (typeof red !== 'number') {
throwTypeError('LIFX %s expects first parameter red to a number', context);
}
if (red < constants.RGB_MINIMUM_VALUE || red > constants.RGB_MAXIMUM_VALUE) {
throwRangeError('LIFX %s expects first parameter red to be between 0 and 255', context);
}
if (typeof green !== 'number') {
throwTypeError('LIFX %s expects second parameter green to a number', context);
}
if (green < constants.RGB_MINIMUM_VALUE || green > constants.RGB_MAXIMUM_VALUE) {
throwRangeError('LIFX %s expects second parameter green to be between 0 and 255', context);
}
if (typeof blue !== 'number') {
throwTypeError('LIFX %s expects third parameter blue to a number', context);
}
if (blue < constants.RGB_MINIMUM_VALUE || blue > constants.RGB_MAXIMUM_VALUE) {
throw new RangeError('LIFX light colorRgb method expects third parameter blue to be between 0 and 255');
}
};
/**
* Checks validity of IR brightness
* @param {any} brightness IR brightness to validate
* @param {String} context validation context
*/
validate.irBrightness = function (brightness, context) {
if (typeof brightness !== 'number' || brightness < constants.IR_MINIMUM_BRIGHTNESS || brightness > constants.IR_MAXIMUM_BRIGHTNESS) {
throwRangeError('LIFX %s expects brightness to be a number between ' + constants.IR_MINIMUM_BRIGHTNESS + ' and ' + constants.IR_MAXIMUM_BRIGHTNESS, context);
}
};
/**
* Checks validity of an optional kelvin value
* @param {any} kelvin Kelvin value to validate
* @param {String} context validation context
*/
validate.optionalKelvin = function (kelvin, context) {
if (kelvin !== undefined) {
var message = `LIFX %s expects kelvin to be a number between ${constants.HSBK_MINIMUM_KELVIN} and ${constants.HSBK_MAXIMUM_KELVIN}`;
if (typeof kelvin !== 'number') {
throwTypeError(message, context);
} else if (kelvin < constants.HSBK_MINIMUM_KELVIN || kelvin > constants.HSBK_MAXIMUM_KELVIN) {
throwRangeError(message, context);
}
}
};
/**
* Checks validity of an optional transition time
* @param {any} duration Transition time to validate
* @param {String} context validation context
*/
validate.optionalDuration = function (duration, context) {
if (duration !== undefined && typeof duration !== 'number') {
throwTypeError('LIFX %s expects duration to be a number', context);
}
};
/**
* Checks validity of a callback function
* @param {any} callback Callback to validate
* @param {String} context validation context
*/
validate.callback = function (callback, context) {
if (typeof callback !== 'function') {
throwTypeError('LIFX %s expects callback to be a function', context);
}
};
/**
* Checks validity of an optional callback function
* @param {any} callback Callback to validate
* @param {String} context validation context
*/
validate.optionalCallback = function (callback, context) {
if (callback !== undefined && typeof callback !== 'function') {
throwTypeError('LIFX %s expects callback to be a function', context);
}
};
/**
* Checks validity of an optional boolean
* @param {any} value value to validate
* @param {any} parameter validated parameter name
* @param {String} context validation context
*/
validate.optionalBoolean = function (value, parameter, context) {
if (value !== undefined && typeof value !== 'boolean') {
throwTypeError('LIFX %s expects "%s" to be a boolean', context, parameter);
}
};
/**
* Checks validity of a light zone index
* @param {any} index Light zone index to validate
* @param {String} context validation context
*/
validate.zoneIndex = function (index, context) {
var zoneMessage = 'LIFX %s expects zone to be a number between ' + constants.ZONE_INDEX_MINIMUM_VALUE + ' and ' + constants.ZONE_INDEX_MAXIMUM_VALUE;
if (typeof index !== 'number') {
throwTypeError(zoneMessage, context);
} else if (index < constants.ZONE_INDEX_MINIMUM_VALUE || index > constants.ZONE_INDEX_MAXIMUM_VALUE) {
throwRangeError(zoneMessage, context);
}
};
/**
* Checks validity of an optional light zone index
* @param {any} index Light zone index to validate
* @param {String} context validation context
* @return {Boolean} const true or an exception
*/
validate.optionalZoneIndex = function (index, context) {
var zoneMessage = 'LIFX %s expects zone to be a number between ' + constants.ZONE_INDEX_MINIMUM_VALUE + ' and ' + constants.ZONE_INDEX_MAXIMUM_VALUE;
Eif (index !== undefined) {
if (typeof index !== 'number') {
throwTypeError(zoneMessage, context);
} else if (index < constants.ZONE_INDEX_MINIMUM_VALUE || index > constants.ZONE_INDEX_MAXIMUM_VALUE) {
throwRangeError(zoneMessage, context);
}
}
return true;
};
/**
* test if the given value is an uint value
* @param {Number} val the given uint value as number
* @param {String} context the string for the error message
* @param {Number} range the range of the uint value
* @return {Boolean} const true or an exception
*/
validate.isUIntRange = function (val, context, range) {
if (typeof val !== 'number') {
throwTypeError('LIFX %s expects "%s" to be a number', context, val);
}
if (!(val >= 0 && val <= range)) {
throw new RangeError(`LIFX ${context} expects "${val}" to be a number between 0 and ${range}`);
}
return true;
};
/**
* test if the given value is an uint8 value
* @param {Number} val the given uint8 value as number
* @param {String} context the string for the error message
* @return {Boolean} const true or an exception
*/
validate.isUInt8 = function (val, context) {
return validate.isUIntRange(val, context, 0xff);
};
/**
* test if the given value is an uint16 value
* @param {Number} val the given uint16 value as number
* @param {String} context the string for the error message
* @return {Boolean} const true or an exception
*/
validate.isUInt16 = function (val, context) {
return validate.isUIntRange(val, context, 0xffff);
};
/**
* test if the given value is an uint32 value
* @param {Number} val the given uint32 value as number
* @param {String} context the string for the error message
* @return {Boolean} const true or an exception
*/
validate.isUInt32 = function (val, context) {
return validate.isUIntRange(val, context, 0xffffffff);
};
/**
* Formats error message and throws a TypeError
* @param {String} message Error message
* @param {String} context Validation context
* @param {String} [parameter] Validated parameter name
*/
function throwTypeError(message, context, parameter) {
throw new TypeError(format(message, context, parameter));
}
/**
* Formats the error message and throws a RangeError
* @param {String} message Error message
* @param {String} context Validation context
* @param {String} [parameter] Validated parameter name
*/
function throwRangeError(message, context, parameter) {
throw new RangeError(format(message, context, parameter));
} |