UNPKG

@opra/common

Version:
74 lines (73 loc) 2.11 kB
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', trim: 'tz', coerce: true, }); const _isDate = vg.isDate({ coerce: true, }); let DateTimeType = class DateTimeType { constructor(attributes) { if (attributes) Object.assign(this, attributes); } [DECODER](properties) { let fn; if (properties.convertToNative) { 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; } }; __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', }, }) .Example('2021-04-18T22:30:15') .Example('2021-04-18 22:30:15') .Example('2021-04-18 22:30')), __metadata("design:paramtypes", [Object]) ], DateTimeType); export { DateTimeType };