UNPKG

expression-language

Version:

Javascript implementation of symfony/expression-language

113 lines (112 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _addcslashes = require("./lib/addcslashes"); function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class Compiler { constructor(functions) { _defineProperty(this, "getFunction", name => { return this.functions[name]; }); /** * Gets the current javascript code after compilation. * * @returns {string} The javascript code */ _defineProperty(this, "getSource", () => { return this.source; }); _defineProperty(this, "reset", () => { this.source = ''; return this; }); /** * Compiles a node * * @param {Node} node * @returns {Compiler} */ _defineProperty(this, "compile", node => { node.compile(this); return this; }); _defineProperty(this, "subcompile", node => { let current = this.source; this.source = ''; node.compile(this); let source = this.source; this.source = current; return source; }); /** * Adds a raw string to the compiled code. * * @param {string} str The string * @returns {Compiler} */ _defineProperty(this, "raw", str => { this.source += str; return this; }); /** * Adds a quoted string to the compiled code. * @param {string} value The string * @returns {Compiler} */ _defineProperty(this, "string", value => { this.source += '"' + (0, _addcslashes.addcslashes)(value, "\0\t\"\$\\") + '"'; return this; }); /** * Returns a javascript representation of a given value. * @param {int|float|null|boolean|Object|Array|string} value The value to convert * @returns {Compiler} */ _defineProperty(this, "repr", (value, isIdentifier = false) => { // Integer or Float if (isIdentifier) { this.raw(value); } else if (Number.isInteger(value) || +value === value && (!isFinite(value) || !!(value % 1))) { this.raw(value); } else if (null === value) { this.raw('null'); } else if (typeof value === 'boolean') { this.raw(value ? 'true' : 'false'); } else if (typeof value === 'object') { this.raw('{'); let first = true; for (let oneKey of Object.keys(value)) { if (!first) { this.raw(', '); } first = false; this.repr(oneKey); this.raw(':'); this.repr(value[oneKey]); } this.raw('}'); } else if (Array.isArray(value)) { this.raw('['); let first = true; for (let oneValue of value) { if (!first) { this.raw(', '); } first = false; this.repr(oneValue); } this.raw(']'); } else { this.string(value); } return this; }); this.source = ''; this.functions = functions; } } exports.default = Compiler;