fast-json-stringify
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -23,5 +23,32 @@ 'use strict' | ||
type: 'string' | ||
}, | ||
obj: { | ||
type: 'object', | ||
properties: { | ||
bool: { | ||
type: 'boolean' | ||
} | ||
} | ||
}, | ||
arr: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
str: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
required: ['now'] | ||
required: ['now'], | ||
patternProperties: { | ||
'.*foo$': { | ||
type: 'string' | ||
}, | ||
'test': { | ||
type: 'number' | ||
} | ||
} | ||
}) | ||
@@ -34,3 +61,9 @@ | ||
now: new Date(), | ||
reg: /"([^"]|\\")*"/ | ||
reg: /"([^"]|\\")*"/, | ||
foo: 'hello', | ||
numfoo: 42, | ||
test: 42, | ||
strtest: '23', | ||
arr: [{ str: 'stark' }, { str: 'lannister' }], | ||
obj: { bool: true } | ||
})) |
67
index.js
@@ -7,3 +7,9 @@ 'use strict' | ||
'use strict' | ||
` | ||
// used to support patternProperties and additionalProperties | ||
// they need to check if a field belongs to the properties in the schema | ||
code += ` | ||
const properties = ${JSON.stringify(schema.properties)} | ||
` | ||
code += ` | ||
${$asString.toString()} | ||
@@ -62,3 +68,3 @@ ${$asStringSmall.toString()} | ||
} else { | ||
return '' + i | ||
return '' + num | ||
} | ||
@@ -136,2 +142,55 @@ } | ||
function addPatternProperties (pp) { | ||
let code = ` | ||
var keys = Object.keys(obj) | ||
for (var i = 0; i < keys.length; i++) { | ||
if (properties[keys[i]]) continue | ||
` | ||
Object.keys(pp).forEach((regex, index) => { | ||
var type = pp[regex].type | ||
code += ` | ||
if (/${regex}/.test(keys[i])) { | ||
` | ||
if (type === 'object') { | ||
code += buildObject(pp[regex], '', 'buildObjectPP' + index) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'array') { | ||
code += buildArray(pp[regex], '', 'buildArrayPP' + index) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildArrayPP${index}(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}') | ||
` | ||
} | ||
code += ` | ||
} | ||
` | ||
}) | ||
code += ` | ||
} | ||
if (Object.keys(properties).length === 0) json = json.substring(0, json.length - 1) | ||
` | ||
return code | ||
} | ||
function buildObject (schema, code, name) { | ||
@@ -142,2 +201,5 @@ code += ` | ||
` | ||
if (schema.patternProperties) { | ||
code += addPatternProperties(schema.patternProperties) | ||
} | ||
@@ -184,3 +246,2 @@ var laterCode = '' | ||
code += laterCode | ||
return code | ||
@@ -187,0 +248,0 @@ } |
{ | ||
"name": "fast-json-stringify", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,2 +61,3 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify) | ||
* `'string'` | ||
* `'integer'` | ||
@@ -110,6 +111,6 @@ * `'number'` | ||
mail: { | ||
type: 'string', | ||
required: true | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
required: ['mail'] | ||
}) | ||
@@ -124,2 +125,36 @@ | ||
#### Pattern properties | ||
`fast-json-stringify` supports pattern properties as defined inside JSON schema. | ||
*patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: `{ type: 'type' }`. | ||
*patternProperties* will work only for the properties that are not explicitly listed in the properties object. | ||
Example: | ||
```javascript | ||
const stringify = fastJson({ | ||
title: 'Example Schema', | ||
type: 'object', | ||
properties: { | ||
nickname: { | ||
type: 'string' | ||
} | ||
}, | ||
patternProperties: { | ||
'num': { | ||
type: 'number' | ||
}, | ||
'.*foo$': { | ||
type: 'string' | ||
} | ||
} | ||
}) | ||
const obj = { | ||
nickname: 'nick', | ||
matchfoo: 42, | ||
otherfoo: 'str' | ||
matchnum: 3 | ||
} | ||
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}' | ||
``` | ||
## Acknowledgements | ||
@@ -126,0 +161,0 @@ |
157
test.js
@@ -319,1 +319,158 @@ 'use strict' | ||
}) | ||
test('patternProperties', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'patternProperties', | ||
type: 'object', | ||
properties: { | ||
str: { | ||
type: 'string' | ||
} | ||
}, | ||
patternProperties: { | ||
'foo': { | ||
type: 'string' | ||
} | ||
} | ||
}) | ||
let obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: {a: true}, notMe: false } | ||
t.equal('{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}', stringify(obj)) | ||
}) | ||
test('patternProperties should not change properties', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'patternProperties should not change properties', | ||
type: 'object', | ||
properties: { | ||
foo: { | ||
type: 'string' | ||
} | ||
}, | ||
patternProperties: { | ||
foo: { | ||
type: 'number' | ||
} | ||
} | ||
}) | ||
const obj = { foo: '42', ofoo: 42 } | ||
t.equal('{"ofoo":42,"foo":"42"}', stringify(obj)) | ||
}) | ||
test('patternProperties - string coerce', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check string coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'string' | ||
} | ||
} | ||
}) | ||
const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } } | ||
t.equal('{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}', stringify(obj)) | ||
}) | ||
test('patternProperties - number coerce', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check number coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'number' | ||
} | ||
} | ||
}) | ||
const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } } | ||
t.equal('{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}', stringify(obj)) | ||
}) | ||
test('patternProperties - boolean coerce', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check boolean coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'boolean' | ||
} | ||
} | ||
}) | ||
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } } | ||
t.equal('{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}', stringify(obj)) | ||
}) | ||
test('patternProperties - object coerce', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check object coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'object', | ||
properties: { | ||
answer: { | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
const obj = { objfoo: { answer: 42 } } | ||
t.equal('{"objfoo":{"answer":42}}', stringify(obj)) | ||
}) | ||
test('patternProperties - array coerce', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check array coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
}) | ||
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } } | ||
t.equal('{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}', stringify(obj)) | ||
}) | ||
test('patternProperties - throw on unknown type', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'check array coerce', | ||
type: 'object', | ||
properties: {}, | ||
patternProperties: { | ||
foo: { | ||
type: 'strangetype' | ||
} | ||
} | ||
}) | ||
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } } | ||
try { | ||
stringify(obj) | ||
t.fail() | ||
} catch (e) { | ||
t.pass() | ||
} | ||
}) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
25173
888
164
2