ibm-appconfiguration-js-client-sdk
Version:
IBM Cloud App Configuration JavaScript Client SDK
119 lines (118 loc) • 4.24 kB
JavaScript
;
/**
* Copyright 2022 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var Rule = /** @class */ (function () {
function Rule(_a) {
var attribute_name = _a.attribute_name, operator = _a.operator, values = _a.values;
this.attribute_name = attribute_name;
this.operator = operator;
this.values = values;
}
Rule.prototype.operatorCheck = function (key, value) {
var result = false;
if (key === undefined || key === null || value === '') {
return result;
}
var reg;
switch (this.operator) {
case 'endsWith':
reg = new RegExp("".concat(value, "$"), 'i');
result = reg.test(key);
break;
case 'notEndsWith':
reg = new RegExp("".concat(value, "$"), 'i');
result = !reg.test(key);
break;
case 'startsWith':
reg = new RegExp("^".concat(value), 'i');
result = reg.test(key);
break;
case 'notStartsWith':
reg = new RegExp("^".concat(value), 'i');
result = !reg.test(key);
break;
case 'contains':
result = key.includes(value);
break;
case 'notContains':
result = !key.includes(value);
break;
case 'is':
if (typeof (key) === 'number') {
result = (key === parseFloat(value));
}
else {
result = (key.toString() === value.toString());
}
break;
case 'isNot':
if (typeof (key) === 'number') {
result = (key !== parseFloat(value));
}
else {
result = (key.toString() !== value.toString());
}
break;
case 'greaterThan':
result = (parseFloat(key) > parseFloat(value));
break;
case 'lesserThan':
result = (parseFloat(key) < parseFloat(value));
break;
case 'greaterThanEquals':
result = (parseFloat(key) >= parseFloat(value));
break;
case 'lesserThanEquals':
result = (parseFloat(key) <= parseFloat(value));
break;
default:
// unknown type
result = false;
}
return result;
};
Rule.prototype.evaluateRule = function (entityAttributes) {
var key;
var result = false;
if (Object.prototype.hasOwnProperty.call(entityAttributes, this.attribute_name)) {
key = entityAttributes[this.attribute_name];
}
else {
return result;
}
if (['isNot', 'notContains', 'notStartsWith', 'notEndsWith'].includes(this.operator)) {
result = true;
for (var index = 0; index < this.values.length; index += 1) {
var value = this.values[index];
if (!this.operatorCheck(key, value)) {
result = false;
}
}
}
else {
for (var index = 0; index < this.values.length; index += 1) {
var value = this.values[index];
if (this.operatorCheck(key, value)) {
result = true;
}
}
}
return result;
};
return Rule;
}());
exports.default = Rule;