model-layer
Version:
62 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringType = void 0;
const Type_1 = require("./Type");
const utils_1 = require("../utils");
const errors_1 = require("../errors");
class StringType extends Type_1.Type {
constructor({ nullAsEmpty = false, emptyAsNull = false, trim = false, lower = false, upper = false, ...otherParams }) {
super(otherParams);
this.nullAsEmpty = nullAsEmpty;
this.emptyAsNull = emptyAsNull;
this.trim = trim;
this.lower = lower;
this.upper = upper;
if (nullAsEmpty && emptyAsNull) {
throw new errors_1.ConflictNullAndEmptyStringParameterError({});
}
if (lower && upper) {
throw new errors_1.ConflictLowerUpperParameterError({});
}
}
prepare(value, key) {
if (value == null) {
if (this.nullAsEmpty) {
return "";
}
return null;
}
if (utils_1.isNaN(value) ||
typeof value === "boolean" ||
utils_1.isObject(value) ||
Array.isArray(value) ||
// infinity
value === 1 / 0 ||
value === -1 / 0) {
const valueAsString = utils_1.invalidValuesAsString(value);
throw new errors_1.InvalidStringError({
key,
invalidValue: valueAsString
});
}
// convert to string
value += "";
if (this.trim) {
value = value.trim();
}
if (this.emptyAsNull) {
if (value === "") {
value = null;
}
}
if (this.lower) {
value = value.toLowerCase();
}
else if (this.upper) {
value = value.toUpperCase();
}
return value;
}
}
exports.StringType = StringType;
//# sourceMappingURL=StringType.js.map