File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,9 @@ export class Serializer {
51
51
if ( data === true || data === false ) {
52
52
return data ;
53
53
}
54
+ if ( data instanceof Date ) {
55
+ return data . toISOString ( ) ;
56
+ }
54
57
if ( Object . prototype . toString . call ( data ) === '[object String]' ) {
55
58
return data ;
56
59
}
@@ -95,6 +98,14 @@ export class Serializer {
95
98
if ( typeof json === 'function' || typeof json === 'object' ) {
96
99
return mapValues ( json ! , x => this . decode ( x ) ) ;
97
100
}
101
+ if (
102
+ typeof json === 'string' &&
103
+ / ^ ( - ? (?: [ 1 - 9 ] [ 0 - 9 ] * ) ? [ 0 - 9 ] { 4 } ) - ( 1 [ 0 - 2 ] | 0 [ 1 - 9 ] ) - ( 3 [ 0 1 ] | 0 [ 1 - 9 ] | [ 1 2 ] [ 0 - 9 ] ) T ( 2 [ 0 - 3 ] | [ 0 1 ] [ 0 - 9 ] ) : ( [ 0 - 5 ] [ 0 - 9 ] ) : ( [ 0 - 5 ] [ 0 - 9 ] ) ( .[ 0 - 9 ] + ) ? ( Z ) ? $ / . test (
104
+ json
105
+ )
106
+ ) {
107
+ return new Date ( json ) ;
108
+ }
98
109
// Anything else is safe to return.
99
110
return json ;
100
111
}
Original file line number Diff line number Diff line change @@ -80,6 +80,18 @@ describe('Serializer', () => {
80
80
expect ( serializer . decode ( 1.2 ) ) . to . equal ( 1.2 ) ;
81
81
} ) ;
82
82
83
+ it ( 'encodes date' , ( ) => {
84
+ expect ( serializer . encode ( new Date ( '2021-02-10T13:26:37.977Z' ) ) ) . to . equal (
85
+ '2021-02-10T13:26:37.977Z'
86
+ ) ;
87
+ } ) ;
88
+
89
+ it ( 'decodes date' , ( ) => {
90
+ expect ( serializer . decode ( '2021-02-10T13:26:37.977Z' ) ) . to . deep . equal (
91
+ new Date ( '2021-02-10T13:26:37.977Z' )
92
+ ) ;
93
+ } ) ;
94
+
83
95
it ( 'encodes string' , ( ) => {
84
96
expect ( serializer . encode ( 'hello' ) ) . to . equal ( 'hello' ) ;
85
97
} ) ;
You can’t perform that action at this time.
0 commit comments