_baseOrderBy.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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;