2019-09-10 00:19:49 +00:00
|
|
|
/*
|
2020-01-01 17:00:00 +00:00
|
|
|
* Copyright (C) 2019-2020 Alex Yatskov <alex@foosoft.net>
|
2019-09-10 00:19:49 +00:00
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-10 00:19:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
function conditionsValidateOptionValue(object, value) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(object, 'validate') && !object.validate(value)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid value for condition');
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(object, 'transform')) {
|
2019-09-10 00:19:49 +00:00
|
|
|
value = object.transform(value);
|
|
|
|
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(object, 'validateTransformed') && !object.validateTransformed(value)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid value for condition');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function conditionsNormalizeOptionValue(descriptors, type, operator, optionValue) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(descriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid type');
|
|
|
|
}
|
|
|
|
|
|
|
|
const conditionDescriptor = descriptors[type];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(conditionDescriptor.operators, operator)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid operator');
|
|
|
|
}
|
|
|
|
|
|
|
|
const operatorDescriptor = conditionDescriptor.operators[operator];
|
|
|
|
|
|
|
|
let transformedValue = conditionsValidateOptionValue(conditionDescriptor, optionValue);
|
|
|
|
transformedValue = conditionsValidateOptionValue(operatorDescriptor, transformedValue);
|
|
|
|
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(operatorDescriptor, 'transformReverse')) {
|
2019-09-10 00:19:49 +00:00
|
|
|
transformedValue = operatorDescriptor.transformReverse(transformedValue);
|
|
|
|
}
|
|
|
|
return transformedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function conditionsTestValueThrowing(descriptors, type, operator, optionValue, value) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(descriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid type');
|
|
|
|
}
|
|
|
|
|
|
|
|
const conditionDescriptor = descriptors[type];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(conditionDescriptor.operators, operator)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
throw new Error('Invalid operator');
|
|
|
|
}
|
|
|
|
|
|
|
|
const operatorDescriptor = conditionDescriptor.operators[operator];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(operatorDescriptor, 'transform')) {
|
|
|
|
if (hasOwn(operatorDescriptor, 'transformCache')) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const key = `${optionValue}`;
|
|
|
|
const transformCache = operatorDescriptor.transformCache;
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(transformCache, key)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
optionValue = transformCache[key];
|
|
|
|
} else {
|
|
|
|
optionValue = operatorDescriptor.transform(optionValue);
|
|
|
|
transformCache[key] = optionValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
optionValue = operatorDescriptor.transform(optionValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return operatorDescriptor.test(value, optionValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
function conditionsTestValue(descriptors, type, operator, optionValue, value) {
|
|
|
|
try {
|
|
|
|
return conditionsTestValueThrowing(descriptors, type, operator, optionValue, value);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function conditionsClearCaches(descriptors) {
|
|
|
|
for (const type in descriptors) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(descriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conditionDescriptor = descriptors[type];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptor, 'transformCache')) {
|
2019-09-10 00:19:49 +00:00
|
|
|
conditionDescriptor.transformCache = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const operatorDescriptors = conditionDescriptor.operators;
|
|
|
|
for (const operator in operatorDescriptors) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(operatorDescriptors, operator)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const operatorDescriptor = operatorDescriptors[operator];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(operatorDescriptor, 'transformCache')) {
|
2019-09-10 00:19:49 +00:00
|
|
|
operatorDescriptor.transformCache = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|