UNPKG

1.2 kBPlain TextView Raw
1export function multicastGroup(universe: number): string {
2 if ((universe > 0 && universe <= 63999) || universe === 64214) {
3 // eslint-disable-next-line no-bitwise
4 return `239.255.${universe >> 8}.${universe & 255}`;
5 }
6 throw new RangeError('universe must be between 1-63999');
7}
8
9export const dp = (n: number, decimals = 2): number =>
10 Math.round(n * 10 ** decimals) / 10 ** decimals;
11
12export function objectify(buf: Buffer): Record<number, number> {
13 const data = {};
14 buf.forEach((val, ch) => {
15 if (val > 0) data[ch + 1] = dp(val / 2.55, 2); // rounding to 2dp will not lose any data
16 });
17 return data;
18}
19
20export const inRange = (n: number): number =>
21 Math.min(255, Math.max(Math.round(n), 0));
22
23export function bit(bitt: 8 | 16 | 32, num: number): number[] {
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
28 // this mutates `arr`
29 const view = new DataView(arr);
30 view[`setUint${bitt}`](0, num, false); // ByteOffset = 0; litteEndian = false
31
32 return Array.from(new Uint8Array(arr));
33}
34export const empty = (len: number): number[] =>
35 Array.from(new Uint8Array(new ArrayBuffer(len)));