Skip to content

Commit dd03804

Browse files
Refactoring to reduce redundancy
1 parent 6c39f7a commit dd03804

4 files changed

Lines changed: 75 additions & 63 deletions

File tree

lib/index.js

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,48 +73,44 @@ $RefParser.parse = function(schema, options, callback) {
7373
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
7474
*/
7575
$RefParser.prototype.parse = function(schema, options, callback) {
76-
if (_isFunction(options)) {
77-
callback = options;
78-
options = undefined;
79-
}
76+
var args = normalizeArgs(arguments);
8077

81-
if (schema && _isObject(schema)) {
78+
if (args.schema && _isObject(args.schema)) {
8279
// The schema is an object, not a path/url
83-
this.schema = schema;
80+
this.schema = args.schema;
8481
this._basePath = '';
8582

86-
util.doCallback(callback, null, this.schema);
83+
util.doCallback(args.callback, null, this.schema);
8784
return Promise.resolve(this.schema);
8885
}
8986

90-
if (!schema || !_isString(schema)) {
91-
var err = ono('Expected a file path, URL, or object. Got %s', schema);
92-
util.doCallback(callback, err, schema);
87+
if (!args.schema || !_isString(args.schema)) {
88+
var err = ono('Expected a file path, URL, or object. Got %s', args.schema);
89+
util.doCallback(args.callback, err, args.schema);
9390
return Promise.reject(err);
9491
}
9592

96-
options = new Options(options);
9793
var me = this;
9894

9995
// Resolve the absolute path of the schema
100-
schema = url.resolve(util.cwd(), schema);
101-
this._basePath = util.stripHash(schema);
96+
args.schema = url.resolve(util.cwd(), args.schema);
97+
this._basePath = util.stripHash(args.schema);
10298

10399
// Read the schema file/url
104-
return read(schema, this, options)
100+
return read(args.schema, this.$refs, args.options)
105101
.then(function($ref) {
106102
// Make sure the file was a POJO (in JSON or YAML format), NOT a Buffer or string
107103
if ($ref.value && _isPlainObject($ref.value)) {
108104
me.schema = $ref.value;
109-
util.doCallback(callback, null, me.schema);
105+
util.doCallback(args.callback, null, me.schema);
110106
return me.schema;
111107
}
112108
else {
113109
throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath);
114110
}
115111
})
116112
.catch(function(err) {
117-
util.doCallback(callback, err);
113+
util.doCallback(args.callback, err);
118114
return Promise.reject(err);
119115
});
120116
};
@@ -149,24 +145,19 @@ $RefParser.resolve = function(schema, options, callback) {
149145
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
150146
*/
151147
$RefParser.prototype.resolve = function(schema, options, callback) {
152-
if (_isFunction(options)) {
153-
callback = options;
154-
options = undefined;
155-
}
156-
157-
options = new Options(options);
158148
var me = this;
149+
var args = normalizeArgs(arguments);
159150

160-
return this.parse(schema, options)
151+
return this.parse(args.schema, args.options)
161152
.then(function() {
162-
return resolve(me, options);
153+
return resolve(me, args.options);
163154
})
164155
.then(function() {
165-
util.doCallback(callback, null, me.$refs);
156+
util.doCallback(args.callback, null, me.$refs);
166157
return me.$refs;
167158
})
168159
.catch(function(err) {
169-
util.doCallback(callback, err, me.$refs);
160+
util.doCallback(args.callback, err, me.$refs);
170161
return Promise.reject(err);
171162
});
172163
};
@@ -197,22 +188,17 @@ $RefParser.bundle = function(schema, options, callback) {
197188
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
198189
*/
199190
$RefParser.prototype.bundle = function(schema, options, callback) {
200-
if (_isFunction(options)) {
201-
callback = options;
202-
options = undefined;
203-
}
204-
205-
options = new Options(options);
206191
var me = this;
192+
var args = normalizeArgs(arguments);
207193

208-
return this.resolve(schema, options)
194+
return this.resolve(args.schema, args.options)
209195
.then(function() {
210-
bundle(me, options);
211-
util.doCallback(callback, null, me.schema);
196+
bundle(me, args.options);
197+
util.doCallback(args.callback, null, me.schema);
212198
return me.schema;
213199
})
214200
.catch(function(err) {
215-
util.doCallback(callback, err, me.schema);
201+
util.doCallback(args.callback, err, me.schema);
216202
return Promise.reject(err);
217203
});
218204
};
@@ -241,22 +227,39 @@ $RefParser.dereference = function(schema, options, callback) {
241227
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
242228
*/
243229
$RefParser.prototype.dereference = function(schema, options, callback) {
244-
if (_isFunction(options)) {
245-
callback = options;
246-
options = undefined;
247-
}
248-
249-
options = new Options(options);
250230
var me = this;
231+
var args = normalizeArgs(arguments);
251232

252-
return this.resolve(schema, options)
233+
return this.resolve(args.schema, args.options)
253234
.then(function() {
254-
dereference(me, options);
255-
util.doCallback(callback, null, me.schema);
235+
dereference(me, args.options);
236+
util.doCallback(args.callback, null, me.schema);
256237
return me.schema;
257238
})
258239
.catch(function(err) {
259-
util.doCallback(callback, err, me.schema);
240+
util.doCallback(args.callback, err, me.schema);
260241
return Promise.reject(err);
261242
});
262243
};
244+
245+
/**
246+
* Normalizes the given arguments, accounting for optional args.
247+
*
248+
* @param {Arguments} args
249+
* @returns {object}
250+
*/
251+
function normalizeArgs(args) {
252+
var options = args[1], callback = args[2];
253+
if (_isFunction(options)) {
254+
callback = options;
255+
options = undefined;
256+
}
257+
if (!(options instanceof Options)) {
258+
options = new Options(options);
259+
}
260+
return {
261+
schema: args[0],
262+
options: options,
263+
callback: callback
264+
};
265+
}

lib/read.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ module.exports = read;
1717
* Reads the specified file path or URL, possibly from cache.
1818
*
1919
* @param {string} path - This path MUST already be resolved, since `read` doesn't know the resolution context
20-
* @param {$RefParser} parser
20+
* @param {$Refs} $refs
2121
* @param {$RefParserOptions} options
2222
*
2323
* @returns {Promise}
2424
* The promise resolves with a {@link $Ref} object.
2525
* {@link $Ref#cached} will be true if the file was already cached.
2626
*/
27-
function read(path, parser, options) {
27+
function read(path, $refs, options) {
2828
try {
2929
// Remove the URL fragment, if any
3030
path = util.stripHash(path);
3131
util.debug('Reading %s', path);
3232

3333
// Return from cache, if possible
34-
var $ref = parser.$refs._get$Ref(path);
34+
var $ref = $refs._get$Ref(path);
3535
if ($ref && !$ref.isExpired()) {
3636
util.debug(' cached from %s', $ref.type);
3737
$ref.cached = true;
@@ -40,10 +40,10 @@ function read(path, parser, options) {
4040

4141
// Add a placeholder $ref to the cache, so we don't read this URL multiple times
4242
$ref = new $Ref(path);
43-
parser.$refs._set$Ref($ref);
43+
$refs._set$Ref($ref);
4444

4545
// Read and return the $ref
46-
return read$Ref($ref, parser, options);
46+
return read$Ref($ref, options);
4747
}
4848
catch (e) {
4949
return Promise.reject(e);
@@ -54,13 +54,12 @@ function read(path, parser, options) {
5454
* Reads the specified file path or URL and updates the given {@link $Ref} accordingly.
5555
*
5656
* @param {$Ref} $ref - The {@link $Ref} to read and update
57-
* @param {$RefParser} parser
5857
* @param {$RefParserOptions} options
5958
*
6059
* @returns {Promise}
6160
* The promise resolves with the updated {@link $Ref} object (the same instance that was passed in)
6261
*/
63-
function read$Ref($ref, parser, options) {
62+
function read$Ref($ref, options) {
6463
try {
6564
var promise = options.$refs.external && (read$RefFile($ref, options) || read$RefUrl($ref, options));
6665

lib/ref.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ $Ref.prototype.set = function(path, value, options) {
203203
}
204204
};
205205

206+
/**
207+
* Determines whether the given value is a JSON reference.
208+
*
209+
* @param {*} value - The value to inspect
210+
* @returns {boolean}
211+
*/
212+
$Ref.is$Ref = function(value) {
213+
return value && _isObject(value) && _isString(value.$ref);
214+
};
215+
206216
/**
207217
* Determines whether the given value is a JSON reference, and whether it is allowed by the options.
208218
* For example, if it references an external file, then options.$refs.external must be true.
@@ -212,7 +222,7 @@ $Ref.prototype.set = function(path, value, options) {
212222
* @returns {boolean}
213223
*/
214224
$Ref.isAllowed = function(value, options) {
215-
if (value && _isObject(value) && _isString(value.$ref)) {
225+
if ($Ref.is$Ref(value)) {
216226
if (value.$ref[0] === '#') {
217227
if (options.$refs.internal) {
218228
return true;

lib/resolve.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function resolve(parser, options) {
3333
}
3434

3535
util.debug('Resolving $ref pointers in %s', parser._basePath);
36-
var promises = crawl(parser.schema, parser._basePath + '#', parser, options);
36+
var promises = crawl(parser.schema, parser._basePath + '#', parser.$refs, options);
3737
return Promise.all(promises);
3838
}
3939
catch (e) {
@@ -46,7 +46,7 @@ function resolve(parser, options) {
4646
*
4747
* @param {*} obj - The value to crawl. If it's not an object or array, it will be ignored.
4848
* @param {string} path - The path to use for resolving relative JSON references.
49-
* @param {$RefParser} parser
49+
* @param {$Refs} $refs
5050
* @param {$RefParserOptions} options
5151
*
5252
* @returns {Promise[]}
@@ -55,7 +55,7 @@ function resolve(parser, options) {
5555
* If any of the JSON references point to files that contain additional JSON references,
5656
* then the corresponding promise will internally reference an array of promises.
5757
*/
58-
function crawl(obj, path, parser, options) {
58+
function crawl(obj, path, $refs, options) {
5959
var promises = [];
6060

6161
if (_isObject(obj) || _isArray(obj)) {
@@ -68,14 +68,14 @@ function crawl(obj, path, parser, options) {
6868
var $refPath = url.resolve(path, value);
6969

7070
// Crawl the $referenced value
71-
var promise = crawl$Ref($refPath, parser, options)
71+
var promise = crawl$Ref($refPath, $refs, options)
7272
.catch(function(err) {
7373
throw ono.syntax(err, 'Error at %s', keyPath);
7474
});
7575
promises.push(promise);
7676
}
7777
else {
78-
promises = promises.concat(crawl(value, keyPath, parser, options));
78+
promises = promises.concat(crawl(value, keyPath, $refs, options));
7979
}
8080
});
8181
}
@@ -87,21 +87,21 @@ function crawl(obj, path, parser, options) {
8787
* any external JSON references.
8888
*
8989
* @param {string} path - The file path or URL to crawl
90-
* @param {$RefParser} parser
90+
* @param {$Refs} $refs
9191
* @param {$RefParserOptions} options
9292
*
9393
* @returns {Promise}
9494
* The promise resolves once all JSON references in the object have been resolved,
9595
* including nested references that are contained in externally-referenced files.
9696
*/
97-
function crawl$Ref(path, parser, options) {
98-
return read(path, parser, options)
97+
function crawl$Ref(path, $refs, options) {
98+
return read(path, $refs, options)
9999
.then(function($ref) {
100100
// If a cached $ref is returned, then we DON'T need to crawl it
101101
if (!$ref.cached) {
102102
// This is a new $ref, so we need to crawl it
103103
util.debug('Resolving $ref pointers in %s', $ref.path);
104-
var promises = crawl($ref.value, $ref.path + '#', parser, options);
104+
var promises = crawl($ref.value, $ref.path + '#', $refs, options);
105105
return Promise.all(promises);
106106
}
107107
});

0 commit comments

Comments
 (0)