Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-json-stringify

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-json-stringify - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

28

index.js

@@ -143,5 +143,8 @@ 'use strict'

Object.keys(schema.properties).forEach((key, i, a) => {
// Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
code += `
json += '${$asString(key)}:'
`
if (obj.${key} !== undefined) {
json += '${$asString(key)}:'
`

@@ -154,4 +157,17 @@ const result = nested(laterCode, name, '.' + key, schema.properties[key])

if (i < a.length - 1) {
code += 'json += \',\''
code += `
json += \',\'
`
}
if (schema.properties[key].required) {
code += `
} else {
throw new Error('${key} is required!')
`
}
code += `
}
`
})

@@ -207,8 +223,2 @@

var funcName
if (schema.required) {
code += `
if (!obj.hasOwnProperty('${key.slice(1)}')) {
throw new Error('${key} is required!')
}`
}
const type = schema.type

@@ -215,0 +225,0 @@ switch (type) {

{
"name": "fast-json-stringify",
"version": "0.5.0",
"version": "0.6.0",
"description": "Stringify your JSON at max speed",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -70,10 +70,54 @@ # fast-json-stringify&nbsp;&nbsp;[![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify)

*Specific use cases:*
#### Specific use cases
| Instance | Serialized as |
| -----------|---------------------------------------------|
| `Date` | `string` <small>via `toISOString()`</small> |
| `Date` | `string` via `toISOString()` |
| `RegExp` | `string` |
#### Required
You can set specific fields of an object as `required` in your schema, by adding `required: true` inside the key properties.
Example:
```javascript
const schema = {
title: 'Example Schema with required field',
type: 'object',
properties: {
nickname: {
type: 'string'
},
mail: {
type: 'string',
required: true
}
}
}
```
If the object to stringify has not the required field(s), `fast-json-stringify` will throw an error.
#### Missing fields
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.
Example:
```javascript
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string'
},
mail: {
type: 'string',
required: true
}
}
})
const obj = {
mail: 'mail@example.com'
}
console.log(stringify(obj)) // '{"mail":"mail@example.com"}'
```
## Acknowledgements

@@ -80,0 +124,0 @@

@@ -291,5 +291,29 @@ 'use strict'

} catch (e) {
t.is(e.message, '.str is required!')
t.is(e.message, 'str is required!')
t.pass()
}
})
test('missing values', (t) => {
t.plan(3)
const stringify = build({
title: 'object with missing values',
type: 'object',
properties: {
str: {
type: 'string'
},
num: {
type: 'number'
},
val: {
type: 'string'
}
}
})
t.equal('{"val":"value"}', stringify({ val: 'value' }))
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' }))
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' }))
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc