/
ensureAllParameters.js
42 lines (39 loc) · 1.01 KB
/
ensureAllParameters.js
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
'use strict';
/**
* add possibly missing parameters in the array
* @param {*} device
* @returns
*/
function ensureAllParameters(device) {
const existingParameters = device.parameters;
let currentPosition = 0;
const parameters = [];
for (let parameter of existingParameters) {
if (!parameter.label) {
throw new Error(`missing label for ${JSON.stringify(parameter)}`);
}
const labelPosition = labelToNumber(parameter.label);
if (labelPosition < currentPosition) {
throw new Error(
`expectedPosition > currentPosition for ${JSON.stringify(parameter)}`,
);
}
while (labelPosition > currentPosition) {
parameters.push(undefined);
currentPosition++;
}
parameters.push(parameter);
currentPosition++;
}
device.parameters = parameters;
return device;
}
function labelToNumber(code) {
let value = 0;
for (let char of code) {
value *= 26;
value += char.charCodeAt(0) - 64;
}
return value - 1;
}
module.exports = ensureAllParameters;