You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

30 lines
675 B

  1. import ascending from "./ascending.js";
  2. export default function greatest(values, compare = ascending) {
  3. let max;
  4. let defined = false;
  5. if (compare.length === 1) {
  6. let maxValue;
  7. for (const element of values) {
  8. const value = compare(element);
  9. if (defined
  10. ? ascending(value, maxValue) > 0
  11. : ascending(value, value) === 0) {
  12. max = element;
  13. maxValue = value;
  14. defined = true;
  15. }
  16. }
  17. } else {
  18. for (const value of values) {
  19. if (defined
  20. ? compare(value, max) > 0
  21. : compare(value, value) === 0) {
  22. max = value;
  23. defined = true;
  24. }
  25. }
  26. }
  27. return max;
  28. }