Skip to content

Commit 0d4d02d

Browse files
committed
Docs: table of contents
1 parent 929320b commit 0d4d02d

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ JSON.stringify obj x 1,763,980 ops/sec ±1.30% (88 runs sampled)
1717
fast-json-stringify obj x 5,085,148 ops/sec ±1.56% (89 runs sampled)
1818
```
1919

20+
#### Table of contents:
21+
- <a href="#example">`Example`</a>
22+
- <a href="#api">`API`</a>
23+
- <a href="#fastJsonStringify">`fastJsonStringify`</a>
24+
- <a href="#specific">`Specific use cases`</a>
25+
- <a href="#required">`Required`</a>
26+
- <a href="#missingFields">`Missing fields`</a>
27+
- <a href="#patternProperties">`Pattern Properties`</a>
28+
- <a href="#additionalProperties">`Additional Properties`</a>
29+
- <a href="#acknowledgements">`Acknowledgements`</a>
30+
- <a href="#license">`License`</a>
31+
32+
33+
<a name="example"></a>
2034
## Example
2135

2236
```js
@@ -48,9 +62,9 @@ console.log(stringify({
4862
reg: /"([^"]|\\")*"/
4963
}))
5064
```
51-
65+
<a name="api"></a>
5266
## API
53-
67+
<a name="fastJsonStringify"></a>
5468
### fastJsonStringify(schema)
5569

5670
Build a `stringify()` function based on
@@ -68,13 +82,15 @@ Supported types:
6882

6983
And nested ones, too.
7084

85+
<a name="specific"></a>
7186
#### Specific use cases
7287

7388
| Instance | Serialized as |
7489
| -----------|------------------------------|
7590
| `Date` | `string` via `toISOString()` |
7691
| `RegExp` | `string` |
7792

93+
<a name="required"></a>
7894
#### Required
7995
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.
8096
Example:
@@ -95,6 +111,7 @@ const schema = {
95111
```
96112
If the object to stringify has not the required field(s), `fast-json-stringify` will throw an error.
97113

114+
<a name="missingFields"></a>
98115
#### Missing fields
99116
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.
100117
Example:
@@ -109,8 +126,7 @@ const stringify = fastJson({
109126
mail: {
110127
type: 'string'
111128
}
112-
},
113-
required: ['mail']
129+
}
114130
})
115131

116132
const obj = {
@@ -120,6 +136,7 @@ const obj = {
120136
console.log(stringify(obj)) // '{"mail":"mail@example.com"}'
121137
```
122138

139+
<a name="patternProperties"></a>
123140
#### Pattern properties
124141
`fast-json-stringify` supports pattern properties as defined inside JSON schema.
125142
*patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: `{ type: 'type' }`.
@@ -151,7 +168,9 @@ const obj = {
151168
matchnum: 3
152169
}
153170

154-
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}'
171+
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nickname":"nick"}'
172+
```
173+
155174
<a name="additionalProperties"></a>
156175
#### Additional properties
157176
`fast-json-stringify` supports additional properties as defined inside JSON schema.
@@ -193,10 +212,12 @@ const obj = {
193212
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
194213
```
195214

215+
<a name="acknowledgements"></a>
196216
## Acknowledgements
197217

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

220+
<a name="license"></a>
200221
## License
201222

202223
MIT

0 commit comments

Comments
 (0)