-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
date.ts
24 lines (23 loc) · 978 Bytes
/
date.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { invariant, processAdditionalPropArgs } from "../utils/utils"
import { PropSchema, AdditionalPropArgs } from "../api/types"
/**
* Similar to primitive, serializes instances of Date objects
*
* @param additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers
*/
export default function date(additionalArgs?: AdditionalPropArgs): PropSchema {
// TODO: add format option?
let result: PropSchema = {
serializer: function(value) {
if (value === null || value === undefined) return value
invariant(value instanceof Date, "Expected Date object")
return value.getTime()
},
deserializer: function(jsonValue, done) {
if (jsonValue === null || jsonValue === undefined) return void done(null, jsonValue)
return void done(null, new Date(jsonValue))
}
}
result = processAdditionalPropArgs(result, additionalArgs)
return result
}