UNPKG

recipe-ts-runtime

Version:

TypeScript run-time library for the Recipe framework

145 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Ingredient { constructor(type, domain) { this.properties = new Map(); this.ingredientType = type; this.domain = domain ? domain : null; } getIngredientType() { return this.ingredientType; } getDomain() { return this.domain; } _getProperty(name) { return this.properties.get(name); } _hasProperty(name) { return this.properties.has(name); } _setRequired(name, value) { this.properties.set(name, value); } _setOptional(name, repeatable, value) { if (!repeatable) { this.properties.set(name, value); } else { const values = this.properties.has(name) ? this.properties.get(name) : []; values.push(value); this.properties.set(name, values); } } _setCompoundOptional(name, repeatable, ...keyValuePairs) { if (keyValuePairs.length % 2 !== 0) { throw new Error("must have an even number of key-value pairs for compound optional"); } for (let i = 0; i < keyValuePairs.length; i += 2) { if (typeof keyValuePairs[i] !== "string") { throw new Error("key in compound optional is not a string"); } } if (!repeatable) { const map = this.properties.has(name) ? this.properties.get(name) : {}; for (let i = 0; i < keyValuePairs.length; i += 2) { map[keyValuePairs[i]] = keyValuePairs[i + 1]; } this.properties.set(name, map); } else { const list = this.properties.has(name) ? this.properties.get(name) : []; const map = {}; for (let i = 0; i < keyValuePairs.length; i += 2) { map[keyValuePairs[i]] = keyValuePairs[i + 1]; } list.push(map); this.properties.set(name, list); } } duplicate() { const copy = Object.assign(Object.create(Object.getPrototypeOf(this)), this); copy.properties = new Map(this.properties); return copy; } cookbookTypeAndValueMatch(type, value) { if (type === "string") { return value === null || typeof value === "string"; } else if (type === "int") { return typeof value === "number"; } else if (type === "float") { return typeof value === "number"; } else if (type === "boolean" || type === "flag") { return typeof value === "boolean"; } else if (type.endsWith("[]")) { return Array.isArray(value) && value.reduce((result, v) => result && this.cookbookTypeAndValueMatch(type.substring(0, type.length - 2), v), true); } else if (type.endsWith("...")) { return Array.isArray(value) && value.reduce((result, v) => result && this.cookbookTypeAndValueMatch(type.substring(0, type.length - 3), v), true); } else { return value && value.constructor.name === type; } } argsMatchSignature(args, signature) { let varargMode = false; let varargType = ""; const signatureHasVararg = signature.length > 0 && signature[signature.length - 1].endsWith("..."); if (args.length < signature.length && !(signatureHasVararg && args.length === signature.length - 1)) { return false; } for (let i = 0; i < args.length; i++) { if (i >= signature.length && !varargMode) { return false; } if (!varargMode && signature[i].endsWith("...")) { varargMode = true; varargType = signature[i].substring(0, signature[i].length - 3); } if (!this.cookbookTypeAndValueMatch(varargMode ? varargType : signature[i], args[i])) { return false; } } return true; } toJSON() { const jsonObj = {}; jsonObj[this.ingredientType] = {}; for (const entry of this.properties) { jsonObj[this.ingredientType][entry[0]] = entry[1]; } return jsonObj; } static fromJSON(json, ingredientTypes) { const name = Object.getOwnPropertyNames(json)[0]; if (name in ingredientTypes === false) { throw new TypeError(`unknown ingredient type ${name}`); } return ingredientTypes[name].fromJSON(json); } static deserialize(value, type, types) { if (type.endsWith("[]")) { return value.map((v) => Ingredient.deserialize(v, type.substring(0, type.length - 2), types)); } if (type.endsWith("...")) { return value.map((v) => Ingredient.deserialize(v, type.substring(0, type.length - 3), types)); } switch (type) { case "string": case "int": case "float": case "boolean": case "flag": return value; default: return types[type].fromJSON(value); break; } } } exports.Ingredient = Ingredient; //# sourceMappingURL=Ingredient.js.map