diff --git a/src/items/date.ts b/src/items/date.ts index 81812bd..03bd2c8 100644 --- a/src/items/date.ts +++ b/src/items/date.ts @@ -1,7 +1,32 @@ +import { parceable } from 'Schema' import SchemaItem from '../SchemaItem' export default class SchemaDate extends SchemaItem { + @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 { return input instanceof Date }