-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixValues.mjs
38 lines (31 loc) · 1.03 KB
/
fixValues.mjs
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
const inputPattern = /INPUT/iu,
fixableTypes = {
"INPUT:number"(value, optionField){
value = value || optionField.value;
if(Object.hasOwn(optionField, "step") && optionField.step !== "any"){
value = Math.round(value / optionField.step) * optionField.step;
}
if(Object.hasOwn(optionField, "min")){
value = Math.max(optionField.min, value);
}
if(Object.hasOwn(optionField, "max")){
value = Math.min(optionField.max, value);
}
return value;
}
};
export default ({ ...options }, { ...optionFields }) => Object.fromEntries(Object.entries(options)
.map(([ name, value ]) => {
const optionField = optionFields[name],
fixableType = optionField.nodeName.toUpperCase() + (inputPattern.test(optionField.nodeName)
? `:${optionField.type.toLowerCase()}`
: ""),
fixType = fixableTypes[fixableType];
if(fixType){
value = fixType(value, optionField);
}
return [
name,
value
];
}));