unzip.js 1.3 KB

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