zip.js 662 B

12345678910111213141516171819202122232425262728
  1. var max = require('./max');
  2. var map = require('./map');
  3. function getLength(arr) {
  4. return arr == null ? 0 : arr.length;
  5. }
  6. /**
  7. * Merges together the values of each of the arrays with the values at the
  8. * corresponding position.
  9. */
  10. function zip(arr){
  11. var len = arr ? max(map(arguments, getLength)) : 0,
  12. results = [],
  13. i = -1;
  14. while (++i < len) {
  15. // jshint loopfunc: true
  16. results.push(map(arguments, function(item) {
  17. return item == null ? undefined : item[i];
  18. }));
  19. }
  20. return results;
  21. }
  22. module.exports = zip;