UNPKG

@carlosv2/glue

Version:

Dependency injection library that stays out of the way

58 lines (57 loc) 2.7 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Buildable } from '../buildable.js'; import { Value } from '../value.js'; import { isArray, isDictionary, isString, resolvePromises, unzip, zip, } from '../utils.js'; export class Literal extends Value { constructor(context, value) { super(context); this.value = value; } buildItem(context, item) { return __awaiter(this, void 0, void 0, function* () { if (isArray(item)) { return yield Promise.all(item.map(element => this.buildItem(context, element))); } if (item instanceof Buildable) { return yield item.build(context); } if (isDictionary(item)) { const [ids, values] = unzip(Object.entries(item)); const results = yield Promise.all(values.map(element => this.buildItem(context, element))); return Object.fromEntries(zip([ids, results])); } if (isString(item)) { const container = context.getContainer(); const placeholders = {}; for (const match of item.matchAll(/(\\)?\@\{([^}]+)\}/g)) { const escape = match[1]; const placeholder = match[2]; if (escape || placeholder in placeholders) { continue; } placeholders[placeholder] = container .getParameter(placeholder) .then(String); } const parameters = yield resolvePromises(placeholders); return item.replace(/(\\)?\@\{([^}]+)\}/g, (match, escape, placeholder) => { return escape ? match.slice(1) : parameters[placeholder]; }); } return item; }); } assemble(context) { return __awaiter(this, void 0, void 0, function* () { return yield this.buildItem(context, this.value); }); } }