File tree Expand file tree Collapse file tree 4 files changed +32
-3
lines changed Expand file tree Collapse file tree 4 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,9 @@ Supported types:
5252 * `object'
5353 * `null'
5454
55+ And nested ones, too.
56+ ` Date ` instances are serialized with ` toISOString() ` .
57+
5558## License
5659
5760MIT
Original file line number Diff line number Diff line change @@ -14,12 +14,16 @@ const stringify = fastJson({
1414 age : {
1515 description : 'Age in years' ,
1616 type : 'integer'
17+ } ,
18+ now : {
19+ type : 'string'
1720 }
1821 }
1922} )
2023
2124console . log ( stringify ( {
2225 firstName : 'Matteo' ,
2326 lastName : 'Collina' ,
24- age : 32
27+ age : 32 ,
28+ now : new Date ( )
2529} ) )
Original file line number Diff line number Diff line change @@ -57,8 +57,13 @@ function $asNumber (i) {
5757 }
5858}
5959
60- function $asString ( s ) {
61- var str = s . toString ( )
60+ function $asString ( str ) {
61+ if ( str instanceof Date ) {
62+ str = str . toISOString ( )
63+ } else {
64+ str = str . toString ( )
65+ }
66+
6267 var result = ''
6368 var last = 0
6469 var l = str . length
Original file line number Diff line number Diff line change @@ -178,3 +178,20 @@ buildTest({
178178 name : 'Dave'
179179 } ]
180180} )
181+
182+ test ( 'render a a date in a string as JSON' , ( t ) => {
183+ t . plan ( 2 )
184+
185+ const schema = {
186+ title : 'a date in a string' ,
187+ type : 'string'
188+ }
189+ const toStringify = new Date ( )
190+
191+ const validate = validator ( schema )
192+ const stringify = build ( schema )
193+ const output = stringify ( toStringify )
194+
195+ t . equal ( output , JSON . stringify ( toStringify ) )
196+ t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
197+ } )
You can’t perform that action at this time.
0 commit comments