UNPKG

@gabliam/expression

Version:
50 lines (49 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Expression = void 0; const tslib_1 = require("tslib"); const lodash_1 = tslib_1.__importDefault(require("lodash")); const parser_1 = require("./parser"); // eslint-disable-next-line @typescript-eslint/no-var-requires const validPath = require('is-valid-path'); class Expression { constructor(ast, context, input) { this.context = context; this.input = input; if (ast) { this.parser = new parser_1.Parser(ast); } } getValue(vars = {}) { if (!this.parser) { if (validPath(this.input)) { return this.input; } return undefined; } const context = Object.keys(vars).reduce((prev, current) => { prev[`$${current}`] = lodash_1.default.cloneDeep(vars[current]); return prev; }, { $root: this.context, }); try { const res = this.parser.parse(Object.assign(Object.assign({}, this.context), context)); if (res === null && validPath(this.input)) { return this.input; } if (res === parser_1.IS_STRING) { return this.input; } return res; } catch (e) { // case of multiple subpath if (validPath(this.input)) { return this.input; } throw e; } } } exports.Expression = Expression;