Comparing version 1.0.1 to 2.0.0
45
index.js
var abs = Math.abs | ||
function closest (target, numbers) { | ||
var closest = Infinity | ||
var difference = 0 | ||
var winner = null | ||
module.exports = closest | ||
numbers = unique(numbers.sort(function (a, b) { | ||
return total(a) - total(b) | ||
})) | ||
for (var i = 0, l = numbers.length; i < l; i++) { | ||
difference = abs(total(target) - total(numbers[i])) | ||
if (difference >= closest) break | ||
closest = difference | ||
winner = numbers[i] | ||
function closest (n, arr, rndx) { | ||
var i, ndx, diff, best = Infinity | ||
var low = 0, high = arr.length - 1 | ||
while (low <= high) { | ||
i = low + (high - low >> 1) | ||
diff = arr[i] - n | ||
diff < 0 ? low = i + 1 : | ||
diff > 0 ? high = i - 1 : void 0 | ||
diff = abs(diff) | ||
if (diff < best) best = diff, ndx = i | ||
if (arr[i] === n) break | ||
} | ||
return winner | ||
return rndx ? ndx : arr[ndx] | ||
} | ||
function unique (target) { | ||
return target.filter(function (v, i, arr) { | ||
return arr.lastIndexOf(v) === i | ||
}) | ||
} | ||
function total (target) { | ||
if (!Array.isArray(target)) { | ||
return target | ||
} | ||
return target.reduce(function(a, b) { | ||
return a + b | ||
}, 0) | ||
} | ||
module.exports = closest |
{ | ||
"name": "closest-to", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "A function that, when given a target number and an array of numbers, will return the array value closest to the target.", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"benchmark": "~2.1.3", | ||
"closest-to": "~1.0.1", | ||
"tape": "~2.0.0" | ||
}, | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
"test": "tape test.js" | ||
}, | ||
@@ -16,0 +15,0 @@ "testling": { |
# closest-to | ||
closest-to is a function that, when given a target number and an array of numbers, will return the array value closest to the target. | ||
closest-to finds the closest value to a given target in a sorted array of numbers | ||
[![Build status](https://travis-ci.org/michaelrhodes/closest-to.png?branch=master)](https://travis-ci.org/michaelrhodes/closest-to) | ||
[![Build status](https://travis-ci.org/michaelrhodes/closest-to.svg?branch=master)](https://travis-ci.org/michaelrhodes/closest-to) | ||
[![Browser support](https://ci.testling.com/michaelrhodes/closest-to.png)](https://ci.testling.com/michaelrhodes/closest-to) | ||
<small>As of 1.0.0, old browsers will require a polyfill for [Array.prototype.reduce](http://kangax.github.io/es5-compat-table/#Array.prototype.reduce) and [Array.isArray](http://kangax.github.io/es5-compat-table/#Array.isArray).</small> | ||
## Install | ||
``` | ||
```sh | ||
npm install closest-to | ||
``` | ||
### Example | ||
``` js | ||
var closest = require('closest-to') | ||
### Usage | ||
```js | ||
require('closest-to')(needle, haystack, [returnIndex]) | ||
require('closest-to/nd')(needle, haystack, [returnIndex]) | ||
``` | ||
```js | ||
closest(10, [1, 3, 5, 7, 9]) | ||
> 9 | ||
closest(10, [1, 3, 5, 7, 9], true) | ||
> 4 | ||
``` | ||
```js | ||
ndclosest([1, 2], [[1, 1], [2, 3], [3, 4]]) | ||
> [1, 1] | ||
closest(10, [3, 7, 1, 9, 5]) | ||
// => 9 | ||
closest(-1.25, [2, 0, -1.5, -0.75]) | ||
// => -1.5 | ||
closest(5, [4, 6]) | ||
// => 4 | ||
// Works on multidimensional arrays as well | ||
closest([1, 2], [[3, 4], [1, 1], [2, 3]]) | ||
// => [1, 1] | ||
ndclosest([1, 2], [[1, 1], [2, 3], [3, 4]], true) | ||
> 0 | ||
``` | ||
## API | ||
``` | ||
closest-to( | ||
target (number || array[number, …]) : | ||
any number | ||
numbers (array[number || array[number, …]) : | ||
the collection of numbers you | ||
want to have searched | ||
) | ||
### Benchmarks | ||
``` | ||
closest-to@1.0.1 x 116,156 ops/sec ±1.02% (85 runs sampled) | ||
closest-to@2.0.0 x 860,497 ops/sec ±1.09% (83 runs sampled) | ||
``` | ||
#### Note | ||
As you may have noticed in the example, if two numbers are equally close to the target, the first (lowest) number is returned. It would be trivial to instead return an array, but I don’t want to add code if it’s not necessary. If this behaviour is a problem for you, please post an issue. | ||
### License | ||
[MIT](http://opensource.org/licenses/MIT) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4395
8
85
3
40