expand-value
Version:
Get deeply nested values from an object, like dot-prop and get-value, but with support for advanced features like bracket-notation and more.
141 lines (140 loc) • 3.73 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/expression.ts
var expression_exports = {};
__export(expression_exports, {
evaluate: () => evaluate
});
module.exports = __toCommonJS(expression_exports);
function parse(expression) {
let pos = 0;
function peek() {
return expression[pos];
}
__name(peek, "peek");
function consume() {
return expression[pos++];
}
__name(consume, "consume");
function skipWs() {
while (pos < expression.length && /\s/.test(peek())) {
pos++;
}
}
__name(skipWs, "skipWs");
function parseNum() {
let n = "";
while (pos < expression.length && /\d/.test(peek())) {
n += consume();
}
return { type: "number", val: parseInt(n, 10) };
}
__name(parseNum, "parseNum");
function parseIdent() {
let id = "";
while (pos < expression.length && /[\w$]/.test(peek())) {
id += consume();
}
return id;
}
__name(parseIdent, "parseIdent");
function parsePath() {
const parts = [parseIdent()];
while (peek() === ".") {
consume();
parts.push(parseIdent());
}
return { type: "path", parts };
}
__name(parsePath, "parsePath");
function parsePrimary() {
skipWs();
const ch = peek();
if (/\d/.test(ch)) {
return parseNum();
}
if (/[\w$]/.test(ch)) {
return parsePath();
}
if (ch === "(") {
consume();
const node = parseExpr();
skipWs();
if (peek() === ")") consume();
return node;
}
return null;
}
__name(parsePrimary, "parsePrimary");
function parseExpr() {
let left = parsePrimary();
while (true) {
skipWs();
const op = peek();
if (op !== "+" && op !== "-") {
break;
}
consume();
const right = parsePrimary();
left = { type: "binary", op, left, right };
}
return left;
}
__name(parseExpr, "parseExpr");
return parseExpr();
}
__name(parse, "parse");
function evaluateExpression(node, data) {
if (!node) return void 0;
switch (node.type) {
case "number": {
return node.val;
}
case "path": {
let val = data;
for (const p of node.parts) {
if (val == null) return void 0;
val = val[p];
}
return val;
}
case "binary": {
const l = evaluateExpression(node.left, data);
const r = evaluateExpression(node.right, data);
if (node.op === "+") return l + r;
if (node.op === "-") return l - r;
return void 0;
}
default: {
return void 0;
}
}
}
__name(evaluateExpression, "evaluateExpression");
function evaluate(expression, data) {
const ast = parse(expression);
return evaluateExpression(ast, data);
}
__name(evaluate, "evaluate");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
evaluate
});
//# sourceMappingURL=expression.js.map