@carlosv2/glue
Version:
Dependency injection library that stays out of the way
62 lines (61 loc) • 2.87 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Literal = void 0;
const buildable_1 = require("../buildable");
const value_1 = require("../value");
const utils_1 = require("../utils");
class Literal extends value_1.Value {
constructor(context, value) {
super(context);
this.value = value;
}
buildItem(context, item) {
return __awaiter(this, void 0, void 0, function* () {
if ((0, utils_1.isArray)(item)) {
return yield Promise.all(item.map(element => this.buildItem(context, element)));
}
if (item instanceof buildable_1.Buildable) {
return yield item.build(context);
}
if ((0, utils_1.isDictionary)(item)) {
const [ids, values] = (0, utils_1.unzip)(Object.entries(item));
const results = yield Promise.all(values.map(element => this.buildItem(context, element)));
return Object.fromEntries((0, utils_1.zip)([ids, results]));
}
if ((0, utils_1.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 (0, utils_1.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);
});
}
}
exports.Literal = Literal;