UNPKG

@edirect/template

Version:
271 lines 12.7 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, "transformers") || ((0, lodash_1.has)(object, "transformerParams") && (0, lodash_1.has)(object, "transformer"))); } isTransformers(object) { return !!((0, lodash_1.has)(object, "transformers") && Array.isArray(object.transformers)); } 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) => { var _a; 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, ignoreIndexs } = target; value = (0, lodash_1.get)(dataSource, arraySource); if ((0, lodash_1.isArray)(value)) { const finalArray = []; for (const [index, dataSource] of value.entries()) { if (ignoreIndexs && ignoreIndexs.includes(index)) continue; 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)); } if ((0, lodash_1.has)(target, "transformers") && Array.isArray(target.transformers)) { value = this.applySequentialTransformers(target, dataSource, rootData); } else { 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; } const useAllowNull = (0, lodash_1.has)(target, "transformers") && Array.isArray(target.transformers) ? !!((_a = target.transformers .filter(t => !this.isDynamicArrayMapper(t)) .slice(-1)[0]) === null || _a === void 0 ? void 0 : _a['allowNull']) : !!target.allowNull; return this.setValueByCondition(acc, key, value, useAllowNull); } return this.setValueByCondition(acc, key, value); }, {}); } applySequentialTransformers(target, dataSource, rootData) { let transformedValue = null; const transformers = target.transformers; for (const transformer of transformers) { if (this.isDynamicArrayMapper(transformer)) { const dynamicArrayMapper = transformer; const { arraySource, arrayTemplate, ignoreIndexs, simpleArray } = dynamicArrayMapper; let arrayData; if (transformedValue !== null) { if ((0, lodash_1.isArray)(transformedValue)) { arrayData = transformedValue; } else { arrayData = (0, lodash_1.get)(transformedValue, arraySource); if (!arrayData) { arrayData = (0, lodash_1.get)(dataSource, arraySource); } } } else { arrayData = (0, lodash_1.get)(dataSource, arraySource); } if ((0, lodash_1.isArray)(arrayData)) { const finalArray = []; for (const [index, arrayItem] of arrayData.entries()) { if (ignoreIndexs && ignoreIndexs.includes(index)) continue; let processedItem; if (!!simpleArray) { const source = this.simpleValueToObject(arrayItem); processedItem = this.transformPayload(source, arrayTemplate); } else { processedItem = this.transformPayload(arrayItem, arrayTemplate, rootData); } if (this.checkValue(processedItem) && this.isValidObject(processedItem)) { finalArray.push(processedItem); } } transformedValue = finalArray; } else { transformedValue = []; } continue; } const { fields, transformer: transformerName, transformerParams, defaultValue, allowNull, } = transformer; 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) { let fieldValue; if (transformedValue !== null) { fieldValue = transformedValue; } else { fieldValue = (0, lodash_1.get)(dataSource, field); } if (transformerName && transformedTransformParams.length) { fieldValue = this.runTransformerWithParams(transformerName, transformedTransformParams, fieldValue); } else if (transformerName) { fieldValue = this.runTransformer(transformerName, fieldValue); } if (this.checkValue(fieldValue, !!allowNull)) { transformedValue = fieldValue; break; } } } else { let valueToTransform = transformedValue !== null ? transformedValue : null; if (transformerName && transformedTransformParams.length) { transformedValue = this.runTransformerWithParams(transformerName, transformedTransformParams, valueToTransform); } else if (transformerName) { transformedValue = this.runTransformer(transformerName, valueToTransform); } } if (!this.checkValue(transformedValue, !!allowNull)) { transformedValue = defaultValue; } } return transformedValue; } } exports.TemplateModule = TemplateModule; //# sourceMappingURL=index.js.map