fast-json-stringify 3.1.0 → 3.2.0

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.
@@ -21,13 +21,13 @@ jobs:
21
21
  matrix:
22
22
  node-version: [12, 14, 16]
23
23
  steps:
24
- - uses: actions/checkout@v2
24
+ - uses: actions/checkout@v3
25
25
  with:
26
26
  persist-credentials: false
27
27
  ref: ${{github.event.pull_request.head.sha}}
28
28
  repository: ${{github.event.pull_request.head.repo.full_name}}
29
29
 
30
- - uses: actions/setup-node@v2
30
+ - uses: actions/setup-node@v3
31
31
  with:
32
32
  node-version: ${{ matrix.node-version }}
33
33
 
@@ -46,7 +46,7 @@ jobs:
46
46
  echo "::set-output name=BENCH_RESULT${{matrix.node-version}}::$content"
47
47
 
48
48
  # main benchmark
49
- - uses: actions/checkout@v2
49
+ - uses: actions/checkout@v3
50
50
  with:
51
51
  ref: 'master'
52
52
 
@@ -19,10 +19,10 @@ jobs:
19
19
  os: [macos-latest, ubuntu-latest, windows-latest]
20
20
 
21
21
  steps:
22
- - uses: actions/checkout@v2
22
+ - uses: actions/checkout@v3
23
23
 
24
24
  - name: Use Node.js
25
- uses: actions/setup-node@v2
25
+ uses: actions/setup-node@v3
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
 
package/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { Options as AjvOptions } from "ajv"
2
2
  declare namespace build {
3
3
  interface BaseSchema {
4
+ /**
5
+ * Schema id
6
+ */
7
+ $id?: string
4
8
  /**
5
9
  * Schema title
6
10
  */
package/index.js CHANGED
@@ -52,11 +52,14 @@ function mergeLocation (source, dest) {
52
52
 
53
53
  const arrayItemsReferenceSerializersMap = new Map()
54
54
  const objectReferenceSerializersMap = new Map()
55
+ const schemaReferenceMap = new Map()
56
+
55
57
  let ajvInstance = null
56
58
 
57
59
  function build (schema, options) {
58
60
  arrayItemsReferenceSerializersMap.clear()
59
61
  objectReferenceSerializersMap.clear()
62
+ schemaReferenceMap.clear()
60
63
 
61
64
  options = options || {}
62
65
 
@@ -174,6 +177,7 @@ function build (schema, options) {
174
177
 
175
178
  arrayItemsReferenceSerializersMap.clear()
176
179
  objectReferenceSerializersMap.clear()
180
+ schemaReferenceMap.clear()
177
181
 
178
182
  return (Function.apply(null, dependenciesName).apply(null, dependencies))
179
183
  }
@@ -605,15 +609,25 @@ function refFinder (ref, location) {
605
609
  return {
606
610
  schema: externalSchema[ref],
607
611
  root: externalSchema[ref],
608
- externalSchema: externalSchema
612
+ externalSchema
609
613
  }
610
614
  }
611
615
 
612
616
  // Split file from walk
613
617
  ref = ref.split('#')
614
618
 
615
- // If external file
616
- if (ref[0]) {
619
+ // Check schemaReferenceMap for $id entry
620
+ if (ref[0] && schemaReferenceMap.has(ref[0])) {
621
+ schema = schemaReferenceMap.get(ref[0])
622
+ root = schemaReferenceMap.get(ref[0])
623
+ if (schema.$ref) {
624
+ return refFinder(schema.$ref, {
625
+ schema,
626
+ root,
627
+ externalSchema
628
+ })
629
+ }
630
+ } else if (ref[0]) { // If external file
617
631
  schema = externalSchema[ref[0]]
618
632
  root = externalSchema[ref[0]]
619
633
 
@@ -623,9 +637,9 @@ function refFinder (ref, location) {
623
637
 
624
638
  if (schema.$ref) {
625
639
  return refFinder(schema.$ref, {
626
- schema: schema,
627
- root: root,
628
- externalSchema: externalSchema
640
+ schema,
641
+ root,
642
+ externalSchema
629
643
  })
630
644
  }
631
645
  }
@@ -652,8 +666,8 @@ function refFinder (ref, location) {
652
666
 
653
667
  return {
654
668
  schema: dereferenced,
655
- root: root,
656
- externalSchema: externalSchema
669
+ root,
670
+ externalSchema
657
671
  }
658
672
  } else {
659
673
  // eslint-disable-next-line
@@ -674,16 +688,16 @@ function refFinder (ref, location) {
674
688
 
675
689
  if (result.$ref) {
676
690
  return refFinder(result.$ref, {
677
- schema: schema,
678
- root: root,
679
- externalSchema: externalSchema
691
+ schema,
692
+ root,
693
+ externalSchema
680
694
  })
681
695
  }
682
696
 
683
697
  return {
684
698
  schema: result,
685
- root: root,
686
- externalSchema: externalSchema
699
+ root,
700
+ externalSchema
687
701
  }
688
702
 
689
703
  function findBadKey (obj, keys) {
@@ -814,7 +828,7 @@ function buildCode (location, code, laterCode, name) {
814
828
  laterCode = builtCode.laterCode
815
829
  }
816
830
 
817
- return { code: code, laterCode: laterCode }
831
+ return { code, laterCode }
818
832
  }
819
833
 
820
834
  function filterRequired (required, key) {
@@ -838,7 +852,7 @@ function buildCodeWithAllOfs (location, code, laterCode, name) {
838
852
  laterCode = builtCode.laterCode
839
853
  }
840
854
 
841
- return { code: code, laterCode: laterCode }
855
+ return { code, laterCode }
842
856
  }
843
857
 
844
858
  function buildInnerObject (location, name) {
@@ -909,7 +923,7 @@ function addIfThenElse (location, name) {
909
923
  code += `
910
924
  }
911
925
  `
912
- return { code: code, laterCode: laterCode }
926
+ return { code, laterCode }
913
927
  }
914
928
 
915
929
  function toJSON (variableName) {
@@ -921,7 +935,9 @@ function toJSON (variableName) {
921
935
 
922
936
  function buildObject (location, code, name) {
923
937
  const schema = location.schema
924
-
938
+ if (schema.$id !== undefined) {
939
+ schemaReferenceMap.set(schema.$id, schema)
940
+ }
925
941
  code += `
926
942
  function ${name} (input) {
927
943
  `
@@ -971,6 +987,9 @@ function buildObject (location, code, name) {
971
987
 
972
988
  function buildArray (location, code, name, key = null) {
973
989
  let schema = location.schema
990
+ if (schema.$id !== undefined) {
991
+ schemaReferenceMap.set(schema.$id, schema)
992
+ }
974
993
  code += `
975
994
  function ${name} (obj) {
976
995
  `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -38,7 +38,7 @@
38
38
  "pre-commit": "^1.2.2",
39
39
  "proxyquire": "^2.1.3",
40
40
  "semver": "^7.1.0",
41
- "standard": "^16.0.1",
41
+ "standard": "^17.0.0",
42
42
  "tap": "^16.0.1",
43
43
  "typescript": "^4.0.2",
44
44
  "webpack": "^5.40.0"
@@ -261,13 +261,13 @@ t.test('if-then-else', t => {
261
261
  const tests = [
262
262
  {
263
263
  name: 'foobar',
264
- schema: schema,
264
+ schema,
265
265
  input: fooBarInput,
266
266
  expected: foobarOutput
267
267
  },
268
268
  {
269
269
  name: 'greeting',
270
- schema: schema,
270
+ schema,
271
271
  input: greetingInput,
272
272
  expected: greetingOutput
273
273
  },
@@ -9,11 +9,11 @@ const nullable = true
9
9
  const complexObject = {
10
10
  type: 'object',
11
11
  properties: {
12
- nullableString: { type: 'string', nullable: nullable },
13
- nullableNumber: { type: 'number', nullable: nullable },
14
- nullableInteger: { type: 'integer', nullable: nullable },
15
- nullableBoolean: { type: 'boolean', nullable: nullable },
16
- nullableNull: { type: 'null', nullable: nullable },
12
+ nullableString: { type: 'string', nullable },
13
+ nullableNumber: { type: 'number', nullable },
14
+ nullableInteger: { type: 'integer', nullable },
15
+ nullableBoolean: { type: 'boolean', nullable },
16
+ nullableNull: { type: 'null', nullable },
17
17
  nullableArray: {
18
18
  type: 'array',
19
19
  nullable: true,
@@ -25,11 +25,11 @@ const complexObject = {
25
25
  nullable: false,
26
26
  additionalProperties: true,
27
27
  properties: {
28
- nullableString: { type: 'string', nullable: nullable },
29
- nullableNumber: { type: 'number', nullable: nullable },
30
- nullableInteger: { type: 'integer', nullable: nullable },
31
- nullableBoolean: { type: 'boolean', nullable: nullable },
32
- nullableNull: { type: 'null', nullable: nullable },
28
+ nullableString: { type: 'string', nullable },
29
+ nullableNumber: { type: 'number', nullable },
30
+ nullableInteger: { type: 'integer', nullable },
31
+ nullableBoolean: { type: 'boolean', nullable },
32
+ nullableNull: { type: 'null', nullable },
33
33
  nullableArray: {
34
34
  type: 'array',
35
35
  nullable: true,
@@ -86,11 +86,11 @@ const complexExpectedResult = {
86
86
  }
87
87
 
88
88
  const testSet = {
89
- nullableString: [{ type: 'string', nullable: nullable }, null, null],
90
- nullableNumber: [{ type: 'number', nullable: nullable }, null, null],
91
- nullableInteger: [{ type: 'integer', nullable: nullable }, null, null],
92
- nullableBoolean: [{ type: 'boolean', nullable: nullable }, null, null],
93
- nullableNull: [{ type: 'null', nullable: nullable }, null, null],
89
+ nullableString: [{ type: 'string', nullable }, null, null],
90
+ nullableNumber: [{ type: 'number', nullable }, null, null],
91
+ nullableInteger: [{ type: 'integer', nullable }, null, null],
92
+ nullableBoolean: [{ type: 'boolean', nullable }, null, null],
93
+ nullableNull: [{ type: 'null', nullable }, null, null],
94
94
  nullableArray: [{
95
95
  type: 'array',
96
96
  nullable: true,
@@ -178,3 +178,68 @@ test('can stringify recursive references in object types (issue #365)', t => {
178
178
  const value = stringify(data)
179
179
  t.equal(value, '{"category":{"parent":{"parent":{"parent":{"parent":{}}}}}}')
180
180
  })
181
+
182
+ test('can stringify recursive inline $id references (issue #410)', t => {
183
+ t.plan(1)
184
+ const schema = {
185
+ $id: 'Node',
186
+ type: 'object',
187
+ properties: {
188
+ id: {
189
+ type: 'string'
190
+ },
191
+ nodes: {
192
+ type: 'array',
193
+ items: {
194
+ $ref: 'Node'
195
+ }
196
+ }
197
+ },
198
+ required: [
199
+ 'id',
200
+ 'nodes'
201
+ ]
202
+ }
203
+
204
+ const stringify = build(schema)
205
+ const data = {
206
+ id: '0',
207
+ nodes: [
208
+ {
209
+ id: '1',
210
+ nodes: [{
211
+ id: '2',
212
+ nodes: [
213
+ { id: '3', nodes: [] },
214
+ { id: '4', nodes: [] },
215
+ { id: '5', nodes: [] }
216
+ ]
217
+ }]
218
+ },
219
+ {
220
+ id: '6',
221
+ nodes: [{
222
+ id: '7',
223
+ nodes: [
224
+ { id: '8', nodes: [] },
225
+ { id: '9', nodes: [] },
226
+ { id: '10', nodes: [] }
227
+ ]
228
+ }]
229
+ },
230
+ {
231
+ id: '11',
232
+ nodes: [{
233
+ id: '12',
234
+ nodes: [
235
+ { id: '13', nodes: [] },
236
+ { id: '14', nodes: [] },
237
+ { id: '15', nodes: [] }
238
+ ]
239
+ }]
240
+ }
241
+ ]
242
+ }
243
+ const value = stringify(data)
244
+ t.equal(value, '{"id":"0","nodes":[{"id":"1","nodes":[{"id":"2","nodes":[{"id":"3","nodes":[]},{"id":"4","nodes":[]},{"id":"5","nodes":[]}]}]},{"id":"6","nodes":[{"id":"7","nodes":[{"id":"8","nodes":[]},{"id":"9","nodes":[]},{"id":"10","nodes":[]}]}]},{"id":"11","nodes":[{"id":"12","nodes":[{"id":"13","nodes":[]},{"id":"14","nodes":[]},{"id":"15","nodes":[]}]}]}]}')
245
+ })