fast-json-stringify 2.2.6 → 2.2.10

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.
@@ -0,0 +1,21 @@
1
+ # Number of days of inactivity before an issue becomes stale
2
+ daysUntilStale: 15
3
+ # Number of days of inactivity before a stale issue is closed
4
+ daysUntilClose: 7
5
+ # Issues with these labels will never be considered stale
6
+ exemptLabels:
7
+ - "discussion"
8
+ - "feature request"
9
+ - "bug"
10
+ - "help wanted"
11
+ - "plugin suggestion"
12
+ - "good first issue"
13
+ # Label to use when marking an issue as stale
14
+ staleLabel: stale
15
+ # Comment to post when marking an issue as stale. Set to `false` to disable
16
+ markComment: >
17
+ This issue has been automatically marked as stale because it has not had
18
+ recent activity. It will be closed if no further activity occurs. Thank you
19
+ for your contributions.
20
+ # Comment to post when closing a stale issue. Set to `false` to disable
21
+ closeComment: false
@@ -5,7 +5,7 @@ jobs:
5
5
  runs-on: ubuntu-latest
6
6
  strategy:
7
7
  matrix:
8
- node-version: [10.x, 12.x, 13.x]
8
+ node-version: [10.x, 12.x, 13.x, 14.x]
9
9
  steps:
10
10
  - uses: actions/checkout@v2
11
11
  - name: Use Node.js ${{ matrix.node-version }}
package/bench.js CHANGED
@@ -58,11 +58,6 @@ const obj = {
58
58
 
59
59
  const multiArray = []
60
60
 
61
- const JSTR = require('json-strify')
62
- const JSTRStringify = JSTR(schemaCJS)
63
- const JSTRArray = JSTR(arraySchemaCJS)
64
- const JSTRInstance = JSTR()
65
-
66
61
  const CJS = require('compile-json-stringify')
67
62
  const CJSStringify = CJS(schemaCJS)
68
63
  const CJSStringifyArray = CJS(arraySchemaCJS)
@@ -90,9 +85,6 @@ for (i = 0; i < 1000; i++) {
90
85
  suite.add('FJS creation', function () {
91
86
  FJS(schema)
92
87
  })
93
- suite.add('JSTR creation', function () {
94
- JSTR(schemaCJS)
95
- })
96
88
  suite.add('CJS creation', function () {
97
89
  CJS(schemaCJS)
98
90
  })
@@ -105,10 +97,6 @@ suite.add('fast-json-stringify array', function () {
105
97
  stringifyArray(multiArray)
106
98
  })
107
99
 
108
- suite.add('json-strify array', function () {
109
- JSTRArray(multiArray)
110
- })
111
-
112
100
  suite.add('compile-json-stringify array', function () {
113
101
  CJSStringifyArray(multiArray)
114
102
  })
@@ -121,10 +109,6 @@ suite.add('fast-json-stringify long string', function () {
121
109
  stringifyString(str)
122
110
  })
123
111
 
124
- suite.add('json-strify long string', function () {
125
- JSTRInstance(str)
126
- })
127
-
128
112
  suite.add('compile-json-stringify long string', function () {
129
113
  CJSStringifyString(str)
130
114
  })
@@ -137,10 +121,6 @@ suite.add('fast-json-stringify short string', function () {
137
121
  stringifyString('hello world')
138
122
  })
139
123
 
140
- suite.add('json-strify short string', function () {
141
- JSTRInstance('hello world')
142
- })
143
-
144
124
  suite.add('compile-json-stringify short string', function () {
145
125
  CJSStringifyString('hello world')
146
126
  })
@@ -153,10 +133,6 @@ suite.add('fast-json-stringify obj', function () {
153
133
  stringify(obj)
154
134
  })
155
135
 
156
- suite.add('json-strify obj', function () {
157
- JSTRStringify(obj)
158
- })
159
-
160
136
  suite.add('compile-json-stringify obj', function () {
161
137
  CJSStringify(obj)
162
138
  })
package/index.js CHANGED
@@ -916,7 +916,6 @@ function buildObject (location, code, name) {
916
916
  var addComma = false
917
917
  `
918
918
 
919
- var laterCode = ''
920
919
  var r
921
920
  if (schema.if && schema.then) {
922
921
  code += `
@@ -927,20 +926,18 @@ function buildObject (location, code, name) {
927
926
  r = buildInnerObject(location, name)
928
927
  }
929
928
 
930
- laterCode = r.laterCode
931
-
932
929
  // Removes the comma if is the last element of the string (in case there are not properties)
933
930
  code += `${r.code}
934
931
  json += '}'
935
932
  return json
936
933
  }
934
+ ${r.laterCode}
937
935
  `
938
936
 
939
- code += laterCode
940
937
  return code
941
938
  }
942
939
 
943
- function buildArray (location, code, name) {
940
+ function buildArray (location, code, name, key = null) {
944
941
  var schema = location.schema
945
942
  code += `
946
943
  function ${name} (obj) {
@@ -991,6 +988,14 @@ function buildArray (location, code, name) {
991
988
  result = nested(laterCode, name, '[i]', mergeLocation(location, { schema: schema.items }), undefined, true)
992
989
  }
993
990
 
991
+ if (key) {
992
+ code += `
993
+ if(!Array.isArray(obj)) {
994
+ throw new TypeError(\`Property '${key}' should be of type array, received '$\{obj}' instead.\`)
995
+ }
996
+ `
997
+ }
998
+
994
999
  code += `
995
1000
  var l = obj.length
996
1001
  var w = l - 1
@@ -1003,12 +1008,9 @@ function buildArray (location, code, name) {
1003
1008
  json += ']'
1004
1009
  return json
1005
1010
  }
1011
+ ${result.laterCode}
1006
1012
  `
1007
1013
 
1008
- laterCode = result.laterCode
1009
-
1010
- code += laterCode
1011
-
1012
1014
  return code
1013
1015
  }
1014
1016
 
@@ -1137,7 +1139,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1137
1139
  break
1138
1140
  case 'array':
1139
1141
  funcName = asFuncName('$arr' + name + key + subKey) // eslint-disable-line
1140
- laterCode = buildArray(location, laterCode, funcName)
1142
+ laterCode = buildArray(location, laterCode, funcName, key)
1141
1143
  code += `
1142
1144
  json += ${funcName}(obj${accessor})
1143
1145
  `
@@ -1157,7 +1159,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1157
1159
  // 2. `nested`, through `buildCode`, replaces any reference in object properties with the actual schema
1158
1160
  // (see https://github.com/fastify/fast-json-stringify/blob/6da3b3e8ac24b1ca5578223adedb4083b7adf8db/index.js#L631)
1159
1161
  code += `
1160
- ${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(location.schema, { depth: null, maxArrayLength: null })}, obj${accessor}))
1162
+ ${index === 0 ? 'if' : 'else if'}(ajv.validate(${JSON.stringify(location.schema)}, obj${accessor}))
1161
1163
  ${nestedResult.code}
1162
1164
  `
1163
1165
  laterCode = nestedResult.laterCode
@@ -1199,7 +1201,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1199
1201
 
1200
1202
  if (type === 'string') {
1201
1203
  code += `
1202
- ${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)
1204
+ ${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 || (typeof obj${accessor} === "object" && Object.hasOwnProperty.call(obj${accessor}, "toString")))
1203
1205
  ${nestedResult.code}
1204
1206
  `
1205
1207
  } else if (type === 'null') {
@@ -1246,7 +1248,9 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1246
1248
 
1247
1249
  function isEmpty (schema) {
1248
1250
  for (var key in schema) {
1249
- if (schema.hasOwnProperty(key)) return false
1251
+ if (schema.hasOwnProperty(key) && schema[key] !== undefined) {
1252
+ return false
1253
+ }
1250
1254
  }
1251
1255
  return true
1252
1256
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "2.2.6",
3
+ "version": "2.2.10",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -33,13 +33,12 @@
33
33
  "benchmark": "^2.1.4",
34
34
  "compile-json-stringify": "^0.1.2",
35
35
  "is-my-json-valid": "^2.20.0",
36
- "json-strify": "^0.1.7",
37
36
  "long": "^4.0.0",
38
37
  "moment": "^2.24.0",
39
38
  "pre-commit": "^1.2.2",
40
39
  "proxyquire": "^2.1.3",
41
40
  "semver": "^7.1.0",
42
- "standard": "^14.0.0",
41
+ "standard": "^16.0.1",
43
42
  "tap": "^14.0.0",
44
43
  "typescript": "^4.0.2"
45
44
  },
package/test/any.test.js CHANGED
@@ -33,6 +33,23 @@ test('object with nested random property', (t) => {
33
33
  }), '{"id":1,"name":["first","last"]}')
34
34
  })
35
35
 
36
+ // reference: https://github.com/fastify/fast-json-stringify/issues/259
37
+ test('object with empty schema with $id: undefined set', (t) => {
38
+ t.plan(1)
39
+
40
+ const schema = {
41
+ title: 'empty schema to allow any object with $id: undefined set',
42
+ type: 'object',
43
+ properties: {
44
+ name: { $id: undefined }
45
+ }
46
+ }
47
+ const stringify = build(schema)
48
+ t.is(stringify({
49
+ name: 'string'
50
+ }), '{"name":"string"}')
51
+ })
52
+
36
53
  test('array with random items', (t) => {
37
54
  t.plan(1)
38
55
 
@@ -197,6 +197,36 @@ test('null value in schema', (t) => {
197
197
  build(schema)
198
198
  })
199
199
 
200
+ test('symbol value in schema', (t) => {
201
+ t.plan(4)
202
+
203
+ const ObjectKind = Symbol('LiteralKind')
204
+ const UnionKind = Symbol('UnionKind')
205
+ const LiteralKind = Symbol('LiteralKind')
206
+
207
+ const schema = {
208
+ kind: ObjectKind,
209
+ type: 'object',
210
+ properties: {
211
+ value: {
212
+ kind: UnionKind,
213
+ anyOf: [
214
+ { kind: LiteralKind, type: 'string', enum: ['foo'] },
215
+ { kind: LiteralKind, type: 'string', enum: ['bar'] },
216
+ { kind: LiteralKind, type: 'string', enum: ['baz'] }
217
+ ]
218
+ }
219
+ },
220
+ required: ['value']
221
+ }
222
+
223
+ const stringify = build(schema)
224
+ t.is(stringify({ value: 'foo' }), '{"value":"foo"}')
225
+ t.is(stringify({ value: 'bar' }), '{"value":"bar"}')
226
+ t.is(stringify({ value: 'baz' }), '{"value":"baz"}')
227
+ t.is(stringify({ value: 'qux' }), '{"value":null}')
228
+ })
229
+
200
230
  test('anyOf and $ref together', (t) => {
201
231
  t.plan(2)
202
232
 
@@ -365,3 +365,73 @@ test('string type array can handle dates', (t) => {
365
365
  })
366
366
  t.is(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
367
367
  })
368
+
369
+ test('object that is simultaneously a string and a json', (t) => {
370
+ t.plan(2)
371
+ const schema = {
372
+ type: 'object',
373
+ properties: {
374
+ simultaneously: {
375
+ type: ['string', 'object'],
376
+ properties: {
377
+ foo: { type: 'string' }
378
+ }
379
+ }
380
+ }
381
+ }
382
+
383
+ const likeObjectId = {
384
+ toString () { return 'hello' }
385
+ }
386
+
387
+ const stringify = build(schema)
388
+ const valueStr = stringify({ simultaneously: likeObjectId })
389
+ t.is(valueStr, '{"simultaneously":"hello"}')
390
+
391
+ const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
392
+ t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
393
+ })
394
+
395
+ test('object that is simultaneously a string and a json switched', (t) => {
396
+ t.plan(2)
397
+ const schema = {
398
+ type: 'object',
399
+ properties: {
400
+ simultaneously: {
401
+ type: ['object', 'string'],
402
+ properties: {
403
+ foo: { type: 'string' }
404
+ }
405
+ }
406
+ }
407
+ }
408
+
409
+ const likeObjectId = {
410
+ toString () { return 'hello' }
411
+ }
412
+
413
+ const stringify = build(schema)
414
+ const valueStr = stringify({ simultaneously: likeObjectId })
415
+ t.is(valueStr, '{"simultaneously":{}}')
416
+
417
+ const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
418
+ t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
419
+ })
420
+
421
+ test('should throw an error when type is array and object is null', (t) => {
422
+ t.plan(1)
423
+ const schema = {
424
+ type: 'object',
425
+ properties: {
426
+ arr: {
427
+ type: 'array',
428
+ items: {
429
+ type: 'number'
430
+ }
431
+ }
432
+ }
433
+ }
434
+
435
+ const stringify = build(schema)
436
+ t.throws(() => stringify({ arr: null }), new TypeError('Property \'arr\' should be of type array, received \'null\' instead.'))
437
+ })