Skip to content

Commit c0f79bc

Browse files
Removed lodash dependency to reduce code size/speed
1 parent dd03804 commit c0f79bc

15 files changed

Lines changed: 573 additions & 2355 deletions

dist/ref-parser.js

Lines changed: 381 additions & 1872 deletions
Large diffs are not rendered by default.

dist/ref-parser.js.map

Lines changed: 12 additions & 100 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 74 additions & 200 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 12 additions & 100 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dereference.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
22

3-
var $Ref = require('./ref'),
4-
util = require('./util'),
5-
url = require('url'),
6-
_forEach = require('lodash/collection/forEach'),
7-
_isArray = require('lodash/lang/isArray'),
8-
_isObject = require('lodash/lang/isObject');
3+
var $Ref = require('./ref'),
4+
util = require('./util'),
5+
url = require('url');
96

107
module.exports = dereference;
118

@@ -31,11 +28,12 @@ function dereference(parser, options) {
3128
* @param {$RefParserOptions} options
3229
*/
3330
function crawl(obj, path, parents, $refs, options) {
34-
if (_isObject(obj) || _isArray(obj)) {
31+
if (obj && typeof(obj) === 'object') {
3532
parents.push(obj);
3633

37-
_forEach(obj, function(value, key) {
34+
Object.keys(obj).forEach(function(key) {
3835
var keyPath = path + '/' + key;
36+
var value = obj[key];
3937

4038
if ($Ref.isAllowed(value, options)) {
4139
// We found a $ref, so resolve it

lib/index.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
'use strict';
22

3-
var Promise = require('./promise'),
4-
Options = require('./options'),
5-
$Refs = require('./refs'),
6-
read = require('./read'),
7-
resolve = require('./resolve'),
8-
bundle = require('./bundle'),
9-
dereference = require('./dereference'),
10-
util = require('./util'),
11-
url = require('url'),
12-
ono = require('ono'),
13-
_isFunction = require('lodash/lang/isFunction'),
14-
_isObject = require('lodash/lang/isObject'),
15-
_isPlainObject = require('lodash/lang/isPlainObject'),
16-
_isString = require('lodash/lang/isString');
3+
var Promise = require('./promise'),
4+
Options = require('./options'),
5+
$Refs = require('./refs'),
6+
read = require('./read'),
7+
resolve = require('./resolve'),
8+
bundle = require('./bundle'),
9+
dereference = require('./dereference'),
10+
util = require('./util'),
11+
url = require('url'),
12+
ono = require('ono');
1713

1814
module.exports = $RefParser;
1915
module.exports.YAML = require('./yaml');
@@ -75,7 +71,7 @@ $RefParser.parse = function(schema, options, callback) {
7571
$RefParser.prototype.parse = function(schema, options, callback) {
7672
var args = normalizeArgs(arguments);
7773

78-
if (args.schema && _isObject(args.schema)) {
74+
if (args.schema && typeof(args.schema) === 'object') {
7975
// The schema is an object, not a path/url
8076
this.schema = args.schema;
8177
this._basePath = '';
@@ -84,7 +80,7 @@ $RefParser.prototype.parse = function(schema, options, callback) {
8480
return Promise.resolve(this.schema);
8581
}
8682

87-
if (!args.schema || !_isString(args.schema)) {
83+
if (!args.schema || typeof(args.schema) !== 'string') {
8884
var err = ono('Expected a file path, URL, or object. Got %s', args.schema);
8985
util.doCallback(args.callback, err, args.schema);
9086
return Promise.reject(err);
@@ -99,15 +95,14 @@ $RefParser.prototype.parse = function(schema, options, callback) {
9995
// Read the schema file/url
10096
return read(args.schema, this.$refs, args.options)
10197
.then(function($ref) {
102-
// Make sure the file was a POJO (in JSON or YAML format), NOT a Buffer or string
103-
if ($ref.value && _isPlainObject($ref.value)) {
98+
if (!$ref.value || typeof($ref.value) !== 'object' || $ref.value instanceof Buffer) {
99+
throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath);
100+
}
101+
else {
104102
me.schema = $ref.value;
105103
util.doCallback(args.callback, null, me.schema);
106104
return me.schema;
107105
}
108-
else {
109-
throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath);
110-
}
111106
})
112107
.catch(function(err) {
113108
util.doCallback(args.callback, err);
@@ -250,7 +245,7 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
250245
*/
251246
function normalizeArgs(args) {
252247
var options = args[1], callback = args[2];
253-
if (_isFunction(options)) {
248+
if (typeof(options) === 'function') {
254249
callback = options;
255250
options = undefined;
256251
}

lib/options.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
var _merge = require('lodash/object/merge');
4-
53
module.exports = $RefParserOptions;
64

75
/**
@@ -82,5 +80,29 @@ function $RefParserOptions(options) {
8280
https: 5 * 60 // 5 minutes
8381
};
8482

85-
_merge(this, options);
83+
merge(options, this);
84+
}
85+
86+
/**
87+
* Fast, two-level object merge.
88+
*
89+
* @param {?object} src - The object to merge into dest
90+
* @param {object} dest - The object to be modified
91+
*/
92+
function merge(src, dest) {
93+
if (src) {
94+
var topKeys = Object.keys(src);
95+
for (var i = 0; i < topKeys.length; i++) {
96+
var topKey = topKeys[i];
97+
var child = src[topKey];
98+
var childKeys = Object.keys(child);
99+
for (var j = 0; j < childKeys.length; j++) {
100+
var childKey = childKeys[j];
101+
var childValue = child[childKey];
102+
if (childValue !== undefined) {
103+
dest[topKey][childKey] = childValue;
104+
}
105+
}
106+
}
107+
}
86108
}

lib/parse.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
22

3-
var YAML = require('./yaml'),
4-
util = require('./util'),
5-
ono = require('ono'),
6-
_isEmpty = require('lodash/lang/isEmpty'),
7-
_isString = require('lodash/lang/isString');
3+
var YAML = require('./yaml'),
4+
util = require('./util'),
5+
ono = require('ono');
86

97
module.exports = parse;
108

@@ -50,13 +48,22 @@ function parse(data, path, options) {
5048
}
5149
}
5250

53-
var empty = _isEmpty(parsed) || // empty objects
54-
parsed.length === 0 || // empty Buffers
55-
(_isString(parsed) && (parsed.trim().length === 0)); // empty strings
56-
57-
if (empty && !options.allow.empty) {
51+
if (isEmpty(parsed) && !options.allow.empty) {
5852
throw ono.syntax('Error parsing "%s". \nParsed value is empty', path);
5953
}
6054

6155
return parsed;
6256
}
57+
58+
/**
59+
* Determines whether the parsed value is "empty".
60+
*
61+
* @param {*} value
62+
* @returns {boolean}
63+
*/
64+
function isEmpty(value) {
65+
return !value ||
66+
(typeof(value) === 'object' && Object.keys(value) === 0) ||
67+
(typeof(value) === 'string' && value.trim().length === 0) ||
68+
(value instanceof Buffer && value.length === 0);
69+
}

lib/read.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22

3-
var fs = require('fs'),
4-
http = require('http'),
5-
https = require('https'),
6-
parse = require('./parse'),
7-
util = require('./util'),
8-
$Ref = require('./ref'),
9-
Promise = require('./promise'),
10-
url = require('url'),
11-
ono = require('ono'),
12-
_isFunction = require('lodash/lang/isFunction');
3+
var fs = require('fs'),
4+
http = require('http'),
5+
https = require('https'),
6+
parse = require('./parse'),
7+
util = require('./util'),
8+
$Ref = require('./ref'),
9+
Promise = require('./promise'),
10+
url = require('url'),
11+
ono = require('ono');
1312

1413
module.exports = read;
1514

@@ -176,7 +175,7 @@ function download(protocol, u, options) {
176175
onResponse
177176
);
178177

179-
if (_isFunction(req.setTimeout)) {
178+
if (typeof(req.setTimeout) === 'function') {
180179
req.setTimeout(5000);
181180
}
182181

lib/ref.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
var util = require('./util'),
44
url = require('url'),
55
ono = require('ono'),
6-
_isObject = require('lodash/lang/isObject'),
7-
_isNumber = require('lodash/lang/isNumber'),
8-
_isString = require('lodash/lang/isString'),
96
escapedSlash = /~1/g,
107
escapedTilde = /~0/g;
118

@@ -83,7 +80,7 @@ $Ref.prototype.setValue = function(value, options) {
8380

8481
// Extend the cache expiration
8582
var cacheDuration = options.cache[this.type];
86-
if (_isNumber(cacheDuration) && cacheDuration > 0) {
83+
if (cacheDuration > 0) {
8784
var expires = Date.now() + (cacheDuration * 1000);
8885
this.expires = new Date(expires);
8986
}
@@ -210,7 +207,7 @@ $Ref.prototype.set = function(path, value, options) {
210207
* @returns {boolean}
211208
*/
212209
$Ref.is$Ref = function(value) {
213-
return value && _isObject(value) && _isString(value.$ref);
210+
return value && typeof(value) === 'object' && typeof(value.$ref) === 'string';
214211
};
215212

216213
/**

0 commit comments

Comments
 (0)