Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use utils.forEach to loop over arguments #127

Merged
merged 1 commit into from Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 22 additions & 31 deletions lib/axios.js
Expand Up @@ -71,40 +71,31 @@ axios.spread = require('./helpers/spread');
// Expose interceptors
axios.interceptors = defaultInstance.interceptors;

// Provide aliases for supported request methods
(function () {
function createShortMethods() {
utils.forEach(arguments, function (method) {
Axios.prototype[method] = function (url, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});
}

function createShortMethodsWithData() {
utils.forEach(arguments, function (method) {
Axios.prototype[method] = function (url, data, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url,
data: data
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});
}

createShortMethods('delete', 'get', 'head');
createShortMethodsWithData('post', 'put', 'patch');
})();

// Helpers
function bind (fn, thisArg) {
return function () {
return fn.apply(thisArg, Array.prototype.slice.call(arguments));
};
}

// Provide aliases for supported request methods
utils.forEach(['delete', 'get', 'head'], function (method) {
Axios.prototype[method] = function (url, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});

utils.forEach(['post', 'put', 'patch'], function (method) {
Axios.prototype[method] = function (url, data, config) {
return this.request(utils.merge(config || {}, {
method: method,
url: url,
data: data
}));
};
axios[method] = bind(Axios.prototype[method], defaultInstance);
});
31 changes: 9 additions & 22 deletions lib/utils.js
Expand Up @@ -130,16 +130,6 @@ function trim(str) {
return str.replace(/^\s*/, '').replace(/\s*$/, '');
}

/**
* Determine if a value is an Arguments object
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an Arguments object, otherwise false
*/
function isArguments(val) {
return toString.call(val) === '[object Arguments]';
}

/**
* Determine if we're running in a standard browser environment
*
Expand All @@ -151,7 +141,7 @@ function isArguments(val) {
* typeof document -> undefined
*
* react-native:
* typeof document.createelement -> undefined
* typeof document.createElement -> undefined
*/
function isStandardBrowserEnv() {
return (
Expand All @@ -164,7 +154,7 @@ function isStandardBrowserEnv() {
/**
* Iterate over an Array or an Object invoking a function for each item.
*
* If `obj` is an Array or arguments callback will be called passing
* If `obj` is an Array callback will be called passing
* the value, index, and complete array for each item.
*
* If 'obj' is an Object callback will be called passing
Expand All @@ -179,16 +169,13 @@ function forEach(obj, fn) {
return;
}

// Check if obj is array-like
var isArrayLike = isArray(obj) || isArguments(obj);

// Force an array if not already something iterable
if (typeof obj !== 'object' && !isArrayLike) {
if (typeof obj !== 'object' && !isArray(obj)) {
obj = [obj];
}

// Iterate over array values
if (isArrayLike) {
if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
Expand Down Expand Up @@ -222,11 +209,11 @@ function forEach(obj, fn) {
*/
function merge(/*obj1, obj2, obj3, ...*/) {
var result = {};
forEach(arguments, function (obj) {
forEach(obj, function (val, key) {
result[key] = val;
});
});
var assignValue = function (val, key) { result[key] = val; };
var length = arguments.length;
for (var i = 0; i < length; i++) {
forEach(arguments[i], assignValue);
}
return result;
}

Expand Down
13 changes: 0 additions & 13 deletions test/specs/utils/forEach.spec.js
Expand Up @@ -11,18 +11,6 @@ describe('utils::forEach', function () {
expect(sum).toEqual(15);
});

it('should loop over arguments', function () {
var sum = 0;

(function () {
forEach(arguments, function (val) {
sum += val;
});
})(1, 2, 3, 4, 5);

expect(sum).toEqual(15);
});

it('should loop over object keys', function () {
var keys = '';
var vals = 0;
Expand Down Expand Up @@ -61,4 +49,3 @@ describe('utils::forEach', function () {
expect(count).toEqual(1);
});
});