Skip to content

Commit d60fe7b

Browse files
Updated all docs examples to use ES6 syntax
1 parent ce1f84a commit d60fe7b

7 files changed

Lines changed: 171 additions & 178 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Example
5858
--------------------------
5959

6060
```javascript
61-
$RefParser.dereference(mySchema, function(err, schema) {
61+
$RefParser.dereference(mySchema, (err, schema) => {
6262
if (err) {
6363
console.error(err);
6464
}
@@ -67,19 +67,19 @@ $RefParser.dereference(mySchema, function(err, schema) {
6767
// including referenced files, combined into a single object
6868
console.log(schema.definitions.person.properties.firstName);
6969
}
70-
});
70+
}
7171
```
7272
73-
Or use [Promises syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead. The following example is the same as above:
73+
Or use `async`/`await` syntax instead. The following example is the same as above:
7474
7575
```javascript
76-
$RefParser.dereference(mySchema)
77-
.then(function(schema) {
78-
console.log(schema.definitions.person.properties.firstName);
79-
})
80-
.catch(function(err) {
81-
console.error(err);
82-
});
76+
try {
77+
let schema = await $RefParser.dereference(mySchema);
78+
console.log(schema.definitions.person.properties.firstName);
79+
}
80+
catch(err) {
81+
console.error(err);
82+
}
8383
```
8484
8585
For more detailed examples, please see the [API Documentation](https://apidevtools.org/json-schema-ref-parser/docs/)

docs/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ $RefParser.bundle("my-schema.json");
4444
... is the same as this:
4545

4646
```javascript
47-
var parser = new $RefParser();
47+
let parser = new $RefParser();
4848
parser.bundle("my-schema.json");
4949
```
5050

5151
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function.
5252

5353

5454
### Callbacks vs. Promises
55-
Many people prefer [ES6 Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
55+
Many people prefer [Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) or `async`/`awiat` instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
5656

57-
If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return an ES6 Promise.
57+
If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return a Promise.
5858

5959
The following two examples are equivalent:
6060

6161
```javascript
6262
// Callback syntax
63-
$RefParser.dereference(mySchema, function(err, api) {
63+
$RefParser.dereference(mySchema, (err, api) => {
6464
if (err) {
6565
// Error
6666
}
@@ -71,14 +71,15 @@ $RefParser.dereference(mySchema, function(err, api) {
7171
```
7272

7373
```javascript
74-
// ES6 Promise syntax
75-
$RefParser.dereference(mySchema)
76-
.then(function(api) {
77-
// Success
78-
})
79-
.catch(function(err) {
80-
// Error
81-
});
74+
try {
75+
// async/await syntax
76+
let api = await $RefParser.dereference(mySchema);
77+
78+
// Success
79+
}
80+
catch (err) {
81+
// Error
82+
}
8283
```
8384

8485

docs/plugins/parsers.md

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ You can see the source code for any of the built-in parsers [right here](../../l
99
The fastest way to learn is by example, so let's do that. Here's a simplistic parser that parses CSV files (comma-separated values):
1010

1111
```javascript
12-
var myParser = {
12+
let myParser = {
1313
order: 1,
1414

1515
canParse: ".csv",
1616

17-
parse: function(file) {
18-
var lines = file.data.toString().split("\n");
19-
return lines.map(function(line) {
17+
parse(file) {
18+
let lines = file.data.toString().split("\n");
19+
return lines.map((line) => {
2020
return line.split(",");
2121
});
2222
}
@@ -37,55 +37,60 @@ If _none_ of the parsers match the file, then _all_ of them are tried, in order,
3737
The `canParse` property tells JSON Schema $Ref Parser what kind of files your parser can handle. In this example, we've simply specified a file extension, but we could have used a simple boolean, an array of file extensions, a regular expression, or even a function with custom logic to determine which files to parse. Here are examples of each approach:
3838

3939
```javascript
40-
// Parse ALL file types
41-
canParse: true
40+
let myParser = {
41+
// Parse ALL file types
42+
canParse: true
4243

43-
// An array of file extensions (lowercased)
44-
canParse: [".txt", ".csv"]
44+
// An array of file extensions (lowercased)
45+
canParse: [".txt", ".csv"]
4546

46-
// A regular expression (matched against the FULL file path)
47-
canParse: /\.(txt|csv)$/i
47+
// A regular expression (matched against the FULL file path)
48+
canParse: /\.(txt|csv)$/i
4849

49-
// A function that returns a truthy/falsy value
50-
canParse: function(file) {
51-
return file.extension === ".csv" || file.extension === ".txt";
52-
}
50+
// A function that returns a truthy/falsy value
51+
canParse(file) {
52+
return file.extension === ".csv" || file.extension === ".txt";
53+
}
54+
};
5355
```
5456

5557
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being parsed.
5658

5759
#### The `parse` method
5860
Obviously, this is where the real work of a parser happens. The `parse` method accepts the same [file info object](file-info-object.md) as the `canParse` function, but rather than returning a boolean value, the `parse` method should return a JavaScript representation of the file contents. For our CSV parser, that is a two-dimensional array of lines and values. For your parser, it might be an object, a string, a custom class, or anything else.
5961

60-
Unlike the `canParse` function, the `parse` method can also be asynchronous. This might be important if your parser needs to retrieve data from a database or if it relies on an external HTTP service to return the parsed value. You can return your asynchronous value using either an [ES6 Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Here are examples of both approaches:
62+
Unlike the `canParse` function, the `parse` method can also be asynchronous. This might be important if your parser needs to retrieve data from a database or if it relies on an external HTTP service to return the parsed value. You can return your asynchronous value via a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Here are examples of both approaches:
6163

6264
```javascript
63-
// Return the value in a callback function
64-
parse: function(file, callback) {
65-
doSomethingAsync(file.data, function(data) {
66-
if (data) {
67-
// Success !
68-
callback(null, data);
69-
}
70-
else {
71-
// Error !
72-
callback(new Error("No data!"));
73-
}
74-
});
75-
}
76-
77-
// Return the value in an ES6 Promise
78-
parse: function(file) {
79-
doSomethingAsync(file.data)
80-
.then(function(data) {
65+
let myCallbackParser = {
66+
// Return the value in a callback function
67+
parse(file, callback) {
68+
doSomethingAsync(file.data, (data) => {
8169
if (data) {
8270
// Success !
83-
return data;
71+
callback(null, data);
8472
}
8573
else {
8674
// Error !
87-
throw new Error("No data!");
75+
callback(new Error("No data!"));
8876
}
8977
});
90-
}
78+
}
79+
};
80+
81+
let myPromiseParser = {
82+
// Return the value in an ES6 Promise
83+
async parse(file) {
84+
let data = await doSomethingAsync(file.data);
85+
86+
if (data) {
87+
// Success !
88+
return data;
89+
}
90+
else {
91+
// Error !
92+
throw new Error("No data!");
93+
}
94+
}
95+
};
9196
```

docs/plugins/resolvers.md

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ You can see the source code for any of the built-in resolvers [right here](../..
99
The fastest way to learn is by example, so let's do that. Here's a simplistic resolver that reads data from a MongoDB database:
1010

1111
```javascript
12-
var myResolver = {
12+
let myResolver = {
1313
order: 1,
1414

1515
canRead: /^mongodb:/i,
1616

17-
read: function(file, callback) {
18-
MongoClient.connect(file.url, function(err, db) {
17+
read(file, callback) {
18+
MongoClient.connect(file.url, (err, db) => {
1919
if (err) {
2020
callback(err);
2121
}
2222
else {
23-
db.find({}).toArray(function(err, document) {
23+
db.find({}).toArray((err, document) => {
2424
callback(null, document);
2525
});
2626
}
@@ -41,16 +41,18 @@ The `order` property and `canRead` property are related to each other. For each
4141
The `canRead` property tells JSON Schema $Ref Parser what kind of files your resolver can read. In this example, we've simply specified a regular expression that matches "mogodb://" URLs, but we could have used a simple boolean, or even a function with custom logic to determine which files to resolve. Here are examples of each approach:
4242
4343
```javascript
44-
// Read ALL files
45-
canRead: true
44+
let myResolver = {
45+
// Read ALL files
46+
canRead: true
4647

47-
// Read all files on localhost
48-
canRead: /localhost/i
48+
// Read all files on localhost
49+
canRead: /localhost/i
4950

50-
// A function that returns a truthy/falsy value
51-
canRead: function(file) {
52-
return file.url.indexOf("127.0.0.1") !== -1;
53-
}
51+
// A function that returns a truthy/falsy value
52+
canRead(file) {
53+
return file.url.indexOf("127.0.0.1") !== -1;
54+
}
55+
};
5456
```
5557
5658
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being resolved.
@@ -59,40 +61,43 @@ When using the function form, the `file` parameter is a [file info object](file-
5961
#### The `read` method
6062
This is where the real work of a resolver happens. The `read` method accepts the same [file info object](file-info-object.md) as the `canRead` function, but rather than returning a boolean value, the `read` method should return the contents of the file. The file contents should be returned in as raw a form as possible, such as a string or a byte array. Any further parsing or processing should be done by [parsers](parsers.md).
6163
62-
Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value using either an [ES6 Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
64+
Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value via a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
6365
6466
```javascript
65-
// Return the value synchronously
66-
read: function(file) {
67-
return fs.readFileSync(file.url);
68-
}
69-
70-
// Return the value in a callback function
71-
read: function(file, callback) {
72-
doSomethingAsync(file.url, function(data) {
73-
if (data) {
74-
// Success !
75-
callback(null, data);
76-
}
77-
else {
78-
// Error !
79-
callback(new Error("No data!"));
80-
}
81-
});
82-
}
67+
let myCallbackResolver = {
68+
// Return the value synchronously
69+
read(file) {
70+
return fs.readFileSync(file.url);
71+
}
8372

84-
// Return the value in an ES6 Promise
85-
read: function(file) {
86-
return doSomethingAsync(file.url)
87-
.then(function(data) {
73+
// Return the value in a callback function
74+
read(file, callback) {
75+
doSomethingAsync(file.url, (data) => {
8876
if (data) {
8977
// Success !
90-
return data;
78+
callback(null, data);
9179
}
9280
else {
9381
// Error !
94-
throw new Error("No data!");
82+
callback(new Error("No data!"));
9583
}
9684
});
97-
}
85+
}
86+
};
87+
88+
let myPromiseResolver = {
89+
// Return the value in an ES6 Promise
90+
async read(file) {
91+
let data = await doSomethingAsync(file.url);
92+
93+
if (data) {
94+
// Success !
95+
return data;
96+
}
97+
else {
98+
// Error !
99+
throw new Error("No data!");
100+
}
101+
}
102+
};
98103
```

0 commit comments

Comments
 (0)