No Description

_baseOrderBy.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var arrayMap = require('./_arrayMap'),
  2. baseGet = require('./_baseGet'),
  3. baseIteratee = require('./_baseIteratee'),
  4. baseMap = require('./_baseMap'),
  5. baseSortBy = require('./_baseSortBy'),
  6. baseUnary = require('./_baseUnary'),
  7. compareMultiple = require('./_compareMultiple'),
  8. identity = require('./identity'),
  9. isArray = require('./isArray');
  10. /**
  11. * The base implementation of `_.orderBy` without param guards.
  12. *
  13. * @private
  14. * @param {Array|Object} collection The collection to iterate over.
  15. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  16. * @param {string[]} orders The sort orders of `iteratees`.
  17. * @returns {Array} Returns the new sorted array.
  18. */
  19. function baseOrderBy(collection, iteratees, orders) {
  20. if (iteratees.length) {
  21. iteratees = arrayMap(iteratees, function(iteratee) {
  22. if (isArray(iteratee)) {
  23. return function(value) {
  24. return baseGet(value, iteratee.length === 1 ? iteratee[0] : iteratee);
  25. }
  26. }
  27. return iteratee;
  28. });
  29. } else {
  30. iteratees = [identity];
  31. }
  32. var index = -1;
  33. iteratees = arrayMap(iteratees, baseUnary(baseIteratee));
  34. var result = baseMap(collection, function(value, key, collection) {
  35. var criteria = arrayMap(iteratees, function(iteratee) {
  36. return iteratee(value);
  37. });
  38. return { 'criteria': criteria, 'index': ++index, 'value': value };
  39. });
  40. return baseSortBy(result, function(object, other) {
  41. return compareMultiple(object, other, orders);
  42. });
  43. }
  44. module.exports = baseOrderBy;