UNPKG

@edirect/template

Version:
162 lines 7.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateModule = void 0; const lodash_1 = require("lodash"); class TemplateModule { constructor() { this.context = {}; this.template = {}; this.transformers = {}; this.options = {}; } setContext(context) { this.context = context; } setTemplate(template) { this.template = template; } setTransformers(transformers) { this.transformers = transformers; } setOptions(options) { this.options = options; } verifyTransformer(transformer, methodName) { return !!(transformer && transformer[methodName] && typeof transformer[methodName] === "function"); } runTransformer(transformer, value) { if (this.verifyTransformer(this.transformers, transformer)) { return this.transformers[transformer]({ context: this.context, value }); } } runTransformerWithParams(transformer, params, value) { if (this.verifyTransformer(this.transformers, transformer)) { return this.transformers[transformer]({ context: this.context, value }, ...params); } } setValueByCondition(object, key, value, allowNull = false) { return this.checkValue(value, allowNull) ? Object.assign(Object.assign({}, object), { [key]: value }) : object; } isTransformer(object) { return !!((0, lodash_1.has)(object, "transformer") || (0, lodash_1.has)(object, "fields") || (0, lodash_1.has)(object, "defaultValue") || ((0, lodash_1.has)(object, "transformerParams") && (0, lodash_1.has)(object, "transformer"))); } isStaticArrayMapper(object) { return Array.isArray(object); } isDynamicArrayMapper(object) { return !!((0, lodash_1.has)(object, "arraySource") && (0, lodash_1.has)(object, "arrayTemplate")); } isValidObject(object) { return (!!object && typeof object === "object" && Object.keys(object).length > 0 && Object.getPrototypeOf(object) !== null && Object.getPrototypeOf(object) === Object.prototype); } checkValue(value, allowNull = false) { var _a, _b; const omitEmptyFields = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.omitEmptyFields) !== null && _b !== void 0 ? _b : false; return !!(value || allowNull || typeof value === "boolean" || typeof value === "number" || (omitEmptyFields ? typeof value === "string" && value !== "" : typeof value === "string")); } containsVar(value) { return !!value.match(/^\$\{[a-z\_0-9\-\.]+\}$/gi); } getVarOnConst(varName, context) { varName = varName.substring(2, varName.length - 1); return (0, lodash_1.get)(context, varName); } simpleValueToObject(value) { return { value }; } transformPayload(dataSource, template = this.template, rootData = undefined) { if (!template) return {}; if (rootData === undefined) rootData = dataSource; return Object.keys(template).reduce((acc, key) => { const target = template[key]; let value = (0, lodash_1.get)(dataSource, target); if (this.isStaticArrayMapper(target)) { const finalArray = []; for (const arrayTemplate of target) { if (this.isValidObject(arrayTemplate)) { value = this.transformPayload(dataSource, arrayTemplate); if (this.checkValue(value) && this.isValidObject(value)) finalArray.push(value); } } return this.setValueByCondition(acc, key, finalArray); } if (this.isDynamicArrayMapper(target)) { const { arraySource, arrayTemplate } = target; value = (0, lodash_1.get)(dataSource, arraySource); if ((0, lodash_1.isArray)(value)) { const finalArray = []; for (const dataSource of value) { if (!!target.simpleArray) { const source = this.simpleValueToObject(dataSource); value = this.transformPayload(source, arrayTemplate); } else { value = this.transformPayload(dataSource, arrayTemplate, rootData); } if (this.checkValue(value) && this.isValidObject(value)) finalArray.push(value); } return this.setValueByCondition(acc, key, finalArray); } return this.setValueByCondition(acc, key, []); } if (this.isValidObject(target)) { if (!this.isTransformer(target)) { return this.setValueByCondition(acc, key, this.transformPayload(dataSource, target)); } const { fields, transformer, transformerParams, defaultValue, allowNull, } = target; const transformedTransformParams = []; if (transformerParams && Array.isArray(transformerParams)) { transformedTransformParams.push(...transformerParams.map((param) => { if (!(0, lodash_1.isString)(param) || !this.containsVar(param)) return param; return (this.getVarOnConst(param, dataSource) || this.getVarOnConst(param, rootData)); })); } if (fields && Array.isArray(fields)) { for (const field of fields) { value = (0, lodash_1.get)(dataSource, field); if (transformer && transformedTransformParams.length) value = this.runTransformerWithParams(transformer, transformedTransformParams, value); else if (transformer) value = this.runTransformer(transformer, value); if (this.checkValue(value, !!allowNull)) break; } } else { if (transformer && transformedTransformParams.length) value = this.runTransformerWithParams(transformer, transformedTransformParams); else if (transformer) value = this.runTransformer(transformer); } value = this.checkValue(value, !!allowNull) ? value : defaultValue; return this.setValueByCondition(acc, key, value, !!allowNull); } return this.setValueByCondition(acc, key, value); }, {}); } } exports.TemplateModule = TemplateModule; //# sourceMappingURL=index.js.map