@opra/common
Version:
Opra common package
90 lines (89 loc) • 2.88 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { vg } from 'valgen';
import { DECODER, ENCODER } from '../../constants.js';
import { SimpleType } from '../simple-type.js';
let DateTimeType = class DateTimeType {
designType;
constructor(attributes) {
if (attributes)
Object.assign(this, attributes);
}
precisionMin;
precisionMax;
[DECODER](properties) {
let fn;
if (properties.designType === Date) {
fn = vg.isDate({ coerce: true });
}
else {
fn = vg.isDateString({
precisionMin: properties.precisionMin || 'day',
precisionMax: properties.precisionMax || 'ms',
trim: true,
coerce: true,
});
}
const x = [fn];
if (properties.minValue != null) {
x.push(vg.isGte(fn(properties.minValue)));
}
if (properties.maxValue != null) {
x.push(vg.isLte(fn(properties.maxValue)));
}
return x.length > 0 ? vg.pipe(x, { returnIndex: 0 }) : fn;
}
[ENCODER](properties) {
const fn = vg.isDateString({
precisionMin: properties.precisionMin || 'day',
precisionMax: properties.precisionMax || 'ms',
trim: true,
coerce: true,
});
const x = [fn];
if (properties.minValue != null) {
x.push(vg.isGte(fn(properties.minValue)));
}
if (properties.maxValue != null) {
x.push(vg.isLte(fn(properties.maxValue)));
}
return x.length > 0 ? vg.pipe(x, { returnIndex: 0 }) : fn;
}
minValue;
maxValue;
};
__decorate([
SimpleType.Attribute({
description: 'Determines the minimum precision, e.g. "year", "month", "hours", "minutes", "seconds" etc. Defaults to "day".',
}),
__metadata("design:type", String)
], DateTimeType.prototype, "precisionMin", void 0);
__decorate([
SimpleType.Attribute({
description: 'Determines the minimum precision, e.g. "year", "month", "hours", "minutes", "seconds" etc. Defaults to "ms".',
}),
__metadata("design:type", String)
], DateTimeType.prototype, "precisionMax", void 0);
__decorate([
SimpleType.Attribute({
description: 'Minimum value',
}),
__metadata("design:type", String)
], DateTimeType.prototype, "minValue", void 0);
__decorate([
SimpleType.Attribute({
description: 'Maximum value',
}),
__metadata("design:type", String)
], DateTimeType.prototype, "maxValue", void 0);
DateTimeType = __decorate([
SimpleType({
name: 'datetime',
description: 'A full datetime value',
nameMappings: {
js: 'string',
json: 'string',
},
}),
__metadata("design:paramtypes", [Object])
], DateTimeType);
export { DateTimeType };