json-expression-eval
Version:
json serializable rule engine / boolean expression evaluator
44 lines • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertUnreachable = exports.objectKeys = exports.getFromPath = void 0;
exports.contextNumberAssertion = contextNumberAssertion;
exports.contextStringAssertion = contextStringAssertion;
exports.expressionNumberAssertion = expressionNumberAssertion;
exports.expressionStringAssertion = expressionStringAssertion;
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`);
}
}
function contextStringAssertion(key, value) {
if (typeof value !== 'string') {
throw new Error(`Invalid context - ${key} must be a string`);
}
}
function expressionNumberAssertion(key, value) {
if (typeof value !== 'number') {
throw new Error(`Invalid expression - ${key} must be a number`);
}
}
function expressionStringAssertion(key, value) {
if (typeof value !== 'string') {
throw new Error(`Invalid expression - ${key} must be a string`);
}
}
//# sourceMappingURL=helpers.js.map