@eddaic/nestjs-decorators
Version:
Additional decorators intended for use with NestJS framework.
39 lines (38 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformInt = transformInt;
exports.transformIntArray = transformIntArray;
exports.TransformInt = TransformInt;
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const util_1 = require("../util");
function transformInt(value, options) {
if (!(0, class_validator_1.isEmpty)(value)) {
const numberValue = Number(value);
if ((0, class_validator_1.isInt)(numberValue)) {
return numberValue;
}
else if (options?.rounding !== undefined) {
return (0, util_1.round)(numberValue, options.rounding);
}
}
return null;
}
function transformIntArray(values, options) {
return values.map((value) => transformInt(value, options));
}
/**
* Transform value into integer using specified options. If the value
* is null, empty or undefined, it will return null.
*
* Otherwise an integer value will be returned rounded as necessary
* using the specified rounding policy.
* @param options {@link TransformIntOptions}
* @returns
*/
function TransformInt(options = {}) {
const { each = false, ...rest } = options;
return (0, class_transformer_1.Transform)(({ value }) => each && (0, class_validator_1.isArray)(value)
? transformIntArray(value, rest)
: transformInt(value, rest), rest);
}