UNPKG

@puregram/hear

Version:

simple implementation of hearing messages system for puregram

36 lines (35 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unifyCondition = exports.getObjectValue = exports.splitPath = void 0; const splitPath = (path) => (path .replace(/\[([^[\]]*)]/g, '.$1.') .split('.') .filter(Boolean)); exports.splitPath = splitPath; const getObjectValue = (source, selectors) => { let link = source; for (const selector of selectors) { if (!link[selector]) { return; } link = link[selector]; } return link; }; exports.getObjectValue = getObjectValue; const unifyCondition = (condition) => { if (typeof condition === 'function') { return condition; } if (condition instanceof RegExp) { return (text) => text ? condition.test(text) : false; } if (Array.isArray(condition)) { const arrayConditions = condition.map(exports.unifyCondition); return (value) => (Array.isArray(value) ? arrayConditions.every(cond => value.some(val => cond(val))) : arrayConditions.some(cond => cond(value))); } return (value) => value === condition; }; exports.unifyCondition = unifyCondition;