UNPKG

json-expression-eval

Version:

json serializable rule engine / boolean expression evaluator

44 lines 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.expressionStringAssertion = exports.expressionNumberAssertion = exports.contextStringAssertion = exports.contextNumberAssertion = exports.assertUnreachable = exports.objectKeys = exports.getFromPath = void 0; const getFromPath = (obj, path) => { const keys = path.split('.'); let exists = keys.length > 0; const value = keys.reduce((acc, key) => { const accessible = acc !== null && acc !== undefined; exists = exists && accessible && key in acc; return accessible ? acc[key] : undefined; }, obj); return { value, exists }; }; exports.getFromPath = getFromPath; exports.objectKeys = Object.keys; const assertUnreachable = (x, message) => { throw new Error(message); }; exports.assertUnreachable = assertUnreachable; function contextNumberAssertion(key, value) { if (typeof value !== 'number') { throw new Error(`Invalid context - ${key} must be a number`); } } exports.contextNumberAssertion = contextNumberAssertion; function contextStringAssertion(key, value) { if (typeof value !== 'string') { throw new Error(`Invalid context - ${key} must be a string`); } } exports.contextStringAssertion = contextStringAssertion; function expressionNumberAssertion(key, value) { if (typeof value !== 'number') { throw new Error(`Invalid expression - ${key} must be a number`); } } exports.expressionNumberAssertion = expressionNumberAssertion; function expressionStringAssertion(key, value) { if (typeof value !== 'string') { throw new Error(`Invalid expression - ${key} must be a string`); } } exports.expressionStringAssertion = expressionStringAssertion; //# sourceMappingURL=helpers.js.map