fast-json-stringify 1.20.0 → 1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -279,7 +279,7 @@ function $asBooleanNullable (bool) {
279
279
  function $asDatetime (date) {
280
280
  if (date instanceof Date) {
281
281
  return '"' + date.toISOString() + '"'
282
- } else if (typeof date.toISOString === 'function') {
282
+ } else if (date && typeof date.toISOString === 'function') {
283
283
  return '"' + date.toISOString() + '"'
284
284
  } else {
285
285
  return $asString(date)
@@ -292,7 +292,7 @@ function $asDate (date) {
292
292
  var month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(date)
293
293
  var day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date)
294
294
  return '"' + year + '-' + month + '-' + day + '"'
295
- } else if (typeof date.format === 'function') {
295
+ } else if (date && typeof date.format === 'function') {
296
296
  return '"' + date.format('YYYY-MM-DD') + '"'
297
297
  } else {
298
298
  return $asString(date)
@@ -305,7 +305,7 @@ function $asTime (date) {
305
305
  var minute = new Intl.DateTimeFormat('en', { minute: 'numeric' }).format(date)
306
306
  var second = new Intl.DateTimeFormat('en', { second: 'numeric' }).format(date)
307
307
  return '"' + $pad2Zeros(hour) + ':' + $pad2Zeros(minute) + ':' + $pad2Zeros(second) + '"'
308
- } else if (typeof date.format === 'function') {
308
+ } else if (date && typeof date.format === 'function') {
309
309
  return '"' + date.format('HH:mm:ss') + '"'
310
310
  } else {
311
311
  return $asString(date)
@@ -1077,9 +1077,10 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
1077
1077
  sortedTypes.forEach((type, index) => {
1078
1078
  var tempSchema = Object.assign({}, schema, { type })
1079
1079
  var nestedResult = nested(laterCode, name, key, tempSchema, externalSchema, fullSchema, subKey)
1080
+
1080
1081
  if (type === 'string') {
1081
1082
  code += `
1082
- ${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp)
1083
+ ${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp)
1083
1084
  ${nestedResult.code}
1084
1085
  `
1085
1086
  } else if (type === 'null') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "1.20.0",
3
+ "version": "1.20.1",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/test/date.test.js CHANGED
@@ -91,7 +91,6 @@ test('render a date in a string when format is time as HH:mm:ss', (t) => {
91
91
  const output = stringify(toStringify)
92
92
 
93
93
  validate(JSON.parse(output))
94
- console.log(validate.errors)
95
94
 
96
95
  t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
97
96
  t.ok(validate(JSON.parse(output)), 'valid schema')
@@ -112,7 +111,6 @@ test('verify padding for rendered date in a string when format is time', (t) =>
112
111
  const output = stringify(toStringify)
113
112
 
114
113
  validate(JSON.parse(output))
115
- console.log(validate.errors)
116
114
 
117
115
  t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
118
116
  t.ok(validate(JSON.parse(output)), 'valid schema')
@@ -194,3 +192,211 @@ test('render a nested object in a string when type is date-format as ISOString',
194
192
  t.equal(output, JSON.stringify(toStringify))
195
193
  t.ok(validate(JSON.parse(output)), 'valid schema')
196
194
  })
195
+
196
+ test('serializing null value', t => {
197
+ const input = { updatedAt: null }
198
+
199
+ function createSchema (properties) {
200
+ return {
201
+ title: 'an object in a string',
202
+ type: 'object',
203
+ properties
204
+ }
205
+ }
206
+
207
+ function serialize (schema, input) {
208
+ const validate = validator(schema)
209
+ const stringify = build(schema)
210
+ const output = stringify(input)
211
+
212
+ return {
213
+ validate,
214
+ output
215
+ }
216
+ }
217
+
218
+ t.plan(3)
219
+
220
+ t.test('type::string', t => {
221
+ t.plan(3)
222
+
223
+ t.test('format::date-time', t => {
224
+ t.plan(2)
225
+
226
+ const prop = {
227
+ updatedAt: {
228
+ type: 'string',
229
+ format: 'date-time'
230
+ }
231
+ }
232
+
233
+ const {
234
+ output,
235
+ validate
236
+ } = serialize(createSchema(prop), input)
237
+
238
+ t.equal(output, '{"updatedAt":""}')
239
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
240
+ })
241
+
242
+ t.test('format::date', t => {
243
+ t.plan(2)
244
+
245
+ const prop = {
246
+ updatedAt: {
247
+ type: 'string',
248
+ format: 'date'
249
+ }
250
+ }
251
+
252
+ const {
253
+ output,
254
+ validate
255
+ } = serialize(createSchema(prop), input)
256
+
257
+ t.equal(output, '{"updatedAt":""}')
258
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
259
+ })
260
+
261
+ t.test('format::time', t => {
262
+ t.plan(2)
263
+
264
+ const prop = {
265
+ updatedAt: {
266
+ type: 'string',
267
+ format: 'time'
268
+ }
269
+ }
270
+
271
+ const {
272
+ output,
273
+ validate
274
+ } = serialize(createSchema(prop), input)
275
+
276
+ t.equal(output, '{"updatedAt":""}')
277
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
278
+ })
279
+ })
280
+
281
+ t.test('type::array', t => {
282
+ t.plan(3)
283
+
284
+ t.test('format::date-time', t => {
285
+ t.plan(2)
286
+
287
+ const prop = {
288
+ updatedAt: {
289
+ type: ['string'],
290
+ format: 'date-time'
291
+ }
292
+ }
293
+
294
+ const {
295
+ output,
296
+ validate
297
+ } = serialize(createSchema(prop), input)
298
+
299
+ t.equal(output, '{"updatedAt":""}')
300
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
301
+ })
302
+
303
+ t.test('format::date', t => {
304
+ t.plan(2)
305
+
306
+ const prop = {
307
+ updatedAt: {
308
+ type: ['string'],
309
+ format: 'date'
310
+ }
311
+ }
312
+
313
+ const {
314
+ output,
315
+ validate
316
+ } = serialize(createSchema(prop), input)
317
+
318
+ t.equal(output, '{"updatedAt":""}')
319
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
320
+ })
321
+
322
+ t.test('format::time', t => {
323
+ t.plan(2)
324
+
325
+ const prop = {
326
+ updatedAt: {
327
+ type: ['string'],
328
+ format: 'time'
329
+ }
330
+ }
331
+
332
+ const {
333
+ output,
334
+ validate
335
+ } = serialize(createSchema(prop), input)
336
+
337
+ t.equal(output, '{"updatedAt":""}')
338
+ t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
339
+ })
340
+ })
341
+
342
+ t.test('type::array::nullable', t => {
343
+ t.plan(3)
344
+
345
+ t.test('format::date-time', t => {
346
+ t.plan(2)
347
+
348
+ const prop = {
349
+ updatedAt: {
350
+ type: ['string', 'null'],
351
+ format: 'date-time'
352
+ }
353
+ }
354
+
355
+ const {
356
+ output,
357
+ validate
358
+ } = serialize(createSchema(prop), input)
359
+
360
+ t.equal(output, '{"updatedAt":null}')
361
+ t.ok(validate(JSON.parse(output)), 'valid schema')
362
+ })
363
+
364
+ t.test('format::date', t => {
365
+ t.plan(2)
366
+
367
+ const prop = {
368
+ updatedAt: {
369
+ type: ['string', 'null'],
370
+ format: 'date'
371
+ }
372
+ }
373
+
374
+ const {
375
+ output,
376
+ validate
377
+ } = serialize(createSchema(prop), input)
378
+
379
+ t.equal(output, '{"updatedAt":null}')
380
+ t.ok(validate(JSON.parse(output)), 'valid schema')
381
+ })
382
+
383
+ t.test('format::time', t => {
384
+ t.plan(2)
385
+
386
+ const prop = {
387
+ updatedAt: {
388
+ type: ['string', 'null'],
389
+ format: 'time'
390
+ }
391
+ }
392
+
393
+ const {
394
+ output,
395
+ validate
396
+ } = serialize(createSchema(prop), input)
397
+
398
+ t.equal(output, '{"updatedAt":null}')
399
+ t.ok(validate(JSON.parse(output)), 'valid schema')
400
+ })
401
+ })
402
+ })