1
1
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
2
2
'use strict' ;
3
3
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' ) ;
11
10
12
11
module . exports = $RefParserOptions ;
13
12
@@ -18,6 +17,11 @@ module.exports = $RefParserOptions;
18
17
* @constructor
19
18
*/
20
19
function $RefParserOptions ( options ) {
20
+ merge ( this , $RefParserOptions . defaults ) ;
21
+ merge ( this , options ) ;
22
+ }
23
+
24
+ $RefParserOptions . defaults = {
21
25
/**
22
26
* Determines how different types of files will be parsed.
23
27
*
@@ -36,31 +40,31 @@ function $RefParserOptions(options) {
36
40
* don't contain any keys.
37
41
*/
38
42
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 ,
43
47
} ;
44
48
45
49
/**
46
- * Determines how external JSON References will be resolved.
50
+ * Determines how JSON References will be resolved.
47
51
*
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.
50
54
*
51
- * Each of the built-in readers has the following options:
55
+ * Each of the built-in resolvers has the following options:
52
56
*
53
- * order {number} - The order in which the reader will run
57
+ * order {number} - The order in which the resolver will run
54
58
*
55
59
* 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 .
58
62
*
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.
60
64
*/
61
65
this . resolve = {
62
- file : readFile ,
63
- http : readHttp ,
66
+ file : fileResolver ,
67
+ http : httpResolver ,
64
68
65
69
/**
66
70
* Determines whether external $ref pointers will be resolved.
@@ -85,39 +89,32 @@ function $RefParserOptions(options) {
85
89
*/
86
90
circular : true
87
91
} ;
88
-
89
- merge ( options , this ) ;
90
- }
92
+ } ;
91
93
92
94
/**
93
- * Merges user- specified options with default options .
95
+ * Merges the specified options into the target object .
94
96
*
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 }
98
100
*/
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 ) ;
102
104
for ( var i = 0 ; i < keys . length ; i ++ ) {
103
105
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 ] ;
112
108
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 ) ;
115
112
}
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 ;
119
116
}
120
117
}
121
118
}
122
- return defaults ;
119
+ return target ;
123
120
}
0 commit comments