Nenhuma descrição

index.js 967B

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