UNPKG

@tunnel-cast/tunnel-cast

Version:

<br>

35 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EndsWith = exports.endsWithMessageBuilder = exports.endsWith = exports.ENDS_WITH = void 0; const decorator_adapter_1 = require("../../../core/factory/decorator-adapter"); const field_constraint_procedure_1 = require("../../../core/field-decorator-procedure/field-constraint.procedure"); exports.ENDS_WITH = "endsWith"; const arrayEndsWithItems = (array, prefixItems) => { if (array.length >= prefixItems.length) { const skippedItems = array.length - prefixItems.length; return prefixItems.every((item, i) => array[i + skippedItems] === item); } else { return false; } }; exports.endsWith = ({ fieldValue, args }) => { if (typeof fieldValue == "string") { return fieldValue.endsWith(args.value); } else if (Array.isArray(fieldValue)) { return Array.isArray(args.value) ? arrayEndsWithItems(fieldValue, args.value) : fieldValue.lastIndexOf(args.value) == fieldValue.length - 1; } else { return false; } }; exports.endsWithMessageBuilder = ({ fieldName }) => `The field ${fieldName} dose not end with the provided value.`; function EndsWith(value, options) { const adaptee = new field_constraint_procedure_1.FieldConstraintProcedure(exports.ENDS_WITH, options, { value }, exports.endsWith, exports.endsWithMessageBuilder); return decorator_adapter_1.decoratorAdapter(adaptee); } exports.EndsWith = EndsWith; //# sourceMappingURL=ends-with.decorator.js.map