unzip.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var arrayFilter = require('../internal/arrayFilter'),
  2. arrayMap = require('../internal/arrayMap'),
  3. baseProperty = require('../internal/baseProperty'),
  4. isArrayLike = require('../internal/isArrayLike');
  5. /* Native method references for those with the same name as other `lodash` methods. */
  6. var nativeMax = Math.max;
  7. /**
  8. * This method is like `_.zip` except that it accepts an array of grouped
  9. * elements and creates an array regrouping the elements to their pre-zip
  10. * configuration.
  11. *
  12. * @static
  13. * @memberOf _
  14. * @category Array
  15. * @param {Array} array The array of grouped elements to process.
  16. * @returns {Array} Returns the new array of regrouped elements.
  17. * @example
  18. *
  19. * var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
  20. * // => [['fred', 30, true], ['barney', 40, false]]
  21. *
  22. * _.unzip(zipped);
  23. * // => [['fred', 'barney'], [30, 40], [true, false]]
  24. */
  25. function unzip(array) {
  26. if (!(array && array.length)) {
  27. return [];
  28. }
  29. var index = -1,
  30. length = 0;
  31. array = arrayFilter(array, function(group) {
  32. if (isArrayLike(group)) {
  33. length = nativeMax(group.length, length);
  34. return true;
  35. }
  36. });
  37. var result = Array(length);
  38. while (++index < length) {
  39. result[index] = arrayMap(array, baseProperty(index));
  40. }
  41. return result;
  42. }
  43. module.exports = unzip;