Skip to content

Commit 437cda8

Browse files
BigBlueHatdavidlehn
authored andcommitted
Chop example code into sections
1 parent d8b307c commit 437cda8

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

README.md

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,23 @@ import {JsonLdProcessor} from 'jsonld';
124124
Examples
125125
--------
126126

127+
Example data and context used throughout examples below:
127128
```js
128-
var doc = {
129+
const doc = {
129130
"http://schema.org/name": "Manu Sporny",
130131
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
131132
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
132133
};
133-
var context = {
134+
const context = {
134135
"name": "http://schema.org/name",
135136
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
136137
"image": {"@id": "http://schema.org/image", "@type": "@id"}
137138
};
139+
```
138140

141+
### [compact](http://json-ld.org/spec/latest/json-ld/#compacted-document-form)
142+
```js
139143
// compact a document according to a particular context
140-
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
141144
jsonld.compact(doc, context, function(err, compacted) {
142145
console.log(JSON.stringify(compacted, null, 2));
143146
/* Output:
@@ -153,8 +156,13 @@ jsonld.compact(doc, context, function(err, compacted) {
153156
// compact using URLs
154157
jsonld.compact('http://example.org/doc', 'http://example.org/context', ...);
155158

159+
// or using promises
160+
const compacted = await jsonld.compact(doc, context);
161+
```
162+
163+
### [expand](http://json-ld.org/spec/latest/json-ld/#expanded-document-form)
164+
```js
156165
// expand a document, removing its context
157-
// see: http://json-ld.org/spec/latest/json-ld/#expanded-document-form
158166
jsonld.expand(compacted, function(err, expanded) {
159167
/* Output:
160168
{
@@ -168,20 +176,36 @@ jsonld.expand(compacted, function(err, expanded) {
168176
// expand using URLs
169177
jsonld.expand('http://example.org/doc', ...);
170178

179+
// or using promises
180+
const expanded = await jsonld.expand(doc);
181+
```
182+
183+
### [flatten](http://json-ld.org/spec/latest/json-ld/#flattened-document-form)
184+
```js
171185
// flatten a document
172-
// see: http://json-ld.org/spec/latest/json-ld/#flattened-document-form
173186
jsonld.flatten(doc, (err, flattened) => {
174187
// all deep-level trees flattened to the top-level
175188
});
176189

190+
// or using promises
191+
const flattened = await jsonld.flatten(doc);
192+
```
193+
194+
### [frame](http://json-ld.org/spec/latest/json-ld-framing/#introduction)
195+
```js
177196
// frame a document
178-
// see: http://json-ld.org/spec/latest/json-ld-framing/#introduction
179197
jsonld.frame(doc, frame, (err, framed) => {
180198
// document transformed into a particular tree structure per the given frame
181199
});
182200

201+
// or using promises
202+
const framed = await jsonld.frame(doc, frame);
203+
```
204+
205+
### [canonize](http://json-ld.github.io/normalization/spec/) (normalize)
206+
```js
183207
// canonize (normalize) a document using the RDF Dataset Normalization Algorithm
184-
// (URDNA2015), see: http://json-ld.github.io/normalization/spec/
208+
// (URDNA2015), see:
185209
jsonld.canonize(doc, {
186210
algorithm: 'URDNA2015',
187211
format: 'application/n-quads'
@@ -190,16 +214,34 @@ jsonld.canonize(doc, {
190214
// that can be used for hashing, comparison, etc.
191215
});
192216

217+
// or using promises
218+
const canonized = await jsonld.canonize(doc, {format: 'application/n-quads'});
219+
```
220+
221+
### toRDF (N-Quads)
222+
```js
193223
// serialize a document to N-Quads (RDF)
194224
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => {
195225
// nquads is a string of N-Quads
196226
});
197227

228+
// or using promises
229+
const rdf = await jsonld.toRDF(doc, {format: 'application/n-quads'});
230+
```
231+
232+
### fromRDF (N-Quads)
233+
```js
198234
// deserialize N-Quads (RDF) to JSON-LD
199235
jsonld.fromRDF(nquads, {format: 'application/n-quads'}, (err, doc) => {
200236
// doc is JSON-LD
201237
});
202238

239+
// or using promises
240+
const doc = await jsonld.fromRDF(nquads, {format: 'application/n-quads'});
241+
```
242+
243+
### Custom RDF Praser
244+
```js
203245
// register a custom async-callback-based RDF parser
204246
jsonld.registerRDFParser(contentType, (input, callback) => {
205247
// parse input to a jsonld.js RDF dataset object...
@@ -212,35 +254,15 @@ jsonld.registerRDFParser(contentType, input => {
212254
return dataset;
213255
});
214256

215-
// use the promises API:
216-
217-
// compaction
218-
const compacted = await jsonld.compact(doc, context);
219-
220-
// expansion
221-
const expanded = await jsonld.expand(doc);
222-
223-
// flattening
224-
const flattened = await jsonld.flatten(doc);
225-
226-
// framing
227-
const framed = await jsonld.frame(doc, frame);
228-
229-
// canonicalization (normalization)
230-
const canonized = await jsonld.canonize(doc, {format: 'application/n-quads'});
231-
232-
// serialize to RDF
233-
const rdf = await jsonld.toRDF(doc, {format: 'application/n-quads'});
234-
235-
// deserialize from RDF
236-
const doc = await jsonld.fromRDF(nquads, {format: 'application/n-quads'});
237-
238257
// register a custom promise-based RDF parser
239258
jsonld.registerRDFParser(contentType, async input => {
240259
// parse input into a jsonld.js RDF dataset object...
241260
return new Promise(...);
242261
});
262+
```
243263

264+
### Custom Document Loader
265+
```js
244266
// how to override the default document loader with a custom one -- for
245267
// example, one that uses pre-loaded contexts:
246268

0 commit comments

Comments
 (0)