| 12 | * Date type |
| 13 | */ |
| 14 | export class DateType implements Type<Date> { |
| 15 | readonly name = 'date'; |
| 16 | |
| 17 | isInstance(value: any) { |
| 18 | return value == null || value instanceof Date; |
| 19 | } |
| 20 | |
| 21 | isCoercible(value: any): boolean { |
| 22 | // Please note new Date(...) allows the following |
| 23 | /* |
| 24 | > new Date('1') |
| 25 | 2001-01-01T08:00:00.000Z |
| 26 | > new Date('0') |
| 27 | 2000-01-01T08:00:00.000Z |
| 28 | > new Date(1) |
| 29 | 1970-01-01T00:00:00.001Z |
| 30 | > new Date(0) |
| 31 | 1970-01-01T00:00:00.000Z |
| 32 | > new Date(true) |
| 33 | 1970-01-01T00:00:00.001Z |
| 34 | > new Date(false) |
| 35 | 1970-01-01T00:00:00.000Z |
| 36 | */ |
| 37 | return value == null || !isNaN(new Date(value).getTime()); |
| 38 | } |
| 39 | |
| 40 | defaultValue() { |
| 41 | return new Date(); |
| 42 | } |
| 43 | |
| 44 | coerce(value: any) { |
| 45 | if (value == null) return value; |
| 46 | if (value instanceof Date) { |
| 47 | return value; |
| 48 | } |
| 49 | const d = new Date(value); |
| 50 | if (isNaN(d.getTime())) { |
| 51 | const msg = util.format('Invalid %s: %j', this.name, value); |
| 52 | throw new TypeError(msg); |
| 53 | } |
| 54 | return d; |
| 55 | } |
| 56 | |
| 57 | serialize(value: Date | null | undefined) { |
| 58 | if (value == null) return value; |
| 59 | return value.toJSON(); |
| 60 | } |
| 61 | } |
nothing calls this directly
no outgoing calls
no test coverage detected