node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
74 lines (73 loc) • 2.18 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleComparison = simpleComparison;
const const_1 = require("../../const");
const assert_1 = require("../../helpers/assert");
function isEqual(a, b) {
// eslint-disable-next-line eqeqeq
return a == b;
}
function isNotEqual(a, b) {
// eslint-disable-next-line eqeqeq
return a != b;
}
function contains(a, b) {
if (!Array.isArray(a) || !(0, assert_1.isString)(b)) {
return false;
}
return a.includes(b);
}
function doesNotContain(a, b) {
return !operators[const_1.ComparatorType.Contains](a, b);
}
function includes(a, b) {
if (!(0, assert_1.isString)(a) || !(0, assert_1.isString)(b)) {
return false;
}
const list = b.split(',').map((item) => item.trim());
return list.includes(a);
}
function doesNotInclude(a, b) {
return !operators[const_1.ComparatorType.Includes](a, b);
}
function startsWith(a, b) {
if (!(0, assert_1.isString)(a) || !(0, assert_1.isString)(b)) {
return false;
}
return a.startsWith(b);
}
function isNull(a) {
return a === null;
}
function isNotNull(a) {
return a !== null;
}
function isTrue(a) {
return a === true;
}
function isFalse(a) {
return a === false;
}
function regex(a, b, c) {
if (!(0, assert_1.isString)(a) || !(0, assert_1.isString)(b) || !(0, assert_1.isBoolean)(c)) {
return false;
}
return new RegExp(b, c ? 'i' : '').test(a);
}
const operators = {
[const_1.ComparatorType.Is]: isEqual,
[const_1.ComparatorType.IsNot]: isNotEqual,
[const_1.ComparatorType.Contains]: contains,
[const_1.ComparatorType.DoesNotContain]: doesNotContain,
[const_1.ComparatorType.Includes]: includes,
[const_1.ComparatorType.DoesNotInclude]: doesNotInclude,
[const_1.ComparatorType.StartsWith]: startsWith,
[const_1.ComparatorType.IsNull]: isNull,
[const_1.ComparatorType.IsNotNull]: isNotNull,
[const_1.ComparatorType.IsTrue]: isTrue,
[const_1.ComparatorType.IsFalse]: isFalse,
[const_1.ComparatorType.Regex]: regex,
};
function simpleComparison(operator, a, b, c) {
return operators[operator](a, b, c);
}
;