@opengis/fastify-table
Version:
core-plugins
97 lines (91 loc) • 3.06 kB
JavaScript
function intersect(a, b) {
const aN = Array.isArray(a) ? a : typeof a === 'string' ? a.split(',') : [];
const bN = Array.isArray(b) ? b : typeof b === 'string' ? b.split(',') : [];
return aN.filter((e) => bN.includes(e));
}
/**
* Створення шаблона або його частини внаслідок виконання одразу декількох перевірок, кожна з яких повинна повернути true.
* Якщо хоча б одна умова не виконується, код у межах хелпера також не виконується.
*
* @summary Створення шаблона за перевіркою виконання декількох умов.
* @priority 4
* @type helper
* @tag condition
* @alias ifCondAnd
* @example
* {{#ifCondAnd 1 '==' 1 2 'in' '2,3' 2 'in' '1,2' }}1{{^}}2{{/ifCondAnd}}
* @descr Виконання перевірки порівняння параметрів та їх входження до певного масиву (множини)
* @example
*/
export default function ifCondAnd() {
const args = Array.from(arguments);
const options = args.pop();
const conditions = [];
for (let i = 0; i < args.length; i += 3) {
const v1 = args[i];
const operator = args[i + 1];
let v2 = args[i + 2];
switch (operator) {
case '==':
conditions.push(v1 == v2);
break;
case '!=':
conditions.push(v1 != v2);
break;
case '===':
conditions.push(v1 === v2);
break;
case '!==':
conditions.push(v1 !== v2);
break;
case '&&':
conditions.push(v1 && v2);
break;
case '||':
conditions.push(v1 || v2);
break;
case '<':
conditions.push(v1 < v2);
break;
case '<=':
conditions.push(v1 <= v2);
break;
case '>':
conditions.push(v1 > v2);
break;
case '>=':
conditions.push(v1 >= v2);
break;
case '&':
conditions.push(intersect(v1, v2).length > 0);
break;
case '!~':
conditions.push((v1 || '').indexOf(v2) === -1);
break;
case '~':
conditions.push((v1 || '').indexOf(v2) !== -1);
break;
case 'period':
conditions.push(new Date(v1) < new Date() && new Date(v2) > new Date());
break;
case 'in': {
if (typeof v2 === 'string') v2 = v2.split(',').map(item => item.trim());
if (Array.isArray(v1)) {
conditions.push(v1.some((value) => v2.includes(value.toString())));
} else {
conditions.push(v2.includes(v1.toString()));
}
break;
}
case 'not in': {
if (typeof v2 === 'string') v2 = v2.split(',').map(item => item.trim());
conditions.push(!v2.includes(v1.toString()));
break;
}
default:
conditions.push(false);
break;
}
}
return conditions.every(Boolean) ? options.fn(this) : options.inverse(this);
}