@pawel-up/jexl
Version:
Javascript Expression Language: Powerful context-based expression parser and evaluator
116 lines • 4.01 kB
JavaScript
export const and = (a, b) => Boolean(a) && Boolean(b);
export const or = (a, b) => Boolean(a) || Boolean(b);
export const not = (a) => !a;
export const xor = (a, b) => Boolean(a) !== Boolean(b);
export const eq = (a, b) => a == b;
export const strictEq = (a, b) => a === b;
export const ne = (a, b) => a != b;
export const strictNe = (a, b) => a !== b;
export const gt = (a, b) => a > b;
export const gte = (a, b) => a >= b;
export const lt = (a, b) => a < b;
export const lte = (a, b) => a <= b;
export const between = (value, min, max) => value >= min && value <= max;
export const inOp = (value, collection) => {
if (typeof collection === 'string') {
return collection.includes(String(value));
}
return Array.isArray(collection) && collection.includes(value);
};
export const notIn = (value, collection) => {
return !inOp(value, collection);
};
export const like = (str, pattern) => {
const regexPattern = pattern
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
.replace(/%/g, '.*');
const regex = new RegExp(`^${regexPattern}$`, 'i');
return regex.test(str);
};
export const notLike = (str, pattern) => {
return !like(str, pattern);
};
export const regex = (str, pattern, flags = 'g') => {
const regex = new RegExp(pattern, flags);
return regex.test(str);
};
export const isNull = (value) => value === null || value === undefined;
export const isNotNull = (value) => value !== null && value !== undefined;
export const isEmpty = (value) => {
if (value === null || value === undefined)
return true;
if (typeof value === 'string')
return value.length === 0;
if (Array.isArray(value))
return value.length === 0;
if (typeof value === 'object')
return Object.keys(value).length === 0;
return false;
};
export const isNotEmpty = (value) => !isEmpty(value);
export const isType = (value, type) => {
switch (type.toLowerCase()) {
case 'string':
return typeof value === 'string';
case 'number':
return typeof value === 'number' && !isNaN(value);
case 'boolean':
return typeof value === 'boolean';
case 'object':
return typeof value === 'object' && value !== null && !Array.isArray(value);
case 'array':
return Array.isArray(value);
case 'function':
return typeof value === 'function';
case 'null':
return value === null || value === undefined;
case 'undefined':
return value === undefined;
default:
return false;
}
};
export const ifElse = (condition, trueValue, falseValue) => {
return condition ? trueValue : falseValue;
};
export const coalesce = (value, defaultValue) => {
return value !== null && value !== undefined ? value : defaultValue;
};
export const safeGet = (obj, path) => {
if (!obj || typeof obj !== 'object')
return undefined;
const keys = path.split('.');
let current = obj;
for (const key of keys) {
if (current === null || current === undefined)
return undefined;
current = current[key];
}
return current;
};
export const defaultTo = (...args) => {
for (const arg of args) {
if (arg !== null && arg !== undefined)
return arg;
}
return undefined;
};
export const inRange = (value, ...ranges) => {
return ranges.some(([min, max]) => value >= min && value <= max);
};
export const equalsAny = (value, ...values) => {
return values.includes(value);
};
export const notEqualsAny = (value, ...values) => {
return !values.includes(value);
};
export const equalsAll = (value, ...values) => {
return values.every((v) => v === value);
};
export const hasProperty = (obj, property) => {
return typeof obj === 'object' && obj !== null && property in obj;
};
export const instanceOf = (obj, constructor) => {
return obj instanceof constructor;
};
//# sourceMappingURL=operators.js.map