UNPKG

@opra/common

Version:
83 lines (82 loc) 2.83 kB
import { __decorate, __metadata } from "tslib"; import { isDateString, toString, validator, vg } from 'valgen'; import { DECODER, ENCODER } from '../../constants.js'; import { SimpleType } from '../simple-type.js'; let PartialDateType = class PartialDateType { constructor(attributes) { if (attributes) Object.assign(this, attributes); } [DECODER](properties) { const fn = toPartialDate; const x = []; if (properties.minValue != null) { isDateString(properties.minValue); x.push(toString, vg.isGte(properties.minValue)); } if (properties.maxValue != null) { isDateString(properties.maxValue); x.push(toString, vg.isLte(properties.maxValue)); } return x.length > 0 ? vg.pipe([fn, ...x], { returnIndex: 0 }) : fn; } [ENCODER](properties) { const fn = toPartialDate; const x = []; if (properties.minValue != null) { isDateString(properties.minValue); x.push(vg.isGte(properties.minValue)); } if (properties.maxValue != null) { isDateString(properties.maxValue); x.push(vg.isLte(properties.maxValue)); } return x.length > 0 ? vg.pipe([fn, ...x], { returnIndex: 0 }) : fn; } }; __decorate([ SimpleType.Attribute({ description: 'Minimum value', }), __metadata("design:type", String) ], PartialDateType.prototype, "minValue", void 0); __decorate([ SimpleType.Attribute({ description: 'Maximum value', }), __metadata("design:type", String) ], PartialDateType.prototype, "maxValue", void 0); PartialDateType = __decorate([ (SimpleType({ name: 'partialdate', description: 'Specifies a point in time using a 24-hour clock notation.\rFormat: YYYY-[MM-[DD-[T?HH:[MM:[SS[.S[S[S[S]]]]]]]]][+/-ZZ:ZZ].', nameMappings: { js: 'string', json: 'string', }, }) .Example('2021-04-18T22:30:15:22+03:00') .Example('2021-04-18 22:30') .Example('2021-04-18') .Example('2021')), __metadata("design:paramtypes", [Object]) ], PartialDateType); export { PartialDateType }; const _isDateString = vg.isDateString({ precisionMin: 'year', }); const toPartialDate = validator((input) => { if (input instanceof Date) { let s = _isDateString(input, { coerce: true }); if (s.endsWith('Z')) s = s.substring(0, s.length - 1); if (s.endsWith('.000')) s = s.substring(0, s.length - 4); if (s.endsWith(':00')) s = s.substring(0, s.length - 3); if (s.endsWith('00:00')) s = s.substring(0, s.length - 6); return s; } return _isDateString(input, { coerce: false }); });