fast-json-stringify 4.0.0 → 5.0.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.
- package/README.md +31 -5
- package/ajv.js +37 -0
- package/index.d.ts +25 -1
- package/index.js +260 -586
- package/package.json +4 -8
- package/serializer.js +163 -0
- package/standalone.js +42 -0
- package/test/allof.test.js +9 -7
- package/test/array.test.js +29 -0
- package/test/fixtures/.keep +0 -0
- package/test/ref.test.js +610 -37
- package/test/standalone-mode.test.js +121 -0
- package/test/types/test.ts +9 -1
package/README.md
CHANGED
|
@@ -62,6 +62,8 @@ compile-json-stringify date format x 1,086,187 ops/sec ±0.16% (99 runs sampled)
|
|
|
62
62
|
- <a href="#nullable">`Nullable`</a>
|
|
63
63
|
- <a href="#largearrays">`Large Arrays`</a>
|
|
64
64
|
- <a href="#security">`Security Notice`</a>
|
|
65
|
+
- <a href="#debug">`Debug Mode`</a>
|
|
66
|
+
- <a href="#standalone">`Standalone Mode`</a>
|
|
65
67
|
- <a href="#acknowledgements">`Acknowledgements`</a>
|
|
66
68
|
- <a href="#license">`License`</a>
|
|
67
69
|
|
|
@@ -663,13 +665,37 @@ const debugCompiled = fastJson({
|
|
|
663
665
|
type: 'string'
|
|
664
666
|
}
|
|
665
667
|
}
|
|
666
|
-
}, {
|
|
668
|
+
}, { mode: 'debug' })
|
|
667
669
|
|
|
668
|
-
console.log(debugCompiled) // it is
|
|
669
|
-
|
|
670
|
+
console.log(debugCompiled) // it is a object contain code, ajv instance
|
|
671
|
+
const rawString = debugCompiled.code // it is the generated code
|
|
672
|
+
console.log(rawString)
|
|
670
673
|
|
|
671
|
-
const
|
|
672
|
-
|
|
674
|
+
const stringify = fastJson.restore(debugCompiled) // use the generated string to get back the `stringify` function
|
|
675
|
+
console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}'
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
<a name="standalone"></a>
|
|
679
|
+
### Standalone Mode
|
|
680
|
+
|
|
681
|
+
The standalone mode is used to compile the code that can be directly run by `node`
|
|
682
|
+
itself. You need to install `ajv`, `fast-uri` and `ajv-formats` for
|
|
683
|
+
the standalone code to work.
|
|
684
|
+
|
|
685
|
+
```js
|
|
686
|
+
const fs = require('fs')
|
|
687
|
+
const code = fastJson({
|
|
688
|
+
title: 'default string',
|
|
689
|
+
type: 'object',
|
|
690
|
+
properties: {
|
|
691
|
+
firstName: {
|
|
692
|
+
type: 'string'
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}, { mode: 'standalone' })
|
|
696
|
+
|
|
697
|
+
fs.writeFileSync('stringify.js', code)
|
|
698
|
+
const stringify = require('stringify.js')
|
|
673
699
|
console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}'
|
|
674
700
|
```
|
|
675
701
|
|
package/ajv.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Ajv = require('ajv')
|
|
4
|
+
const fastUri = require('fast-uri')
|
|
5
|
+
const ajvFormats = require('ajv-formats')
|
|
6
|
+
|
|
7
|
+
module.exports = buildAjv
|
|
8
|
+
|
|
9
|
+
function buildAjv (options) {
|
|
10
|
+
const ajvInstance = new Ajv({ ...options, strictSchema: false, validateSchema: false, uriResolver: fastUri })
|
|
11
|
+
ajvFormats(ajvInstance)
|
|
12
|
+
|
|
13
|
+
const validateDateTimeFormat = ajvFormats.get('date-time').validate
|
|
14
|
+
const validateDateFormat = ajvFormats.get('date').validate
|
|
15
|
+
const validateTimeFormat = ajvFormats.get('time').validate
|
|
16
|
+
|
|
17
|
+
ajvInstance.addKeyword({
|
|
18
|
+
keyword: 'fjs_date_type',
|
|
19
|
+
validate: (schema, date) => {
|
|
20
|
+
if (date instanceof Date) {
|
|
21
|
+
return true
|
|
22
|
+
}
|
|
23
|
+
if (schema === 'date-time') {
|
|
24
|
+
return validateDateTimeFormat(date)
|
|
25
|
+
}
|
|
26
|
+
if (schema === 'date') {
|
|
27
|
+
return validateDateFormat(date)
|
|
28
|
+
}
|
|
29
|
+
if (schema === 'time') {
|
|
30
|
+
return validateTimeFormat(date)
|
|
31
|
+
}
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
return ajvInstance
|
|
37
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Options as AjvOptions } from "ajv"
|
|
1
|
+
import Ajv, { Options as AjvOptions } from "ajv"
|
|
2
2
|
declare namespace build {
|
|
3
3
|
interface BaseSchema {
|
|
4
4
|
/**
|
|
@@ -157,14 +157,38 @@ declare namespace build {
|
|
|
157
157
|
* Optionally configure how the integer will be rounded
|
|
158
158
|
*/
|
|
159
159
|
rounding?: 'ceil' | 'floor' | 'round'
|
|
160
|
+
/**
|
|
161
|
+
* @deprecated
|
|
162
|
+
* Enable debug mode. Please use `mode: "debug"` instead
|
|
163
|
+
*/
|
|
164
|
+
debugMode?: boolean
|
|
165
|
+
/**
|
|
166
|
+
* Running mode of fast-json-stringify
|
|
167
|
+
*/
|
|
168
|
+
mode?: 'debug' | 'standalone'
|
|
160
169
|
}
|
|
161
170
|
}
|
|
162
171
|
|
|
172
|
+
interface DebugOption extends build.Options {
|
|
173
|
+
mode: 'debug'
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface DeprecateDebugOption extends build.Options {
|
|
177
|
+
debugMode: true
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
interface StandaloneOption extends build.Options {
|
|
181
|
+
mode: 'standalone'
|
|
182
|
+
}
|
|
183
|
+
|
|
163
184
|
/**
|
|
164
185
|
* Build a stringify function using a schema of the documents that should be stringified
|
|
165
186
|
* @param schema The schema used to stringify values
|
|
166
187
|
* @param options The options to use (optional)
|
|
167
188
|
*/
|
|
189
|
+
declare function build(schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv };
|
|
190
|
+
declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv };
|
|
191
|
+
declare function build(schema: build.AnySchema, options: StandaloneOption): string;
|
|
168
192
|
declare function build(schema: build.AnySchema, options?: build.Options): (doc: any) => any;
|
|
169
193
|
declare function build(schema: build.StringSchema, options?: build.Options): (doc: string) => string;
|
|
170
194
|
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): (doc: number) => string;
|