UNPKG

1.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.empty = exports.bit = exports.inRange = exports.objectify = exports.dp = exports.multicastGroup = void 0;
4function multicastGroup(universe) {
5 if ((universe > 0 && universe <= 63999) || universe === 64214) {
6 // eslint-disable-next-line no-bitwise
7 return `239.255.${universe >> 8}.${universe & 255}`;
8 }
9 throw new RangeError('universe must be between 1-63999');
10}
11exports.multicastGroup = multicastGroup;
12exports.dp = (n, decimals = 2) => Math.round(n * Math.pow(10, decimals)) / Math.pow(10, decimals);
13function objectify(buf) {
14 const data = {};
15 buf.forEach((val, ch) => {
16 if (val > 0)
17 data[ch + 1] = exports.dp(val / 2.55, 2); // rounding to 2dp will not lose any data
18 });
19 return data;
20}
21exports.objectify = objectify;
22exports.inRange = (n) => Math.min(255, Math.max(Math.round(n), 0));
23function bit(bitt, num) {
24 // we could just do a _bit_ of shifting here instead :P
25 // e.g. (0x1234 >> 8) & 255
26 const arr = new ArrayBuffer(bitt / 8);
27 // this mutates `arr`
28 const view = new DataView(arr);
29 view[`setUint${bitt}`](0, num, false); // ByteOffset = 0; litteEndian = false
30 return Array.from(new Uint8Array(arr));
31}
32exports.bit = bit;
33exports.empty = (len) => Array.from(new Uint8Array(new ArrayBuffer(len)));