maxBy.js 609 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. *
  9. */
  10. var minBy = require('./minBy');
  11. var compareNumber = function compareNumber(a, b) {
  12. return a - b;
  13. };
  14. /**
  15. * Returns the maximum element as measured by a scoring function f. Returns the
  16. * first such element if there are ties.
  17. */
  18. function maxBy(as, f, compare) {
  19. compare = compare || compareNumber;
  20. return minBy(as, f, function (u, v) {
  21. return compare(v, u);
  22. });
  23. }
  24. module.exports = maxBy;