1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
var util = require('util');
|
4 |
|
|
var visit = require('object-visit');
|
5 |
|
|
|
6 |
|
|
/**
|
7 |
|
|
* Map `visit` over an array of objects.
|
8 |
|
|
*
|
9 |
|
|
* @param {Object} `collection` The context in which to invoke `method`
|
10 |
|
|
* @param {String} `method` Name of the method to call on `collection`
|
11 |
|
|
* @param {Object} `arr` Array of objects.
|
12 |
|
|
*/
|
13 |
|
|
|
14 |
|
|
module.exports = function mapVisit(collection, method, val) {
|
15 |
|
|
if (isObject(val)) {
|
16 |
|
|
return visit.apply(null, arguments);
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
if (!Array.isArray(val)) {
|
20 |
|
|
throw new TypeError('expected an array: ' + util.inspect(val));
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
var args = [].slice.call(arguments, 3);
|
24 |
|
|
|
25 |
|
|
for (var i = 0; i < val.length; i++) {
|
26 |
|
|
var ele = val[i];
|
27 |
|
|
if (isObject(ele)) {
|
28 |
|
|
visit.apply(null, [collection, method, ele].concat(args));
|
29 |
|
|
} else {
|
30 |
|
|
collection[method].apply(collection, [ele].concat(args));
|
31 |
|
|
}
|
32 |
|
|
}
|
33 |
|
|
};
|
34 |
|
|
|
35 |
|
|
function isObject(val) {
|
36 |
|
|
return val && (typeof val === 'function' || (!Array.isArray(val) && typeof val === 'object'));
|
37 |
|
|
}
|