All files / src/lifx validate.js

95.95% Statements 95/99
94.17% Branches 97/103
100% Functions 19/19
95.95% Lines 95/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286    1x 1x 1x                 1x 52x 52x 3x 49x 6x     43x   43x 2x 41x 6x     35x   35x 2x 33x 6x                     1x 12x 1x   11x 2x   9x 1x   8x 2x   6x 1x   5x 2x                 1x 6x 3x                   1x 27x 25x 25x 2x 23x 4x                   1x 32x 4x                 1x 30x 14x                 1x 48x 10x                   1x 8x 1x                   1x 15x                   1x 5x     5x 5x     5x                     1x 43x   43x 6x 37x 3x                   1x 6x   6x 6x 1x 5x 1x     4x               1x 19x   19x 4x 15x 6x                     1x 16x 1x   15x 6x   9x                 1x 5x                 1x 5x                 1x 5x                   53x                   39x    
'use strict';
 
const {constants} = require('../lifx');
const {format} = require('util');
const 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) {
  const 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);
  }
 
  const 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);
  }
 
  const 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) {
    const 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 an optional number
 * @param {any} value value to validate
 * @param {any} parameter validated parameter name
 * @param {String} context validation context
 */
validate.optionalNumber = function(value, parameter, context) {
  Iif (value !== undefined && typeof value !== 'number') {
    throwTypeError('LIFX %s expects "%s" to be a number', context, parameter);
  }
};
 
/**
 * Checks validity of an waveform id
 * @param {any} value value to validate
 * @param {String} context validation context
 */
validate.optionalWaveform = function(value, context) {
  Iif (value !== undefined && typeof value !== 'number') {
    throwTypeError('LIFX %s expects "%s" to be a number', context, 'waveform');
  }
  Eif (value !== undefined) {
    Iif (value < 0) {
      throwTypeError('LIFX %s expects "%s" to be >= 0', context, 'waveform');
    }
    Iif (value > 4) {
      throwTypeError('LIFX %s expects "%s" to be <= 4', context, 'waveform');
    }
  }
};
 
/**
 * 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) {
  const 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) {
  const 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;
};
 
/**
 * Checks validity of a relay index
 * @param {any} index Light relay index to validate
 * @param {String} context validation context
 */
validate.relayIndex = function(index, context) {
  const relayMessage = 'LIFX %s expects zone to be a number between ' +
    constants.RELAY_INDEX_MINIMUM_VALUE + ' and ' + constants.RELAY_INDEX_MAXIMUM_VALUE;
  if (typeof index !== 'number') {
    throwTypeError(relayMessage, context);
  } else if (index < constants.RELAY_INDEX_MINIMUM_VALUE || index > constants.RELAY_INDEX_MAXIMUM_VALUE) {
    throwRangeError(relayMessage, context);
  }
};
 
/**
 * 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));
}