Skip to content

Commit 319138d

Browse files
Updated to ES6 syntax
1 parent eee464c commit 319138d

File tree

14 files changed

+87
-88
lines changed

14 files changed

+87
-88
lines changed

lib/bundle.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function crawl (parent, key, path, pathFromRoot, indirections, inventory, $refs,
6565
});
6666

6767
// eslint-disable-next-line no-shadow
68-
keys.forEach((key) => {
68+
for (let key of keys) {
6969
let keyPath = Pointer.join(path, key);
7070
let keyPathFromRoot = Pointer.join(pathFromRoot, key);
7171
let value = obj[key];
@@ -76,7 +76,7 @@ function crawl (parent, key, path, pathFromRoot, indirections, inventory, $refs,
7676
else {
7777
crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options);
7878
}
79-
});
79+
}
8080
}
8181
}
8282
}
@@ -202,7 +202,7 @@ function remap (inventory) {
202202
});
203203

204204
let file, hash, pathFromRoot;
205-
inventory.forEach((entry) => {
205+
for (let entry of inventory) {
206206
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
207207

208208
if (!entry.external) {
@@ -234,7 +234,7 @@ function remap (inventory) {
234234
}
235235

236236
// console.log(' new value: %s', (entry.$ref && entry.$ref.$ref) ? entry.$ref.$ref : '[object Object]');
237-
});
237+
}
238238
}
239239

240240
/**

lib/dereference.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function crawl (obj, path, pathFromRoot, parents, $refs, options) {
4848
result.value = dereferenced.value;
4949
}
5050
else {
51-
Object.keys(obj).forEach((key) => {
51+
for (let key of Object.keys(obj)) {
5252
let keyPath = Pointer.join(path, key);
5353
let keyPathFromRoot = Pointer.join(pathFromRoot, key);
5454
let value = obj[key];
@@ -72,7 +72,7 @@ function crawl (obj, path, pathFromRoot, parents, $refs, options) {
7272

7373
// Set the "isCircular" flag if this or any other property is circular
7474
result.circular = result.circular || circular;
75-
});
75+
}
7676
}
7777

7878
parents.pop();

lib/index.js

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ $RefParser.parse = function (path, schema, options, callback) {
6666
* @param {function} [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
6767
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
6868
*/
69-
$RefParser.prototype.parse = function (path, schema, options, callback) {
69+
$RefParser.prototype.parse = async function (path, schema, options, callback) {
7070
let args = normalizeArgs(arguments);
7171
let promise;
7272

@@ -108,19 +108,20 @@ $RefParser.prototype.parse = function (path, schema, options, callback) {
108108
}
109109

110110
let me = this;
111-
return promise
112-
.then((result) => {
113-
if (!result || typeof result !== "object" || Buffer.isBuffer(result)) {
114-
throw ono.syntax(`"${me.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
115-
}
116-
else {
117-
me.schema = result;
118-
return maybe(args.callback, Promise.resolve(me.schema));
119-
}
120-
})
121-
.catch((e) => {
122-
return maybe(args.callback, Promise.reject(e));
123-
});
111+
try {
112+
let result = await promise;
113+
114+
if (!result || typeof result !== "object" || Buffer.isBuffer(result)) {
115+
throw ono.syntax(`"${me.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
116+
}
117+
else {
118+
me.schema = result;
119+
return maybe(args.callback, Promise.resolve(me.schema));
120+
}
121+
}
122+
catch (e) {
123+
return maybe(args.callback, Promise.reject(e));
124+
}
124125
};
125126

126127
/**
@@ -155,20 +156,18 @@ $RefParser.resolve = function (path, schema, options, callback) {
155156
* @returns {Promise}
156157
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
157158
*/
158-
$RefParser.prototype.resolve = function (path, schema, options, callback) {
159+
$RefParser.prototype.resolve = async function (path, schema, options, callback) {
159160
let me = this;
160161
let args = normalizeArgs(arguments);
161162

162-
return this.parse(args.path, args.schema, args.options)
163-
.then(() => {
164-
return resolveExternal(me, args.options);
165-
})
166-
.then(() => {
167-
return maybe(args.callback, Promise.resolve(me.$refs));
168-
})
169-
.catch((err) => {
170-
return maybe(args.callback, Promise.reject(err));
171-
});
163+
try {
164+
await this.parse(args.path, args.schema, args.options);
165+
await resolveExternal(me, args.options);
166+
return maybe(args.callback, Promise.resolve(me.$refs));
167+
}
168+
catch (err) {
169+
return maybe(args.callback, Promise.reject(err));
170+
}
172171
};
173172

174173
/**
@@ -199,18 +198,18 @@ $RefParser.bundle = function (path, schema, options, callback) {
199198
* @param {function} [callback] - An error-first callback. The second parameter is the bundled JSON schema object
200199
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
201200
*/
202-
$RefParser.prototype.bundle = function (path, schema, options, callback) {
201+
$RefParser.prototype.bundle = async function (path, schema, options, callback) {
203202
let me = this;
204203
let args = normalizeArgs(arguments);
205204

206-
return this.resolve(args.path, args.schema, args.options)
207-
.then(() => {
208-
bundle(me, args.options);
209-
return maybe(args.callback, Promise.resolve(me.schema));
210-
})
211-
.catch((err) => {
212-
return maybe(args.callback, Promise.reject(err));
213-
});
205+
try {
206+
await this.resolve(args.path, args.schema, args.options);
207+
bundle(me, args.options);
208+
return maybe(args.callback, Promise.resolve(me.schema));
209+
}
210+
catch (err) {
211+
return maybe(args.callback, Promise.reject(err));
212+
}
214213
};
215214

216215
/**
@@ -239,16 +238,16 @@ $RefParser.dereference = function (path, schema, options, callback) {
239238
* @param {function} [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
240239
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
241240
*/
242-
$RefParser.prototype.dereference = function (path, schema, options, callback) {
241+
$RefParser.prototype.dereference = async function (path, schema, options, callback) {
243242
let me = this;
244243
let args = normalizeArgs(arguments);
245244

246-
return this.resolve(args.path, args.schema, args.options)
247-
.then(() => {
248-
dereference(me, args.options);
249-
return maybe(args.callback, Promise.resolve(me.schema));
250-
})
251-
.catch((err) => {
252-
return maybe(args.callback, Promise.reject(err));
253-
});
245+
try {
246+
await this.resolve(args.path, args.schema, args.options);
247+
dereference(me, args.options);
248+
return maybe(args.callback, Promise.resolve(me.schema));
249+
}
250+
catch (err) {
251+
return maybe(args.callback, Promise.reject(err));
252+
}
254253
};

lib/parse.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = parse;
1616
* @returns {Promise}
1717
* The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
1818
*/
19-
function parse (path, $refs, options) {
19+
async function parse (path, $refs, options) {
2020
try {
2121
// Remove the URL fragment, if any
2222
path = url.stripHash(path);
@@ -32,16 +32,14 @@ function parse (path, $refs, options) {
3232
};
3333

3434
// Read the file and then parse the data
35-
return readFile(file, options)
36-
.then((resolver) => {
37-
$ref.pathType = resolver.plugin.name;
38-
file.data = resolver.result;
39-
return parseFile(file, options);
40-
})
41-
.then((parser) => {
42-
$ref.value = parser.result;
43-
return parser.result;
44-
});
35+
const resolver = await readFile(file, options);
36+
$ref.pathType = resolver.plugin.name;
37+
file.data = resolver.result;
38+
39+
const parser = await parseFile(file, options);
40+
$ref.value = parser.result;
41+
42+
return parser.result;
4543
}
4644
catch (e) {
4745
return Promise.reject(e);

lib/parsers/binary.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
3030
* @returns {boolean}
3131
*/
32-
canParse: function isBinary (file) {
32+
canParse (file) {
3333
// Use this parser if the file is a Buffer, and has a known binary extension
3434
return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url);
3535
},
@@ -43,7 +43,7 @@ module.exports = {
4343
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
4444
* @returns {Promise<Buffer>}
4545
*/
46-
parse: function parseBinary (file) {
46+
parse (file) {
4747
if (Buffer.isBuffer(file.data)) {
4848
return file.data;
4949
}

lib/parsers/json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
3535
* @returns {Promise}
3636
*/
37-
parse: function parseJSON (file) {
37+
parse (file) {
3838
return new Promise(((resolve, reject) => {
3939
let data = file.data;
4040
if (Buffer.isBuffer(data)) {

lib/parsers/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
3737
* @returns {boolean}
3838
*/
39-
canParse: function isText (file) {
39+
canParse (file) {
4040
// Use this parser if the file is a string or Buffer, and has a known text-based extension
4141
return (typeof file.data === "string" || Buffer.isBuffer(file.data)) && TEXT_REGEXP.test(file.url);
4242
},
@@ -50,7 +50,7 @@ module.exports = {
5050
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
5151
* @returns {Promise<string>}
5252
*/
53-
parse: function parseText (file) {
53+
parse (file) {
5454
if (typeof file.data === "string") {
5555
return file.data;
5656
}

lib/parsers/yaml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
3737
* @returns {Promise}
3838
*/
39-
parse: function parseYAML (file) {
39+
parse (file) {
4040
return new Promise(((resolve, reject) => {
4141
let data = file.data;
4242
if (Buffer.isBuffer(data)) {

lib/ref.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ $Ref.isExtended$Ref = function (value) {
213213
$Ref.dereference = function ($ref, resolvedValue) {
214214
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
215215
let merged = {};
216-
Object.keys($ref).forEach((key) => {
216+
for (let key of Object.keys($ref)) {
217217
if (key !== "$ref") {
218218
merged[key] = $ref[key];
219219
}
220-
});
221-
Object.keys(resolvedValue).forEach((key) => {
220+
}
221+
222+
for (let key of Object.keys(resolvedValue)) {
222223
if (!(key in merged)) {
223224
merged[key] = resolvedValue[key];
224225
}
225-
});
226+
}
227+
226228
return merged;
227229
}
228230
else {

lib/resolve-external.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function crawl (obj, path, $refs, options) {
5858
promises.push(resolve$Ref(obj, path, $refs, options));
5959
}
6060
else {
61-
Object.keys(obj).forEach((key) => {
61+
for (let key of Object.keys(obj)) {
6262
let keyPath = Pointer.join(path, key);
6363
let value = obj[key];
6464

@@ -68,7 +68,7 @@ function crawl (obj, path, $refs, options) {
6868
else {
6969
promises = promises.concat(crawl(value, keyPath, $refs, options));
7070
}
71-
});
71+
}
7272
}
7373
}
7474

@@ -87,7 +87,7 @@ function crawl (obj, path, $refs, options) {
8787
* The promise resolves once all JSON references in the object have been resolved,
8888
* including nested references that are contained in externally-referenced files.
8989
*/
90-
function resolve$Ref ($ref, path, $refs, options) {
90+
async function resolve$Ref ($ref, path, $refs, options) {
9191
// console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
9292

9393
let resolvedPath = url.resolve(path, $ref.$ref);
@@ -101,11 +101,11 @@ function resolve$Ref ($ref, path, $refs, options) {
101101
}
102102

103103
// Parse the $referenced file/url
104-
return parse(resolvedPath, $refs, options)
105-
.then((result) => {
106-
// Crawl the parsed value
107-
// console.log('Resolving $ref pointers in %s', withoutHash);
108-
let promises = crawl(result, withoutHash + "#", $refs, options);
109-
return Promise.all(promises);
110-
});
104+
const result = await parse(resolvedPath, $refs, options);
105+
106+
// Crawl the parsed value
107+
// console.log('Resolving $ref pointers in %s', withoutHash);
108+
let promises = crawl(result, withoutHash + "#", $refs, options);
109+
110+
return Promise.all(promises);
111111
}

0 commit comments

Comments
 (0)