UNPKG

@dingdaoos/lucid-utils

Version:
103 lines (98 loc) 3.95 kB
import dayjs from 'dayjs' interface Options<T> { type: string defaultFormat: string startValue: string value: Array<string> endValue: string valueFormat: string } export class Normal<T> { private type: string private defaultFormat: string private startValue: string private value: Array<string> private endValue: string private valueFormat: string constructor(options: Options<T>) { this.type = options.type this.defaultFormat = options.defaultFormat this.startValue = options.startValue this.value = options.value this.endValue = options.endValue this.valueFormat = options.valueFormat this.init() } init() { if (this.type === 'yearrange') { this.defaultFormat = 'YYYY' this.startValue = this.value[0] ? this.value[0] : '' this.endValue = this.value[1] ? this.value[1] : '' } else if (this.type === 'monthrange') { this.defaultFormat = 'YYYY-MM' if (this.valueFormat) { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.valueFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.valueFormat) : '' } else { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.defaultFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.defaultFormat) : '' } } else if (this.type === 'daterange') { this.defaultFormat = 'YYYY-MM-DD' if (this.valueFormat) { if (this.valueFormat === 'timestamp') { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.defaultFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.defaultFormat) : '' } else { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.valueFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.valueFormat) : '' } } else { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.defaultFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.defaultFormat) : '' } } else if (this.type === 'datetimerange') { this.defaultFormat = 'YYYY-MM-DD HH:mm:ss' if (this.valueFormat) { if (this.valueFormat === 'timestamp') { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.defaultFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.defaultFormat) : '' } else { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.valueFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.valueFormat) : '' } } else { this.startValue = this.value[0] ? dayjs(this.value[0]).format(this.defaultFormat) : '' this.endValue = this.value[1] ? dayjs(this.value[1]).format(this.defaultFormat) : '' } } } onChange(val) { if (val.length === 1) { if (this.valueFormat) { if (this.valueFormat === 'timestamp') { this.startValue = dayjs(val[0]).format(this.defaultFormat) } else { this.startValue = dayjs(val[0]).format(this.valueFormat) } } else { this.startValue = dayjs(val[0]).format(this.defaultFormat) } this.endValue = '' } else if (val.length === 2) { if (this.valueFormat) { if (this.valueFormat === 'timestamp') { this.startValue = dayjs(val[0]).format(this.defaultFormat) this.endValue = dayjs(val[1]).format(this.defaultFormat) } else { this.startValue = dayjs(val[0]).format(this.valueFormat) this.endValue = dayjs(val[1]).format(this.valueFormat) } } else { this.startValue = dayjs(val[0]).format(this.defaultFormat) this.endValue = dayjs(val[1]).format(this.defaultFormat) } } } } export default Normal