mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Updating forEach to handle non iterable values
This commit is contained in:
Vendored
+19
-9
File diff suppressed because one or more lines are too long
Vendored
+19
-9
File diff suppressed because one or more lines are too long
Vendored
+19
-9
File diff suppressed because one or more lines are too long
Vendored
+19
-9
File diff suppressed because one or more lines are too long
+15
-2
@@ -1,5 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
|
function isArrayLike(obj) {
|
||||||
|
return obj.constructor === Array || typeof obj.callee === 'function';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*
|
*
|
||||||
@@ -13,12 +18,20 @@
|
|||||||
* @param {Function} fn The callback to invoke for each item
|
* @param {Function} fn The callback to invoke for each item
|
||||||
*/
|
*/
|
||||||
module.exports = function forEach(obj, fn) {
|
module.exports = function forEach(obj, fn) {
|
||||||
if (typeof obj !== 'object') {
|
// Don't bother if no value provided
|
||||||
|
if (obj === null || typeof obj === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isArray = isArrayLike(obj);
|
||||||
|
|
||||||
|
// Force an array if not already something iterable
|
||||||
|
if (typeof obj !== 'object' && !isArray) {
|
||||||
|
obj = [obj];
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate over array values
|
// Iterate over array values
|
||||||
if (obj.constructor === Array || typeof obj.callee === 'function') {
|
if (isArray) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ var forEach = require('./forEach');
|
|||||||
* @returns {*} The resulting transformed data
|
* @returns {*} The resulting transformed data
|
||||||
*/
|
*/
|
||||||
module.exports = function transformData(data, headers, fns) {
|
module.exports = function transformData(data, headers, fns) {
|
||||||
if (typeof fns === 'function') {
|
|
||||||
return fns(data, headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
forEach(fns, function (fn) {
|
forEach(fns, function (fn) {
|
||||||
data = fn(data, headers);
|
data = fn(data, headers);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,5 +29,27 @@ module.exports = {
|
|||||||
test.equal(keys, 'bar');
|
test.equal(keys, 'bar');
|
||||||
test.equal(vals, 6);
|
test.equal(vals, 6);
|
||||||
test.done();
|
test.done();
|
||||||
|
},
|
||||||
|
|
||||||
|
testUndefined: function (test) {
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
forEach(undefined, function () {
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
test.equals(count, 0);
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
|
||||||
|
testFunction: function (test) {
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
forEach(function () {}, function () {
|
||||||
|
count++;
|
||||||
|
})
|
||||||
|
|
||||||
|
test.equals(count, 1);
|
||||||
|
test.done();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user