fast-json-stringify 2.7.10 → 2.7.11

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,111 @@
1
+ name: Benchmark
2
+
3
+ on:
4
+ pull_request_target:
5
+ types: [labeled]
6
+
7
+ jobs:
8
+ benchmark:
9
+ if: ${{ github.event.label.name == 'benchmark' }}
10
+ runs-on: ubuntu-latest
11
+ outputs:
12
+ PR-BENCH-12: ${{ steps.benchmark-pr.outputs.BENCH_RESULT12 }}
13
+ PR-BENCH-14: ${{ steps.benchmark-pr.outputs.BENCH_RESULT14 }}
14
+ PR-BENCH-16: ${{ steps.benchmark-pr.outputs.BENCH_RESULT16 }}
15
+ MAIN-BENCH-12: ${{ steps.benchmark-main.outputs.BENCH_RESULT12 }}
16
+ MAIN-BENCH-14: ${{ steps.benchmark-main.outputs.BENCH_RESULT14 }}
17
+ MAIN-BENCH-16: ${{ steps.benchmark-main.outputs.BENCH_RESULT16 }}
18
+ strategy:
19
+ matrix:
20
+ node-version: [12, 14, 16]
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ with:
24
+ ref: ${{github.event.pull_request.head.ref}}
25
+ repository: ${{github.event.pull_request.head.repo.full_name}}
26
+
27
+ - uses: actions/setup-node@v2
28
+ with:
29
+ node-version: ${{ matrix.node-version }}
30
+
31
+ - name: Install
32
+ run: |
33
+ npm install --ignore-scripts
34
+
35
+ - name: Run benchmark
36
+ id: benchmark-pr
37
+ run: |
38
+ npm run --silent benchmark > ./bench-result
39
+ content=$(cat ./bench-result)
40
+ content="${content//'%'/'%25'}"
41
+ content="${content//$'\n'/'%0A'}"
42
+ content="${content//$'\r'/'%0D'}"
43
+ echo "::set-output name=BENCH_RESULT${{matrix.node-version}}::$content"
44
+
45
+ # main benchmark
46
+ - uses: actions/checkout@v2
47
+ with:
48
+ ref: 'master'
49
+
50
+ - name: Install
51
+ run: |
52
+ npm install --ignore-scripts
53
+
54
+ - name: Run benchmark
55
+ id: benchmark-master
56
+ run: |
57
+ npm run --silent benchmark > ./bench-result
58
+ content=$(cat ./bench-result)
59
+ content="${content//'%'/'%25'}"
60
+ content="${content//$'\n'/'%0A'}"
61
+ content="${content//$'\r'/'%0D'}"
62
+ echo "::set-output name=BENCH_RESULT${{matrix.node-version}}::$content"
63
+
64
+ output-benchmark:
65
+ needs: [benchmark]
66
+ runs-on: ubuntu-latest
67
+ steps:
68
+ - name: Comment PR
69
+ uses: thollander/actions-comment-pull-request@v1
70
+ with:
71
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72
+ message: |
73
+ **Node**: 12
74
+ **PR**:
75
+ ```
76
+ ${{ needs.benchmark.outputs.PR-BENCH-12 }}
77
+ ```
78
+ **MAIN**:
79
+ ```
80
+ ${{ needs.benchmark.outputs.MAIN-BENCH-12 }}
81
+ ```
82
+
83
+ ---
84
+
85
+ **Node**: 14
86
+ **PR**:
87
+ ```
88
+ ${{ needs.benchmark.outputs.PR-BENCH-14 }}
89
+ ```
90
+ **MAIN**:
91
+ ```
92
+ ${{ needs.benchmark.outputs.MAIN-BENCH-14 }}
93
+ ```
94
+
95
+ ---
96
+
97
+ **Node**: 16
98
+ **PR**:
99
+ ```
100
+ ${{ needs.benchmark.outputs.PR-BENCH-16 }}
101
+ ```
102
+ **MAIN**:
103
+ ```
104
+ ${{ needs.benchmark.outputs.MAIN-BENCH-16 }}
105
+ ```
106
+
107
+ - uses: actions-ecosystem/action-remove-labels@v1
108
+ with:
109
+ labels: |
110
+ benchmark
111
+ github_token: ${{ secrets.GITHUB_TOKEN }}
@@ -38,6 +38,6 @@ jobs:
38
38
  needs: test
39
39
  runs-on: ubuntu-latest
40
40
  steps:
41
- - uses: fastify/github-action-merge-dependabot@v2.4.0
41
+ - uses: fastify/github-action-merge-dependabot@v2.5.0
42
42
  with:
43
43
  github-token: ${{ secrets.GITHUB_TOKEN }}
package/index.js CHANGED
@@ -47,7 +47,10 @@ function mergeLocation (source, dest) {
47
47
  }
48
48
  }
49
49
 
50
+ const referenceSerializersMap = new Map()
51
+
50
52
  function build (schema, options) {
53
+ referenceSerializersMap.clear()
51
54
  options = options || {}
52
55
  isValidSchema(schema)
53
56
  if (options.schema) {
@@ -144,6 +147,8 @@ function build (schema, options) {
144
147
  return dependenciesName
145
148
  }
146
149
 
150
+ referenceSerializersMap.clear()
151
+
147
152
  return (Function.apply(null, dependenciesName).apply(null, dependencies))
148
153
  }
149
154
 
@@ -969,8 +974,18 @@ function buildArray (location, code, name, key = null) {
969
974
  schema = location.schema
970
975
  schema[fjsCloned] = true
971
976
  }
977
+
972
978
  location = refFinder(schema.items.$ref, location)
973
979
  schema.items = location.schema
980
+
981
+ if (referenceSerializersMap.has(schema.items)) {
982
+ code += `
983
+ return ${referenceSerializersMap.get(schema.items)}(obj)
984
+ }
985
+ `
986
+ return code
987
+ }
988
+ referenceSerializersMap.set(schema.items, name)
974
989
  }
975
990
 
976
991
  let result = { code: '', laterCode: '' }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "2.7.10",
3
+ "version": "2.7.11",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,137 @@
1
+ 'use strict'
2
+
3
+ const test = require('tap').test
4
+ const build = require('..')
5
+
6
+ test('can stringify recursive directory tree (issue #181)', (t) => {
7
+ t.plan(1)
8
+
9
+ const schema = {
10
+ definitions: {
11
+ directory: {
12
+ type: 'object',
13
+ properties: {
14
+ name: { type: 'string' },
15
+ subDirectories: {
16
+ type: 'array',
17
+ items: { $ref: '#/definitions/directory' },
18
+ default: []
19
+ }
20
+ }
21
+ }
22
+ },
23
+ type: 'array',
24
+ items: { $ref: '#/definitions/directory' }
25
+ }
26
+ const stringify = build(schema)
27
+
28
+ t.equal(stringify([
29
+ { name: 'directory 1', subDirectories: [] },
30
+ {
31
+ name: 'directory 2',
32
+ subDirectories: [
33
+ { name: 'directory 2.1', subDirectories: [] },
34
+ { name: 'directory 2.2', subDirectories: [] }
35
+ ]
36
+ }
37
+ ]), '[{"name":"directory 1","subDirectories":[]},{"name":"directory 2","subDirectories":[{"name":"directory 2.1","subDirectories":[]},{"name":"directory 2.2","subDirectories":[]}]}]')
38
+ })
39
+
40
+ test('can stringify when recursion in external schema', t => {
41
+ t.plan(1)
42
+
43
+ const referenceSchema = {
44
+ $id: 'person',
45
+ type: 'object',
46
+ properties: {
47
+ name: { type: 'string' },
48
+ children: {
49
+ type: 'array',
50
+ items: { $ref: '#' }
51
+ }
52
+ }
53
+ }
54
+
55
+ const schema = {
56
+ $id: 'mainSchema',
57
+ type: 'object',
58
+ properties: {
59
+ people: {
60
+ $ref: 'person'
61
+ }
62
+ }
63
+ }
64
+ const stringify = build(schema, {
65
+ schema: {
66
+ [referenceSchema.$id]: referenceSchema
67
+ }
68
+ })
69
+
70
+ const value = stringify({ people: { name: 'Elizabeth', children: [{ name: 'Charles' }] } })
71
+ t.equal(value, '{"people":{"name":"Elizabeth","children":[{"name":"Charles"}]}}')
72
+ })
73
+
74
+ test('use proper serialize function', t => {
75
+ t.plan(1)
76
+
77
+ const personSchema = {
78
+ $id: 'person',
79
+ type: 'object',
80
+ properties: {
81
+ name: { type: 'string' },
82
+ children: {
83
+ type: 'array',
84
+ items: { $ref: '#' }
85
+ }
86
+ }
87
+ }
88
+
89
+ const directorySchema = {
90
+ $id: 'directory',
91
+ type: 'object',
92
+ properties: {
93
+ name: { type: 'string' },
94
+ subDirectories: {
95
+ type: 'array',
96
+ items: { $ref: '#' },
97
+ default: []
98
+ }
99
+ }
100
+ }
101
+
102
+ const schema = {
103
+ $id: 'mainSchema',
104
+ type: 'object',
105
+ properties: {
106
+ people: { $ref: 'person' },
107
+ directory: { $ref: 'directory' }
108
+ }
109
+ }
110
+ const stringify = build(schema, {
111
+ schema: {
112
+ [personSchema.$id]: personSchema,
113
+ [directorySchema.$id]: directorySchema
114
+ }
115
+ })
116
+
117
+ const value = stringify({
118
+ people: {
119
+ name: 'Elizabeth',
120
+ children: [{
121
+ name: 'Charles',
122
+ children: [{ name: 'William', children: [{ name: 'George' }, { name: 'Charlotte' }] }, { name: 'Harry' }]
123
+ }]
124
+ },
125
+ directory: {
126
+ name: 'directory 1',
127
+ subDirectories: [
128
+ { name: 'directory 1.1', subDirectories: [] },
129
+ {
130
+ name: 'directory 1.2',
131
+ subDirectories: [{ name: 'directory 1.2.1' }, { name: 'directory 1.2.2' }]
132
+ }
133
+ ]
134
+ }
135
+ })
136
+ t.equal(value, '{"people":{"name":"Elizabeth","children":[{"name":"Charles","children":[{"name":"William","children":[{"name":"George"},{"name":"Charlotte"}]},{"name":"Harry"}]}]},"directory":{"name":"directory 1","subDirectories":[{"name":"directory 1.1","subDirectories":[]},{"name":"directory 1.2","subDirectories":[{"name":"directory 1.2.1","subDirectories":[]},{"name":"directory 1.2.2","subDirectories":[]}]}]}}')
137
+ })