UNPKG

@grouparoo/core

Version:
56 lines (55 loc) 2.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MustacheUtils = void 0; const mustache_1 = __importDefault(require("mustache")); const actionhero_1 = require("actionhero"); var MustacheUtils; (function (MustacheUtils) { function strictlyRender(string, data, errorPrefix, allowNull = false) { validateMustacheVariables(string, data, errorPrefix, allowNull); return render(string, data); } MustacheUtils.strictlyRender = strictlyRender; function render(string, data) { return mustache_1.default.render(string, data); } MustacheUtils.render = render; function validateMustacheVariables(string, data, errorPrefix = "missing mustache key", allowNull = false) { getMustacheVariables(string).map((key) => { const value = key.split(".").reduce((o, i) => o[i], data); if (value === undefined || (allowNull === false && value === null)) { throw new Error(`${errorPrefix} ${JSON.stringify(key)}`); } }); } MustacheUtils.validateMustacheVariables = validateMustacheVariables; function getMustacheVariables(string) { return mustache_1.default.parse(string) .filter((chunk) => chunk[0] === "name" || chunk[0] === "&") .map((chunk) => chunk[1]); } MustacheUtils.getMustacheVariables = getMustacheVariables; async function getMustacheVariablesAsPropertyIds(string, configObjects = []) { const keys = getMustacheVariables(string); const properties = await actionhero_1.api.sequelize.models.Property.findAll(); const searchItems = [].concat(properties.map((p) => { return { id: p["id"], key: p["key"] }; }), configObjects .filter((c) => c.class.toLowerCase() === "property") .map((c) => { //@ts-ignore const key = c["key"] || c["name"]; return { id: c.id, key }; })); return keys.map((k) => { const item = searchItems.find((p) => p.key === k); if (!item) throw new Error(`no property with key ${k}`); return item.id; }); } MustacheUtils.getMustacheVariablesAsPropertyIds = getMustacheVariablesAsPropertyIds; })(MustacheUtils = exports.MustacheUtils || (exports.MustacheUtils = {}));