@toreda/strong-types
Version:
Better TypeScript code in fewer lines.
33 lines (32 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.idMake = void 0;
const rules_1 = require("../rules");
const type_1 = require("../create/type");
const value_1 = require("../initial/value");
/**
* Create a Strong Id type.
* @param fallback
* @param initial
* @returns
*
* @category Strings
*/
function idMake(fallback, value, options) {
const rules = new rules_1.Rules();
if (options) {
if (typeof options.maxLength === 'number') {
rules.add().must.have.length.lessThanOrEqualTo(options.maxLength);
}
if (typeof options.minLength === 'number') {
rules.add().must.have.length.greaterThanOrEqualTo(options.minLength);
}
if (typeof options.contains === 'string' || Array.isArray(options.contains)) {
rules.add().must.contain.text(options.contains);
}
}
rules.add().must.match.type.string();
const typeId = options && typeof options.typeId === 'string' ? options.typeId : 'Id';
return (0, type_1.createType)(fallback, (0, value_1.initialValue)(value), rules, typeId);
}
exports.idMake = idMake;