collect.js 646 B

123456789101112131415161718192021222324252627
  1. var append = require('./append');
  2. var makeIterator = require('../function/makeIterator_');
  3. /**
  4. * Maps the items in the array and concatenates the result arrays.
  5. */
  6. function collect(arr, callback, thisObj){
  7. callback = makeIterator(callback, thisObj);
  8. var results = [];
  9. if (arr == null) {
  10. return results;
  11. }
  12. var i = -1, len = arr.length;
  13. while (++i < len) {
  14. var value = callback(arr[i], i, arr);
  15. if (value != null) {
  16. append(results, value);
  17. }
  18. }
  19. return results;
  20. }
  21. module.exports = collect;