Skip to content

Commit 7e80246

Browse files
committed
Updated docs and example
1 parent cbc14de commit 7e80246

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

README.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Build a `stringify()` function based on
5858

5959
Supported types:
6060

61+
* `'string'`
6162
* `'integer'`
6263
* `'number'`
6364
* `'array'`
@@ -106,10 +107,10 @@ const stringify = fastJson({
106107
type: 'string'
107108
},
108109
mail: {
109-
type: 'string',
110-
required: true
110+
type: 'string'
111111
}
112-
}
112+
},
113+
required: ['mail']
113114
})
114115

115116
const obj = {
@@ -119,6 +120,74 @@ const obj = {
119120
console.log(stringify(obj)) // '{"mail":"[email protected]"}'
120121
```
121122

123+
#### Pattern properties
124+
`fast-json-stringify` supports pattern properties as defined inside JSON schema.
125+
*patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: `{ type: 'type' }`.
126+
*patternProperties* will work only for the properties that are not explicitly listed in the properties object.
127+
Example:
128+
```javascript
129+
const stringify = fastJson({
130+
title: 'Example Schema',
131+
type: 'object',
132+
properties: {
133+
nickname: {
134+
type: 'string'
135+
}
136+
},
137+
patternProperties: {
138+
'num': {
139+
type: 'number'
140+
},
141+
'.*foo$': {
142+
type: 'string'
143+
}
144+
}
145+
})
146+
147+
const obj = {
148+
nickname: 'nick',
149+
matchfoo: 42,
150+
otherfoo: 'str'
151+
matchnum: 3
152+
}
153+
154+
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}'
155+
```
156+
`Object` and `Array` will be stringified as `{}` and `[]`.
157+
Since `fast-json-stringify` is faster than `JSON.stringify` because works on a **fixed schema**, we strongly advice you to not pass complex objects or array as object values, but decompose them in their individual properties and stringify them.
158+
```javascript
159+
const stringifyMe = {
160+
obj: {
161+
filed1: 'str',
162+
field2: 42
163+
}
164+
}
165+
// Not correct
166+
const stringify = fastJson({
167+
title: 'Example Schema',
168+
type: 'object',
169+
properties: {
170+
obj: {
171+
type: 'object'
172+
}
173+
}
174+
})
175+
176+
// Correct
177+
const stringify = fastJson({
178+
title: 'Example Schema',
179+
type: 'object',
180+
properties: {
181+
field1: {
182+
type: 'string'
183+
},
184+
field2: {
185+
type: 'number'
186+
}
187+
}
188+
})
189+
```
190+
122191
## Acknowledgements
123192

124193
This project was kindly sponsored by [nearForm](http://nearform.com).

example.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ const stringify = fastJson({
2222
type: 'string'
2323
}
2424
},
25-
required: ['now']
25+
required: ['now'],
26+
patternProperties: {
27+
'.*foo$': {
28+
type: 'string'
29+
},
30+
'test': {
31+
type: 'number'
32+
}
33+
}
2634
})
2735

2836
console.log(stringify({
2937
firstName: 'Matteo',
3038
lastName: 'Collina',
3139
age: 32,
3240
now: new Date(),
33-
reg: /"([^"]|\\")*"/
41+
reg: /"([^"]|\\")*"/,
42+
foo: 'hello',
43+
numfoo: 42,
44+
test: 42,
45+
strtest: '23'
3446
}))

0 commit comments

Comments
 (0)