fast-json-stringify
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -52,2 +52,5 @@ 'use strict' | ||
} | ||
}, | ||
additionalProperties: { | ||
type: 'string' | ||
} | ||
@@ -67,3 +70,6 @@ }) | ||
arr: [{ str: 'stark' }, { str: 'lannister' }], | ||
obj: { bool: true } | ||
obj: { bool: true }, | ||
notmatch: 'valar morghulis', | ||
notmatchobj: { a: true }, | ||
notmatchnum: 42 | ||
})) |
76
index.js
'use strict' | ||
const fastSafeStringify = require('fast-safe-stringify') | ||
function build (schema) { | ||
@@ -54,3 +56,5 @@ /* eslint no-new-func: "off" */ | ||
` | ||
if (schema.additionalProperties === true) { | ||
return (new Function('fastSafeStringify', code))(fastSafeStringify) | ||
} | ||
return (new Function(code))() | ||
@@ -141,3 +145,3 @@ } | ||
function addPatternProperties (pp) { | ||
function addPatternProperties (pp, ap) { | ||
let code = ` | ||
@@ -181,12 +185,17 @@ var keys = Object.keys(obj) | ||
code += ` | ||
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}') | ||
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}') | ||
` | ||
} | ||
code += ` | ||
continue | ||
} | ||
` | ||
}) | ||
if (ap) { | ||
code += additionalProperty(ap) | ||
} | ||
code += ` | ||
} | ||
if (Object.keys(properties).length === 0) json = json.substring(0, json.length - 1) | ||
` | ||
@@ -196,2 +205,54 @@ return code | ||
function additionalProperty (ap) { | ||
let code = '' | ||
if (ap === true) { | ||
return ` | ||
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ',' | ||
` | ||
} | ||
let type = ap.type | ||
if (type === 'object') { | ||
code += buildObject(ap, '', 'buildObjectAP') | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'array') { | ||
code += buildArray(ap, '', 'buildArrayAP') | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'null') { | ||
code += ` | ||
json += $asString(keys[i]) +':null,' | ||
` | ||
} else if (type === 'string') { | ||
code += ` | ||
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'number' || type === 'integer') { | ||
code += ` | ||
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'boolean') { | ||
code += ` | ||
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) + ',' | ||
` | ||
} else { | ||
code += ` | ||
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}') | ||
` | ||
} | ||
return code | ||
} | ||
function addAdditionalProperties (ap) { | ||
return ` | ||
var keys = Object.keys(obj) | ||
for (var i = 0; i < keys.length; i++) { | ||
if (properties[keys[i]]) continue | ||
${additionalProperty(ap)} | ||
} | ||
` | ||
} | ||
function buildObject (schema, code, name) { | ||
@@ -202,4 +263,7 @@ code += ` | ||
` | ||
if (schema.patternProperties) { | ||
code += addPatternProperties(schema.patternProperties) | ||
code += addPatternProperties(schema.patternProperties, schema.additionalProperties) | ||
} else if (schema.additionalProperties && !schema.patternProperties) { | ||
code += addAdditionalProperties(schema.additionalProperties) | ||
} | ||
@@ -240,3 +304,5 @@ | ||
// Removes the comma if is the last element of the string (in case there are not properties) | ||
code += ` | ||
if (json[json.length - 1] === ',') json = json.substring(0, json.length - 1) | ||
json += '}' | ||
@@ -243,0 +309,0 @@ return json |
{ | ||
"name": "fast-json-stringify", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -32,3 +32,6 @@ "main": "index.js", | ||
"tap": "^7.1.2" | ||
}, | ||
"dependencies": { | ||
"fast-safe-stringify": "^1.1.0" | ||
} | ||
} |
@@ -20,2 +20,16 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify) | ||
#### Table of contents: | ||
- <a href="#example">`Example`</a> | ||
- <a href="#api">`API`</a> | ||
- <a href="#fastJsonStringify">`fastJsonStringify`</a> | ||
- <a href="#specific">`Specific use cases`</a> | ||
- <a href="#required">`Required`</a> | ||
- <a href="#missingFields">`Missing fields`</a> | ||
- <a href="#patternProperties">`Pattern Properties`</a> | ||
- <a href="#additionalProperties">`Additional Properties`</a> | ||
- <a href="#acknowledgements">`Acknowledgements`</a> | ||
- <a href="#license">`License`</a> | ||
<a name="example"></a> | ||
## Example | ||
@@ -52,5 +66,5 @@ | ||
``` | ||
<a name="api"></a> | ||
## API | ||
<a name="fastJsonStringify"></a> | ||
### fastJsonStringify(schema) | ||
@@ -73,2 +87,3 @@ | ||
<a name="specific"></a> | ||
#### Specific use cases | ||
@@ -81,2 +96,3 @@ | ||
<a name="required"></a> | ||
#### Required | ||
@@ -102,2 +118,3 @@ You can set specific fields of an object as required in your schema, by adding the field name inside the `required` array in your schema. | ||
<a name="missingFields"></a> | ||
#### Missing fields | ||
@@ -117,4 +134,3 @@ If a field *is present* in the schema (and is not required) but it *is not present* in the object to stringify, `fast-json-stringify` will not write it in the final string. | ||
} | ||
}, | ||
required: ['mail'] | ||
} | ||
}) | ||
@@ -129,2 +145,3 @@ | ||
<a name="patternProperties"></a> | ||
#### Pattern properties | ||
@@ -161,5 +178,49 @@ `fast-json-stringify` supports pattern properties as defined inside JSON schema. | ||
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}' | ||
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nickname":"nick"}' | ||
``` | ||
<a name="additionalProperties"></a> | ||
#### Additional properties | ||
`fast-json-stringify` supports additional properties as defined inside JSON schema. | ||
*additionalProperties* must be an object or a boolean, declared in this way: `{ type: 'type' }`. | ||
*additionalProperties* will work only for the properties that are not explicitly listed in the *properties* and *patternProperties* objects. | ||
If *additionalProperties* is not present or is setted to false, every property that is not explicitly listed in the *properties* and *patternProperties* objects, will be ignored, as said in <a href="#missingFields">Missing fields</a>. | ||
If *additionalProperties* is setted to *true*, it will be used `fast-safe-stringify` to stringify the additional properties. If you want to achieve maximum performances we strongly encourage you to use a fixed schema where possible. | ||
Example: | ||
```javascript | ||
const stringify = fastJson({ | ||
title: 'Example Schema', | ||
type: 'object', | ||
properties: { | ||
nickname: { | ||
type: 'string' | ||
} | ||
}, | ||
patternProperties: { | ||
'num': { | ||
type: 'number' | ||
}, | ||
'.*foo$': { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: { | ||
type: 'string' | ||
} | ||
}) | ||
const obj = { | ||
nickname: 'nick', | ||
matchfoo: 42, | ||
otherfoo: 'str' | ||
matchnum: 3, | ||
nomatchstr: 'valar morghulis', | ||
nomatchint: 313 | ||
} | ||
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}' | ||
``` | ||
<a name="acknowledgements"></a> | ||
## Acknowledgements | ||
@@ -169,4 +230,5 @@ | ||
<a name="license"></a> | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34307
16
1133
226
1
3
+ Addedfast-safe-stringify@^1.1.0
+ Addedfast-safe-stringify@1.2.3(transitive)