Skip to content

Commit 2d5b52c

Browse files
Lots of refactoring. Introducing a plug-in model ("resolvers" and "parsers") for #8
1 parent 30ecdd2 commit 2d5b52c

34 files changed

+799
-629
lines changed

.eslintignore

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ coverage
22
dist
33
www
44
node_modules
5-
tests

.eslintrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"browser": true, // browser global variables.
3333
"node": true, // Node.js global variables and Node.js-specific rules.
3434
"amd": false, // defines require() and define() as global variables as per the amd spec.
35-
"mocha": true, // adds all of the Mocha testing global variables.
35+
"mocha": false, // adds all of the Mocha testing global variables.
3636
"jasmine": false, // adds all of the Jasmine testing global variables for version 0.3 and 0.0.
3737
"phantomjs": false, // phantomjs global variables.
3838
"jquery": false, // jquery global variables.
@@ -207,7 +207,7 @@
207207
"beforeColon": false, // require a space before the colon in a key/value pair
208208
"afterColon": true // require a space after the colon in a key/value pair
209209
}],
210-
"lines-around-comment": [2, { // enforce empty lines around comments
210+
"lines-around-comment": [0, { // enforce empty lines around comments
211211
"beforeBlockComment": true, // require a blank line before block comments
212212
"afterBlockComment": false, // require a blank line after block comments
213213
"beforeLineComment": false, // require a blank line before line comments

lib/bundle.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
var $Ref = require('./ref'),
1010
Pointer = require('./pointer'),
11-
util = require('./util'),
12-
url = require('url');
11+
debug = require('./util/debug'),
12+
url = require('./util/url');
1313

1414
module.exports = bundle;
1515

@@ -22,7 +22,7 @@ module.exports = bundle;
2222
* @param {$RefParserOptions} options
2323
*/
2424
function bundle(parser, options) {
25-
util.debug('Bundling $ref pointers in %s', parser.$refs._basePath);
25+
debug('Bundling $ref pointers in %s', parser.$refs._basePath);
2626

2727
// Build an inventory of all $ref pointers in the JSON Schema
2828
var inventory = [];
@@ -88,8 +88,8 @@ function inventory$Ref($refParent, $refKey, path, pathFromRoot, inventory, $refs
8888
var $refPath = url.resolve(path, $ref.$ref);
8989
var pointer = $refs._resolve($refPath, options);
9090
var depth = Pointer.parse(pathFromRoot).length;
91-
var file = util.path.stripHash(pointer.path);
92-
var hash = util.path.getHash(pointer.path);
91+
var file = url.stripHash(pointer.path);
92+
var hash = url.getHash(pointer.path);
9393
var external = file !== $refs._basePath;
9494
var extended = $Ref.isExtended$Ref($ref);
9595

@@ -160,7 +160,7 @@ function remap(inventory) {
160160

161161
var file, hash, pathFromRoot;
162162
inventory.forEach(function(i) {
163-
util.debug('Re-mapping $ref pointer "%s" at %s', i.$ref.$ref, i.pathFromRoot);
163+
debug('Re-mapping $ref pointer "%s" at %s', i.$ref.$ref, i.pathFromRoot);
164164

165165
if (!i.external) {
166166
// This $ref already resolves to the main JSON Schema file
@@ -186,6 +186,6 @@ function remap(inventory) {
186186
i.$ref.$ref = Pointer.join(pathFromRoot, Pointer.parse(i.hash));
187187
}
188188

189-
util.debug(' new value: %s', (i.$ref && i.$ref.$ref) ? i.$ref.$ref : '[object Object]');
189+
debug(' new value: %s', (i.$ref && i.$ref.$ref) ? i.$ref.$ref : '[object Object]');
190190
});
191191
}

lib/dereference.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
var $Ref = require('./ref'),
44
Pointer = require('./pointer'),
5-
util = require('./util'),
65
ono = require('ono'),
7-
url = require('url');
6+
debug = require('./util/debug'),
7+
url = require('./util/url');
88

99
module.exports = dereference;
1010

@@ -16,7 +16,7 @@ module.exports = dereference;
1616
* @param {$RefParserOptions} options
1717
*/
1818
function dereference(parser, options) {
19-
util.debug('Dereferencing $ref pointers in %s', parser.$refs._basePath);
19+
debug('Dereferencing $ref pointers in %s', parser.$refs._basePath);
2020
parser.$refs.circular = false;
2121
crawl(parser.schema, parser.$refs._basePath, '#', [], parser.$refs, options);
2222
}
@@ -79,7 +79,7 @@ function crawl(obj, path, pathFromRoot, parents, $refs, options) {
7979
* @returns {object}
8080
*/
8181
function dereference$Ref($ref, path, pathFromRoot, parents, $refs, options) {
82-
util.debug('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
82+
debug('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
8383

8484
var $refPath = url.resolve(path, $ref.$ref);
8585
var pointer = $refs._resolve($refPath, options);

lib/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var Promise = require('./util/promise'),
88
resolve = require('./resolve'),
99
bundle = require('./bundle'),
1010
dereference = require('./dereference'),
11-
util = require('./util'),
12-
url = require('url'),
11+
url = require('./util/url'),
1312
maybe = require('call-me-maybe'),
1413
ono = require('ono');
1514

@@ -82,9 +81,9 @@ $RefParser.prototype.parse = function(schema, options, callback) {
8281
}
8382

8483
// Resolve the absolute path of the schema
85-
args.schema = util.path.localPathToUrl(args.schema);
86-
args.schema = url.resolve(util.path.cwd(), args.schema);
87-
this.$refs._basePath = util.path.stripHash(args.schema);
84+
args.schema = url.localPathToUrl(args.schema);
85+
args.schema = url.resolve(url.cwd(), args.schema);
86+
this.$refs._basePath = url.stripHash(args.schema);
8887

8988
// Read the schema file/url
9089
promise = read(args.schema, this.$refs, args.options);

lib/options.js

+42-45
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
22
'use strict';
33

4-
var parseJSON = require('./parse/json'),
5-
parseYAML = require('./parse/yaml'),
6-
parseText = require('./parse/text'),
7-
parseBinary = require('./parse/binary'),
8-
readFile = require('./read/file'),
9-
readHttp = require('./read/http'),
10-
util = require('./util');
4+
var jsonParser = require('./parsers/json'),
5+
yamlParser = require('./parsers/yaml'),
6+
textParser = require('./parsers/text'),
7+
binaryParser = require('./parsers/binary'),
8+
fileResolver = require('./resolvers/file'),
9+
httpResolver = require('./resolvers/http');
1110

1211
module.exports = $RefParserOptions;
1312

@@ -18,6 +17,11 @@ module.exports = $RefParserOptions;
1817
* @constructor
1918
*/
2019
function $RefParserOptions(options) {
20+
merge(this, $RefParserOptions.defaults);
21+
merge(this, options);
22+
}
23+
24+
$RefParserOptions.defaults = {
2125
/**
2226
* Determines how different types of files will be parsed.
2327
*
@@ -36,31 +40,31 @@ function $RefParserOptions(options) {
3640
* don't contain any keys.
3741
*/
3842
this.parse = {
39-
json: parseJSON,
40-
yaml: parseYAML,
41-
text: parseText,
42-
binary: parseBinary,
43+
json: jsonParser,
44+
yaml: yamlParser,
45+
text: textParser,
46+
binary: binaryParser,
4347
};
4448

4549
/**
46-
* Determines how external JSON References will be resolved.
50+
* Determines how JSON References will be resolved.
4751
*
48-
* You can add additional readers of your own, replace an existing one with
49-
* your own implemenation, or disable any reader by setting it to false.
52+
* You can add additional resolvers of your own, replace an existing one with
53+
* your own implemenation, or disable any resolver by setting it to false.
5054
*
51-
* Each of the built-in readers has the following options:
55+
* Each of the built-in resolvers has the following options:
5256
*
53-
* order {number} - The order in which the reader will run
57+
* order {number} - The order in which the resolver will run
5458
*
5559
* cache {number} - How long to cache files (in milliseconds)
56-
* The default cache duration is different for each reader.
57-
* Setting the cache duration to zero disables caching for that reader.
60+
* The default cache duration is different for each resolver.
61+
* Setting the cache duration to zero disables caching for that resolver.
5862
*
59-
* The HTTP reader has additional options. See read/http.js for details.
63+
* The HTTP resolver has additional options. See read/http.js for details.
6064
*/
6165
this.resolve = {
62-
file: readFile,
63-
http: readHttp,
66+
file: fileResolver,
67+
http: httpResolver,
6468

6569
/**
6670
* Determines whether external $ref pointers will be resolved.
@@ -85,39 +89,32 @@ function $RefParserOptions(options) {
8589
*/
8690
circular: true
8791
};
88-
89-
merge(options, this);
90-
}
92+
};
9193

9294
/**
93-
* Merges user-specified options with default options.
95+
* Merges the specified options into the target object.
9496
*
95-
* @param {?object} user - The options that were specified by the user
96-
* @param {$RefParserOptions} defaults - The {@link $RefParserOptions} object that we're populating
97-
* @returns {*}
97+
* @param {object} target - The object that we're populating
98+
* @param {?object} source - The options that are being merged
99+
* @returns {object}
98100
*/
99-
function merge(user, defaults) {
100-
if (user) {
101-
var keys = Object.keys(user);
101+
function merge(target, source) {
102+
if (source) {
103+
var keys = Object.keys(source);
102104
for (var i = 0; i < keys.length; i++) {
103105
var key = keys[i];
104-
var userSetting = user[key];
105-
var defaultSetting = defaults[key];
106-
107-
if (userSetting && typeof userSetting === 'object' && !Array.isArray(userSetting)) {
108-
if (typeof defaultSetting === 'function') {
109-
// Create a copy of the default function, so the user can safely modify its options
110-
defaultSetting = merge(defaultSetting, util.bind(defaultSetting));
111-
}
106+
var sourceSetting = source[key];
107+
var targetSetting = target[key];
112108

113-
// Merge the user-sepcified options for this object/function
114-
defaults[key] = merge(userSetting, defaultSetting || {});
109+
if (sourceSetting && typeof sourceSetting === 'object' && !Array.isArray(sourceSetting)) {
110+
// It's a nested object, so merge it recursively
111+
target[key] = merge(targetSetting || {}, sourceSetting);
115112
}
116-
else {
117-
// A scalar value, function, or array. So override the default value.
118-
defaults[key] = userSetting;
113+
else if (sourceSetting !== undefined) {
114+
// It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
115+
target[key] = sourceSetting;
119116
}
120117
}
121118
}
122-
return defaults;
119+
return target;
123120
}

lib/parse/index.js renamed to lib/parse.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
var util = require('../util'),
4-
Promise = require('../util/promise'),
5-
ono = require('ono');
3+
var ono = require('ono'),
4+
debug = require('./util/debug'),
5+
url = require('./util/url'),
6+
Promise = require('../util/promise');
67

78
module.exports = parse;
89

@@ -17,7 +18,7 @@ module.exports = parse;
1718
*/
1819
function parse(data, path, options) {
1920
return new Promise(function(resolve, reject) {
20-
util.debug('Parsing %s', path);
21+
debug('Parsing %s', path);
2122
var parsers = getSortedParsers(path, options);
2223
util.runOrderedFunctions(parsers, data, path, options).then(onParsed, onError);
2324

@@ -52,7 +53,7 @@ function parse(data, path, options) {
5253
* @returns {{name: string, order: number, score: number, fn: function}[]}
5354
*/
5455
function getSortedParsers(path, options) {
55-
var ext = util.path.extname(path);
56+
var ext = url.extname(path);
5657
var bestScore = 3;
5758

5859
return util.getOrderedFunctions(options.parse)

lib/parse/binary.js

-42
This file was deleted.

lib/parse/json.js

-49
This file was deleted.

0 commit comments

Comments
 (0)