UNPKG

@opengis/fastify-table

Version:

core-plugins

111 lines (110 loc) 3.93 kB
/* eslint-disable prefer-rest-params */ function intersect(a, b) { let aN = []; let bN = []; if (Array.isArray(a)) { aN = a; } else if (typeof a === 'string') { aN = a.split(','); } if (Array.isArray(b)) { bN = b; } else if (typeof b === 'string') { bN = b.split(','); } return aN.filter(e => bN.includes(e)); } /** * Створення шаблона або його частини внаслідок виконання одразу декількох перевірок, хоча б одна з яких повинна повернути true * * @summary Створення шаблона за перевіркою "АБО". Повинна виконатись хоча б одна умова. * @priority 4 * @type helper * @tag condition * @alias ifCondOr * @example * {{#ifCondOr 1 '==' 1 2 'in' '2,3' 2 'in' '1,2' }}Хоча б одна умова виконана{{^}}Жодна умова не була виконана{{/ifCondOr}} * @descr Виконання перевірки порівняння параметрів та їх входження до певного масиву (множини) * @example * @param {Array} args Параметри для значень і умов * @returns {String} Returns HTML */ export default function ifCondOr() { 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 '==': // eslint-disable-next-line eqeqeq conditions.push(v1 == v2); break; case '!=': // eslint-disable-next-line eqeqeq 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.some(Boolean) ? options.fn(this) : options.inverse(this); }