From 174d66e53b75bd858545874c33c5de235e4cf0dc Mon Sep 17 00:00:00 2001 From: Luan Muniz Date: Sun, 26 Jul 2015 15:06:01 -0300 Subject: [PATCH 1/2] Require just used functions & use natives --- lib/rp.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/rp.js b/lib/rp.js index 0c5e309..e689c66 100644 --- a/lib/rp.js +++ b/lib/rp.js @@ -2,7 +2,10 @@ var Bluebird = require('./bluebird-fresh.js'), CapturedTrace = require('./bluebird-captured-trace-fresh.js'), - _ = require('lodash'), + assign = require('lodash/object/assign'), + isFunction = require('lodash/lang/isFunction'), + isPlainObject = require('lodash/lang/isPlainObject'), + isString = require('lodash/lang/isString'), chalk = require('chalk'), errors = require('./errors.js'); @@ -11,18 +14,18 @@ var Bluebird = require('./bluebird-fresh.js'), var request = (function () { function clearCache() { - _.forEach(_.keys(require.cache), function (key) { + Object.keys(require.cache).forEach(function (key) { delete require.cache[key]; }); } - var temp = _.assign({}, require.cache); + var temp = assign({}, require.cache); clearCache(); var freshRequest = require('request'); clearCache(); - _.assign(require.cache, temp); + assign(require.cache, temp); return freshRequest; @@ -36,7 +39,7 @@ function RP$callback(err, response, body) { var origCallbackThrewException = false, thrownException; - if (_.isFunction(self._rp_callbackOrig)) { + if (isFunction(self._rp_callbackOrig)) { try { self._rp_callbackOrig.apply(self, arguments); } catch (e) { @@ -47,7 +50,7 @@ function RP$callback(err, response, body) { if (err) { - self._rp_reject(_.assign(new errors.RequestError(err), { + self._rp_reject(assign(new errors.RequestError(err), { error: err, options: self._rp_options, response: response @@ -55,14 +58,14 @@ function RP$callback(err, response, body) { } else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) { - self._rp_reject(_.assign(new errors.StatusCodeError(response.statusCode, body), { + self._rp_reject(assign(new errors.StatusCodeError(response.statusCode, body), { error: body, options: self._rp_options, response: response })); } else { - if (_.isFunction(self._rp_options.transform)) { + if (isFunction(self._rp_options.transform)) { try { self._rp_resolve(self._rp_options.transform(body, response)); } catch (e) { @@ -80,7 +83,7 @@ function RP$callback(err, response, body) { } // Mimic original behavior of errors emitted by request with no error listener registered - if (err && _.isFunction(self._rp_callbackOrig) === false && self._rp_promise_in_use !== true && self.listeners('error').length === 1) { + if (err && isFunction(self._rp_callbackOrig) === false && self._rp_promise_in_use !== true && self.listeners('error').length === 1) { throw err; } @@ -93,7 +96,7 @@ request.Request.prototype.init = function RP$initInterceptor(options) { var self = this; // Init may be called again - currently in case of redirects - if (_.isPlainObject(options) && self._callback === undefined && self._rp_promise === undefined) { + if (isPlainObject(options) && self._callback === undefined && self._rp_promise === undefined) { self._rp_promise = new Bluebird(function (resolve, reject) { self._rp_resolve = resolve; @@ -104,7 +107,7 @@ request.Request.prototype.init = function RP$initInterceptor(options) { self._rp_callbackOrig = self.callback; self.callback = RP$callback; - if (_.isString(options.method)) { + if (isString(options.method)) { options.method = options.method.toUpperCase(); } @@ -128,7 +131,7 @@ function expose(methodToExpose, exposeAs) { exposeAs = exposeAs || methodToExpose; /* istanbul ignore if */ - if (!_.isUndefined(request.Request.prototype[exposeAs])) { + if (request.Request.prototype[exposeAs] !== undefined) { console.error(chalk.bold.bgRed('[Request-Promise] Unable to expose method "' + exposeAs + '". It is already implemented by Request. Please visit https://github.com/request/request-promise/wiki/Troubleshooting')); return; } From 34a3995770d605e2809211e6904ae108938f811f Mon Sep 17 00:00:00 2001 From: Luan Muniz Date: Sun, 26 Jul 2015 15:36:25 -0300 Subject: [PATCH 2/2] Update lodash dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e6831e7..9847fb9 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "dependencies": { "bluebird": "^2.3", "chalk": "1.0.x", - "lodash": "3.6.x", + "lodash": "^3.6.0", "request": "^2.34" }, "devDependencies": {