https://github.com/lamansky/utmost
[Node.js] Returns the item which ranks highest by some criterion.
https://github.com/lamansky/utmost
Last synced: 3 months ago
JSON representation
[Node.js] Returns the item which ranks highest by some criterion.
- Host: GitHub
- URL: https://github.com/lamansky/utmost
- Owner: lamansky
- License: mit
- Created: 2018-02-16T11:12:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-16T11:14:09.000Z (over 8 years ago)
- Last Synced: 2026-04-08T09:23:20.430Z (3 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# utmost
Returns the item which ranks highest by some criterion.
## Installation
Requires [Node.js](https://nodejs.org/) 6.0.0 or above.
```bash
npm i utmost
```
## API
The module exports a single function.
### Parameters
1. Bindable: `items` (iterable): The items from which to select the “utmost” item.
2. Object argument:
* Optional: `getValue` (function): A callback which accepts each item as its sole argument and returns the value that forms the basis of the comparison. Defaults to `x => x`.
* Optional: `isBetterThan` (function): A callback which accepts two values (`a` and `b`) and returns `true` if `a` is “better than” `b`, otherwise `false`. Defaults to `(a, b) => a > b`.
### Return Value
The “utmost” item which is “better than” the others. In the case of a tie, the item returned is the one iterated first.
## Examples
```javascript
const utmost = require('utmost')
// Without additional arguments, the module defaults to finding the greatest item.
// This means that, in this example, the highest number will be returned.
utmost([1, 3, 2]) // 3
// Returns the lowest number
utmost([1, 3, 2], {isBetterThan: (a, b) => a < b}) // 1
// Returns the longest string
utmost(['test', 'example'], {getValue: x => x.length}) // 'example'
// Returns the shortest string
utmost(['test', 'example'], {getValue: x => x.length, isBetterThan: (a, b) => a < b}) // 'test'
// Supports the bind operator
['test', 'example']::utmost({getValue: x => x.length}) // 'example'
```