@opra/common
Version:
Opra common package
76 lines (75 loc) • 2.09 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { vg } from 'valgen';
import { DECODER, ENCODER } from '../../constants.js';
import { SimpleType } from '../simple-type.js';
const _isDateString = vg.isDateString({
precisionMin: 'day',
precisionMax: 'day',
trim: true,
coerce: true,
});
const _isDate = vg.isDate({
trim: 'day',
coerce: true,
});
let DateType = class DateType {
designType;
constructor(attributes) {
if (attributes)
Object.assign(this, attributes);
}
[DECODER](properties) {
let fn;
if (properties.designType === Date) {
fn = _isDate;
}
else {
fn = _isDateString;
}
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 = _isDateString;
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: 'Minimum value',
}),
__metadata("design:type", String)
], DateType.prototype, "minValue", void 0);
__decorate([
SimpleType.Attribute({
description: 'Maximum value',
}),
__metadata("design:type", String)
], DateType.prototype, "maxValue", void 0);
DateType = __decorate([
(SimpleType({
name: 'date',
description: 'A date without time',
nameMappings: {
js: 'string',
json: 'string',
},
}).Example('2021-04-18', 'Full date value')),
__metadata("design:paramtypes", [Object])
], DateType);
export { DateType };