Skip to content

Commit

Permalink
Merge pull request #127 from ctimmerm/ie8-arguments-foreach
Browse files Browse the repository at this point in the history
Don't use utils.forEach to loop over arguments
  • Loading branch information
mzabriskie committed Oct 28, 2015
2 parents a090b05 + 1e2cb9b commit ed4a39a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 66 deletions.
53 changes: 22 additions & 31 deletions lib/axios.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});
});

0 comments on commit ed4a39a

Please sign in to comment.