feat: add parse string to date
This commit is contained in:
parent
a7a20f55f4
commit
7c01ca36b5
@ -1,7 +1,32 @@
|
|||||||
|
import { parceable } from 'Schema'
|
||||||
import SchemaItem from '../SchemaItem'
|
import SchemaItem from '../SchemaItem'
|
||||||
|
|
||||||
export default class SchemaDate extends SchemaItem<Date> {
|
export default class SchemaDate extends SchemaItem<Date> {
|
||||||
|
|
||||||
|
@parceable()
|
||||||
|
public parseString(format: 'iso8601' | 'yy-mm-dd' | 'jj/mm/yy' = 'iso8601') {
|
||||||
|
switch (format) {
|
||||||
|
case 'yy-mm-dd':
|
||||||
|
case 'iso8601': {
|
||||||
|
this.addPreProcess((it) => typeof it === 'string' ? new Date(it) : it)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'jj/mm/yy': {
|
||||||
|
this.addPreProcess((input) => {
|
||||||
|
if (typeof input !== 'string') {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
const splitted = input.split('/').map((it) => Number.parseInt(it, 10))
|
||||||
|
if (splitted.length !== 3) {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Date(splitted[2], splitted[1] - 1, splitted[0])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override isOfType(input: unknown): input is Date {
|
public override isOfType(input: unknown): input is Date {
|
||||||
return input instanceof Date
|
return input instanceof Date
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user