All files / src/lifx/packets setInfrared.js

57.89% Statements 11/19
28.57% Branches 2/7
50% Functions 1/2
57.89% Lines 11/19

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    1x                 1x                                       1x 2x 2x 2x   2x     2x 2x   2x     1x  
'use strict';
 
const Packet = {
  size: 2
};
 
/**
 * Converts packet specific data from a buffer to an object
 * @param  {Buffer} buf Buffer containing only packet specific data no header
 * @return {Object}     Information contained in packet
 */
Packet.toObject = function(buf) {
  const obj = {};
  let offset = 0;
 
  if (buf.length !== this.size) {
    throw new Error('Invalid length given for setInfrared LIFX packet');
  }
 
  obj.brightness = buf.readUInt16LE(offset);
  offset += 2;
 
  return obj;
};
 
/**
 * Converts the given packet specific object into a packet
 * @param  {Object} obj object with configuration data
 * @param  {Number} obj.brightness between 0 and 65535
 * @return {Buffer} packet
 */
Packet.toBuffer = function(obj) {
  const buf = Buffer.alloc(this.size);
  buf.fill(0);
  let offset = 0;
 
  Iif (typeof obj.brightness !== 'number' && obj.brightness < 0 && obj.brightness > 65535) {
    throw new RangeError('Invalid brightness given for setInfrared LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.brightness, offset);
  offset += 2;
 
  return buf;
};
 
module.exports = Packet;