fast-json-stringify 6.0.0 → 6.1.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.
Files changed (68) hide show
  1. package/.github/dependabot.yml +1 -1
  2. package/.github/workflows/ci.yml +7 -2
  3. package/LICENSE +5 -2
  4. package/README.md +50 -45
  5. package/benchmark/bench-cmp-branch.js +1 -1
  6. package/benchmark/bench-cmp-lib.js +29 -1
  7. package/eslint.config.js +9 -0
  8. package/index.js +38 -24
  9. package/lib/serializer.js +5 -7
  10. package/lib/validator.js +1 -1
  11. package/package.json +42 -22
  12. package/test/additionalProperties.test.js +20 -20
  13. package/test/allof.test.js +36 -40
  14. package/test/any.test.js +28 -28
  15. package/test/anyof.test.js +44 -46
  16. package/test/array.test.js +19 -19
  17. package/test/asNumber.test.js +2 -2
  18. package/test/basic.test.js +20 -32
  19. package/test/bigint.test.js +6 -6
  20. package/test/clean-cache.test.js +9 -7
  21. package/test/const.test.js +35 -35
  22. package/test/date.test.js +77 -77
  23. package/test/debug-mode.test.js +27 -27
  24. package/test/defaults.test.js +2 -2
  25. package/test/enum.test.js +3 -3
  26. package/test/fix-604.test.js +18 -17
  27. package/test/if-then-else.test.js +22 -24
  28. package/test/inferType.test.js +4 -4
  29. package/test/infinity.test.js +4 -4
  30. package/test/integer.test.js +21 -21
  31. package/test/invalidSchema.test.js +4 -8
  32. package/test/issue-479.test.js +2 -2
  33. package/test/json-schema-test-suite/README.md +1 -1
  34. package/test/json-schema-test-suite/draft4.test.js +3 -3
  35. package/test/json-schema-test-suite/draft6.test.js +3 -3
  36. package/test/json-schema-test-suite/draft7.test.js +3 -3
  37. package/test/json-schema-test-suite/util.js +6 -9
  38. package/test/missing-values.test.js +8 -8
  39. package/test/multi-type-serializer.test.js +2 -2
  40. package/test/nestedObjects.test.js +3 -3
  41. package/test/nullable.test.js +35 -35
  42. package/test/oneof.test.js +27 -29
  43. package/test/patternProperties.test.js +11 -11
  44. package/test/recursion.test.js +6 -6
  45. package/test/ref.test.js +102 -186
  46. package/test/regex.test.js +4 -5
  47. package/test/required.test.js +42 -77
  48. package/test/requiresAjv.test.js +34 -32
  49. package/test/sanitize.test.js +40 -41
  50. package/test/sanitize2.test.js +11 -11
  51. package/test/sanitize3.test.js +13 -11
  52. package/test/sanitize4.test.js +10 -8
  53. package/test/sanitize5.test.js +11 -11
  54. package/test/sanitize6.test.js +14 -14
  55. package/test/sanitize7.test.js +5 -5
  56. package/test/side-effect.test.js +12 -12
  57. package/test/standalone-mode.test.js +29 -26
  58. package/test/string.test.js +12 -12
  59. package/test/surrogate.test.js +9 -9
  60. package/test/toJSON.test.js +9 -9
  61. package/test/typebox.test.js +2 -2
  62. package/test/typesArray.test.js +34 -34
  63. package/test/unknownFormats.test.js +5 -5
  64. package/test/webpack.test.js +2 -2
  65. package/types/index.d.ts +29 -29
  66. package/types/index.test-d.ts +75 -74
  67. package/.github/workflows/benchmark.yml +0 -85
  68. package/.taprc +0 -7
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Ajv, { Options as AjvOptions } from "ajv"
1
+ import Ajv, { Options as AjvOptions } from 'ajv'
2
2
 
3
3
  type Build = typeof build
4
4
 
@@ -65,33 +65,33 @@ declare namespace build {
65
65
  */
66
66
  $ref: string;
67
67
  }
68
-
68
+
69
69
  export interface AnySchema extends BaseSchema {
70
70
  }
71
71
 
72
72
  export interface StringSchema extends BaseSchema {
73
- type: "string";
73
+ type: 'string';
74
74
  format?: string;
75
75
  }
76
76
 
77
77
  export interface IntegerSchema extends BaseSchema {
78
- type: "integer";
78
+ type: 'integer';
79
79
  }
80
80
 
81
81
  export interface NumberSchema extends BaseSchema {
82
- type: "number";
82
+ type: 'number';
83
83
  }
84
84
 
85
85
  export interface NullSchema extends BaseSchema {
86
- type: "null";
86
+ type: 'null';
87
87
  }
88
88
 
89
89
  export interface BooleanSchema extends BaseSchema {
90
- type: "boolean";
90
+ type: 'boolean';
91
91
  }
92
92
 
93
93
  export interface ArraySchema extends BaseSchema {
94
- type: "array";
94
+ type: 'array';
95
95
  /**
96
96
  * The schema for the items in the array
97
97
  */
@@ -99,7 +99,7 @@ declare namespace build {
99
99
  }
100
100
 
101
101
  export interface TupleSchema extends BaseSchema {
102
- type: "array";
102
+ type: 'array';
103
103
  /**
104
104
  * The schemas for the items in the tuple
105
105
  */
@@ -115,7 +115,7 @@ declare namespace build {
115
115
  }
116
116
 
117
117
  export interface ObjectSchema extends BaseSchema {
118
- type: "object";
118
+ type: 'object';
119
119
  /**
120
120
  * Describe the properties of the object
121
121
  */
@@ -145,7 +145,7 @@ declare namespace build {
145
145
  | BooleanSchema
146
146
  | ArraySchema
147
147
  | TupleSchema
148
- | ObjectSchema;
148
+ | ObjectSchema
149
149
 
150
150
  export interface Options {
151
151
  /**
@@ -158,12 +158,12 @@ declare namespace build {
158
158
  ajv?: AjvOptions
159
159
  /**
160
160
  * Optionally configure how the integer will be rounded
161
- *
161
+ *
162
162
  * @default 'trunc'
163
163
  */
164
164
  rounding?: 'ceil' | 'floor' | 'round' | 'trunc'
165
165
  /**
166
- * @deprecated
166
+ * @deprecated
167
167
  * Enable debug mode. Please use `mode: "debug"` instead
168
168
  */
169
169
  debugMode?: boolean
@@ -190,7 +190,7 @@ declare namespace build {
190
190
  }
191
191
 
192
192
  export const validLargeArrayMechanisms: string[]
193
- export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
193
+ export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
194
194
 
195
195
  export const build: Build
196
196
  export { build as default }
@@ -208,24 +208,24 @@ interface StandaloneOption extends build.Options {
208
208
  mode: 'standalone'
209
209
  }
210
210
 
211
- type StringCoercible = string | Date | RegExp;
212
- type IntegerCoercible = number | BigInt;
211
+ type StringCoercible = string | Date | RegExp
212
+ type IntegerCoercible = number | BigInt
213
213
 
214
214
  /**
215
215
  * Build a stringify function using a schema of the documents that should be stringified
216
216
  * @param schema The schema used to stringify values
217
217
  * @param options The options to use (optional)
218
218
  */
219
- declare function build(schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv };
220
- declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv };
221
- declare function build(schema: build.AnySchema, options: StandaloneOption): string;
222
- declare function build(schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any;
223
- declare function build(schema: build.StringSchema, options?: build.Options): <TDoc extends StringCoercible = StringCoercible>(doc: TDoc) => string;
224
- declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc extends IntegerCoercible = IntegerCoercible>(doc: TDoc) => string;
225
- declare function build(schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) => "null";
226
- declare function build(schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string;
227
- declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string;
228
- declare function build(schema: build.ObjectSchema, options?: build.Options): <TDoc extends object = object>(doc: TDoc) => string;
229
- declare function build(schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string;
230
-
231
- export = build;
219
+ declare function build (schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv }
220
+ declare function build (schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv }
221
+ declare function build (schema: build.AnySchema, options: StandaloneOption): string
222
+ declare function build (schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any
223
+ declare function build (schema: build.StringSchema, options?: build.Options): <TDoc extends StringCoercible = StringCoercible>(doc: TDoc) => string
224
+ declare function build (schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc extends IntegerCoercible = IntegerCoercible>(doc: TDoc) => string
225
+ declare function build (schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) => 'null'
226
+ declare function build (schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string
227
+ declare function build (schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string
228
+ declare function build (schema: build.ObjectSchema, options?: build.Options): <TDoc extends object = object>(doc: TDoc) => string
229
+ declare function build (schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string
230
+
231
+ export = build
@@ -1,32 +1,33 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- Test using this disabled, see https://github.com/fastify/fast-json-stringify/pull/683
1
2
  import Ajv from 'ajv'
2
3
  import build, { restore, Schema, validLargeArrayMechanisms } from '..'
3
4
  import { expectError, expectType } from 'tsd'
4
5
 
5
6
  // Number schemas
6
7
  build({
7
- type: 'number'
8
+ type: 'number'
8
9
  })(25)
9
10
  build({
10
- type: 'integer'
11
+ type: 'integer'
11
12
  })(-5)
12
13
  build({
13
- type: 'integer'
14
+ type: 'integer'
14
15
  })(5n)
15
16
 
16
17
  build({
17
- type: 'number'
18
+ type: 'number'
18
19
  }, { rounding: 'ceil' })
19
20
  build({
20
- type: 'number'
21
+ type: 'number'
21
22
  }, { rounding: 'floor' })
22
23
  build({
23
- type: 'number'
24
+ type: 'number'
24
25
  }, { rounding: 'round' })
25
26
  build({
26
- type: 'number'
27
+ type: 'number'
27
28
  }, { rounding: 'trunc' })
28
29
  expectError(build({
29
- type: 'number'
30
+ type: 'number'
30
31
  }, { rounding: 'invalid' }))
31
32
 
32
33
  // String schema
@@ -36,55 +37,55 @@ build({
36
37
 
37
38
  // Boolean schema
38
39
  build({
39
- type: 'boolean'
40
+ type: 'boolean'
40
41
  })(true)
41
42
 
42
43
  // Null schema
43
44
  build({
44
- type: 'null'
45
+ type: 'null'
45
46
  })(null)
46
47
 
47
48
  // Array schemas
48
49
  build({
49
- type: 'array',
50
- items: { type: 'number' }
50
+ type: 'array',
51
+ items: { type: 'number' }
51
52
  })([25])
52
53
  build({
53
- type: 'array',
54
- items: [{ type: 'string'}, {type: 'integer'}]
54
+ type: 'array',
55
+ items: [{ type: 'string' }, { type: 'integer' }]
55
56
  })(['hello', 42])
56
57
 
57
58
  // Object schemas
58
59
  build({
59
- type: 'object'
60
+ type: 'object'
60
61
  })({})
61
62
  build({
62
- type: 'object',
63
- properties: {
64
- foo: { type: 'string' },
65
- bar: { type: 'integer' }
66
- },
67
- required: ['foo'],
68
- patternProperties: {
69
- 'baz*': { type: 'null' }
70
- },
71
- additionalProperties: {
72
- type: 'boolean'
73
- }
63
+ type: 'object',
64
+ properties: {
65
+ foo: { type: 'string' },
66
+ bar: { type: 'integer' }
67
+ },
68
+ required: ['foo'],
69
+ patternProperties: {
70
+ 'baz*': { type: 'null' }
71
+ },
72
+ additionalProperties: {
73
+ type: 'boolean'
74
+ }
74
75
  })({ foo: 'bar' })
75
76
  build({
76
- type: 'object',
77
- properties: {
78
- foo: { type: 'string' },
79
- bar: { type: 'integer' }
80
- },
81
- required: ['foo'],
82
- patternProperties: {
83
- 'baz*': { type: 'null' }
84
- },
85
- additionalProperties: {
86
- type: 'boolean'
87
- }
77
+ type: 'object',
78
+ properties: {
79
+ foo: { type: 'string' },
80
+ bar: { type: 'integer' }
81
+ },
82
+ required: ['foo'],
83
+ patternProperties: {
84
+ 'baz*': { type: 'null' }
85
+ },
86
+ additionalProperties: {
87
+ type: 'boolean'
88
+ }
88
89
  }, { rounding: 'floor' })({ foo: 'bar' })
89
90
 
90
91
  // Reference schemas
@@ -113,7 +114,7 @@ build({
113
114
  }
114
115
  },
115
116
  patternProperties: {
116
- 'num': {
117
+ num: {
117
118
  $ref: '#/definitions/num'
118
119
  }
119
120
  },
@@ -159,7 +160,7 @@ build({
159
160
  })(new Date())
160
161
 
161
162
  /*
162
- This overload doesn't work yet -
163
+ This overload doesn't work yet -
163
164
  TypeScript chooses the generic for the schema
164
165
  before it chooses the overload for the options
165
166
  parameter.
@@ -207,52 +208,52 @@ interface InferenceSchema {
207
208
  }
208
209
 
209
210
  const stringify3 = build({
210
- type: "object",
211
- properties: { a: { type: "string" } },
212
- });
213
- stringify3<InferenceSchema>({ id: "123" });
214
- stringify3<InferenceSchema>({ a: 123, id: "123" });
215
- expectError(stringify3<InferenceSchema>({ anotherOne: "bar" }));
216
- expectError(stringify3<Schema>({ a: "bar" }));
211
+ type: 'object',
212
+ properties: { a: { type: 'string' } },
213
+ })
214
+ stringify3<InferenceSchema>({ id: '123' })
215
+ stringify3<InferenceSchema>({ a: 123, id: '123' })
216
+ expectError(stringify3<InferenceSchema>({ anotherOne: 'bar' }))
217
+ expectError(stringify3<Schema>({ a: 'bar' }))
217
218
 
218
219
  // Without inference
219
220
  const stringify4 = build({
220
- type: "object",
221
- properties: { a: { type: "string" } },
222
- });
223
- stringify4({ id: "123" });
224
- stringify4({ a: 123, id: "123" });
225
- stringify4({ anotherOne: "bar" });
226
- stringify4({ a: "bar" });
221
+ type: 'object',
222
+ properties: { a: { type: 'string' } },
223
+ })
224
+ stringify4({ id: '123' })
225
+ stringify4({ a: 123, id: '123' })
226
+ stringify4({ anotherOne: 'bar' })
227
+ stringify4({ a: 'bar' })
227
228
 
228
229
  // Without inference - string type
229
230
  const stringify5 = build({
230
- type: "string",
231
- });
232
- stringify5("foo");
233
- expectError(stringify5({ id: "123" }));
231
+ type: 'string',
232
+ })
233
+ stringify5('foo')
234
+ expectError(stringify5({ id: '123' }))
234
235
 
235
236
  // Without inference - null type
236
237
  const stringify6 = build({
237
- type: "null",
238
- });
239
- stringify6(null);
240
- expectError(stringify6("a string"));
238
+ type: 'null',
239
+ })
240
+ stringify6(null)
241
+ expectError(stringify6('a string'))
241
242
 
242
243
  // Without inference - boolean type
243
244
  const stringify7 = build({
244
- type: "boolean",
245
- });
246
- stringify7(true);
247
- expectError(stringify7("a string"));
245
+ type: 'boolean',
246
+ })
247
+ stringify7(true)
248
+ expectError(stringify7('a string'))
248
249
 
249
250
  // largeArrayMechanism
250
251
 
251
- build({}, { largeArrayMechanism: 'json-stringify'} )
252
- build({}, { largeArrayMechanism: 'default'} )
253
- expectError(build({} as Schema, { largeArrayMechanism: 'invalid'} ))
252
+ build({}, { largeArrayMechanism: 'json-stringify' })
253
+ build({}, { largeArrayMechanism: 'default' })
254
+ expectError(build({} as Schema, { largeArrayMechanism: 'invalid' }))
254
255
 
255
- build({}, { largeArraySize: 2000 } )
256
- build({}, { largeArraySize: '2e4' } )
257
- build({}, { largeArraySize: 2n } )
258
- expectError(build({} as Schema, { largeArraySize: ['asdf']} ))
256
+ build({}, { largeArraySize: 2000 })
257
+ build({}, { largeArraySize: '2e4' })
258
+ build({}, { largeArraySize: 2n })
259
+ expectError(build({} as Schema, { largeArraySize: ['asdf'] }))
@@ -1,85 +0,0 @@
1
- name: Benchmark PR
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
- permissions:
12
- contents: read
13
- outputs:
14
- PR-BENCH: ${{ steps.benchmark-pr.outputs.BENCH_RESULT }}
15
- MASTER-BENCH: ${{ steps.benchmark-master.outputs.BENCH_RESULT }}
16
- steps:
17
- - uses: actions/checkout@v4
18
- with:
19
- persist-credentials: false
20
- ref: ${{github.event.pull_request.head.sha}}
21
- repository: ${{github.event.pull_request.head.repo.full_name}}
22
-
23
- - uses: actions/setup-node@v4
24
- with:
25
- node-version: 18
26
-
27
- - name: Install
28
- run: |
29
- npm install --ignore-scripts
30
-
31
- - name: Run benchmark
32
- id: benchmark-pr
33
- run: |
34
- npm run --silent bench > ./bench-result
35
- content=$(cat ./bench-result)
36
- content="${content//'%'/'%25'}"
37
- content="${content//$'\n'/'%0A'}"
38
- content="${content//$'\r'/'%0D'}"
39
- echo "::set-output name=BENCH_RESULT::$content"
40
-
41
- # master benchmark
42
- - uses: actions/checkout@v4
43
- with:
44
- ref: 'master'
45
-
46
- - name: Install
47
- run: |
48
- npm install --ignore-scripts
49
-
50
- - name: Run benchmark
51
- id: benchmark-master
52
- run: |
53
- npm run --silent bench > ./bench-result
54
- content=$(cat ./bench-result)
55
- content="${content//'%'/'%25'}"
56
- content="${content//$'\n'/'%0A'}"
57
- content="${content//$'\r'/'%0D'}"
58
- echo "::set-output name=BENCH_RESULT::$content"
59
-
60
- output-benchmark:
61
- if: "always()"
62
- needs: [benchmark]
63
- runs-on: ubuntu-latest
64
- permissions:
65
- pull-requests: write
66
- steps:
67
- - name: Comment PR
68
- uses: thollander/actions-comment-pull-request@v2
69
- with:
70
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71
- message: |
72
- **PR**:
73
- ```
74
- ${{ needs.benchmark.outputs.PR-BENCH }}
75
- ```
76
- **MASTER**:
77
- ```
78
- ${{ needs.benchmark.outputs.MASTER-BENCH }}
79
- ```
80
-
81
- - uses: actions-ecosystem/action-remove-labels@v1
82
- with:
83
- labels: |
84
- benchmark
85
- github_token: ${{ secrets.GITHUB_TOKEN }}
package/.taprc DELETED
@@ -1,7 +0,0 @@
1
- disable-coverage: true
2
-
3
- files:
4
- - test/**/*.test.js
5
-
6
- plugin:
7
- - "!@tapjs/typescript"