@tunnel-cast/tunnel-cast
Version:
<br>
159 lines • 7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCastProcedures = void 0;
const field_procedure_type_enum_1 = require("../../models/enums/field-procedure-type.enum");
const globals_1 = require("../globals/globals");
const returnProceduresWithTags = (procedures, tags) => !(tags && tags.length)
? procedures
: procedures.filter((procedure) => { var _a; return ((_a = procedure.options) === null || _a === void 0 ? void 0 : _a.tags) == undefined || tags.some((tag) => procedure.options.tags.includes(tag)); });
function executeCastProcedures(field, procedures, target, projectedContext, options) {
const fieldValue = target[field];
const executionInfoCollection = {};
const conditionalHandling = returnProceduresWithTags(procedures[field_procedure_type_enum_1.FieldProcedureType.ConditionalHandling] || [], options.tags);
const conditionalHandlingResult = conditionalHandling.map((cond) => executeFieldConditionalHandling(cond, fieldValue, target, executionInfoCollection));
const skipHandling = conditionalHandlingResult.some((cond) => cond.conditionPass == false);
if (skipHandling) {
projectedContext[field] = fieldValue;
return [];
}
const [defaultAssignment] = returnProceduresWithTags(procedures[field_procedure_type_enum_1.FieldProcedureType.DefaultAssignment] || [], options.tags);
const { isEmpty, defaultValue } = executeFieldDefaultAssignment(defaultAssignment, fieldValue, target, executionInfoCollection);
if (isEmpty) {
projectedContext[field] = defaultValue;
return [];
}
const parsers = returnProceduresWithTags(procedures[field_procedure_type_enum_1.FieldProcedureType.Parser] || [], options.tags);
const parsedFieldValue = parsers.reduce((accParseValue, parser) => executeFieldParser(parser, accParseValue, target, executionInfoCollection).parseValue, fieldValue);
const constraints = returnProceduresWithTags(procedures[field_procedure_type_enum_1.FieldProcedureType.Constraint] || [], options.tags);
const constraintsResult = constraints
.map((cons) => executeFieldConstraint(cons, parsedFieldValue, target, executionInfoCollection))
.filter(({ message }) => message != undefined);
if (constraintsResult.length == 0) {
projectedContext[field] = parsedFieldValue;
}
return constraintsResult;
}
exports.executeCastProcedures = executeCastProcedures;
const executeFieldDefaultAssignment = (emptyIdentifierProcedure, processedValue, context, executionInfoCollection) => {
if (!emptyIdentifierProcedure) {
return { info: {} };
}
const fieldValue = processedValue;
const { emptyIdentifier, args, fieldName, defaultWith, procedureId, fieldProcedureType, options, } = emptyIdentifierProcedure;
const isEmpty = !emptyIdentifier
? globals_1.globalSetting.defaultEmptyIdentifier({ args, fieldValue, fieldName, path: fieldName })
: typeof emptyIdentifier == "function"
? emptyIdentifier({ args, fieldValue, fieldName, path: fieldName })
: emptyIdentifier.includes(fieldValue);
const defaultValue = !isEmpty
? undefined
: typeof defaultWith == "function"
? defaultWith({ args, fieldValue, fieldName, path: fieldName, context })
: defaultWith;
return {
isEmpty,
defaultValue,
info: {
procedure: emptyIdentifierProcedure,
fieldName,
procedureId,
fieldProcedureType,
options,
context,
},
};
};
const executeFieldConditionalHandling = (fieldConditionalHandling, processedValue, context, executionInfoCollection) => {
const fieldValue = processedValue;
const conditionPass = fieldConditionalHandling.condition({
args: fieldConditionalHandling.args,
fieldValue,
fieldName: fieldConditionalHandling.fieldName,
path: fieldConditionalHandling.fieldName,
context,
});
const { procedureId, fieldProcedureType, options, fieldName } = fieldConditionalHandling;
return {
conditionPass,
info: {
procedure: fieldConditionalHandling,
fieldName,
procedureId,
fieldProcedureType,
options,
context,
},
};
};
const executeFieldConstraint = (fieldConstraint, processedValue, context, executionInfoCollection) => {
const fieldValue = processedValue;
const { procedureId, fieldProcedureType, options, fieldName } = fieldConstraint;
let constraintPass = undefined, msgValue = fieldValue, msgPath = fieldName;
if ((options === null || options === void 0 ? void 0 : options.iterate) == true) {
const valueIteratableValues = Object.values(fieldValue);
for (let index = 0; index < valueIteratableValues.length; index++) {
const currValue = valueIteratableValues[index];
const currPath = `${fieldName}[${index}]`;
const currConstraintPass = fieldConstraint.constraint({
args: fieldConstraint.args,
fieldValue: currValue,
fieldName,
path: currPath,
options,
context,
});
if (currConstraintPass == false) {
msgPath = currPath;
msgValue = currValue;
constraintPass = currConstraintPass;
break;
}
}
constraintPass = constraintPass != false;
}
else {
constraintPass = fieldConstraint.constraint({
args: fieldConstraint.args,
fieldValue,
fieldName,
path: fieldName,
options,
context,
});
}
let message;
if (!constraintPass) {
message =
typeof fieldConstraint.messageBuilder == "string"
? fieldConstraint.messageBuilder
: fieldConstraint.messageBuilder({
args: fieldConstraint.args,
fieldValue: msgValue,
fieldName,
path: msgPath,
options,
});
}
return {
message,
constraintPass,
info: { procedure: fieldConstraint, fieldName, procedureId, fieldProcedureType, options, context },
};
};
const executeFieldParser = (fieldParser, processedValue, context, executionInfoCollection) => {
const fieldValue = processedValue;
const { procedureId, fieldProcedureType, options, fieldName } = fieldParser;
const parseValue = fieldParser.parse({
args: fieldParser.args,
fieldValue,
fieldName,
path: fieldName,
options,
context,
});
return {
parseValue,
info: { procedure: fieldParser, fieldName, procedureId, fieldProcedureType, options, context },
};
};
//# sourceMappingURL=execute-cast-procedures.js.map