提交 ed4a39a5 编写于 作者: M Matt Zabriskie

Merge pull request #127 from ctimmerm/ie8-arguments-foreach

Don't use utils.forEach to loop over arguments
...@@ -71,40 +71,31 @@ axios.spread = require('./helpers/spread'); ...@@ -71,40 +71,31 @@ axios.spread = require('./helpers/spread');
// Expose interceptors // Expose interceptors
axios.interceptors = defaultInstance.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 // Helpers
function bind (fn, thisArg) { function bind (fn, thisArg) {
return function () { return function () {
return fn.apply(thisArg, Array.prototype.slice.call(arguments)); 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);
});
...@@ -130,16 +130,6 @@ function trim(str) { ...@@ -130,16 +130,6 @@ function trim(str) {
return str.replace(/^\s*/, '').replace(/\s*$/, ''); 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 * Determine if we're running in a standard browser environment
* *
...@@ -151,7 +141,7 @@ function isArguments(val) { ...@@ -151,7 +141,7 @@ function isArguments(val) {
* typeof document -> undefined * typeof document -> undefined
* *
* react-native: * react-native:
* typeof document.createelement -> undefined * typeof document.createElement -> undefined
*/ */
function isStandardBrowserEnv() { function isStandardBrowserEnv() {
return ( return (
...@@ -164,7 +154,7 @@ function isStandardBrowserEnv() { ...@@ -164,7 +154,7 @@ function isStandardBrowserEnv() {
/** /**
* Iterate over an Array or an Object invoking a function for each item. * 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. * the value, index, and complete array for each item.
* *
* If 'obj' is an Object callback will be called passing * If 'obj' is an Object callback will be called passing
...@@ -179,16 +169,13 @@ function forEach(obj, fn) { ...@@ -179,16 +169,13 @@ function forEach(obj, fn) {
return; return;
} }
// Check if obj is array-like
var isArrayLike = isArray(obj) || isArguments(obj);
// Force an array if not already something iterable // Force an array if not already something iterable
if (typeof obj !== 'object' && !isArrayLike) { if (typeof obj !== 'object' && !isArray(obj)) {
obj = [obj]; obj = [obj];
} }
// Iterate over array values // Iterate over array values
if (isArrayLike) { if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) { for (var i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj); fn.call(null, obj[i], i, obj);
} }
...@@ -222,11 +209,11 @@ function forEach(obj, fn) { ...@@ -222,11 +209,11 @@ function forEach(obj, fn) {
*/ */
function merge(/*obj1, obj2, obj3, ...*/) { function merge(/*obj1, obj2, obj3, ...*/) {
var result = {}; var result = {};
forEach(arguments, function (obj) { var assignValue = function (val, key) { result[key] = val; };
forEach(obj, function (val, key) { var length = arguments.length;
result[key] = val; for (var i = 0; i < length; i++) {
}); forEach(arguments[i], assignValue);
}); }
return result; return result;
} }
......
...@@ -11,18 +11,6 @@ describe('utils::forEach', function () { ...@@ -11,18 +11,6 @@ describe('utils::forEach', function () {
expect(sum).toEqual(15); 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 () { it('should loop over object keys', function () {
var keys = ''; var keys = '';
var vals = 0; var vals = 0;
...@@ -61,4 +49,3 @@ describe('utils::forEach', function () { ...@@ -61,4 +49,3 @@ describe('utils::forEach', function () {
expect(count).toEqual(1); expect(count).toEqual(1);
}); });
}); });
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册