Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

closest-to

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

closest-to

A function that, when given a target number and an array of numbers, will return the array value closest to the target.

  • 1.0.1
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Build status

Browser support

As of 1.0.0, old browsers will require a polyfill for Array.prototype.reduce and Array.isArray.

Install

npm install closest-to

Example

var closest = require('closest-to')

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] 

API

closest-to(

  target (number || array[number, …]) :
    any number

  numbers (array[number || array[number, …]) :
    the collection of numbers you 
    want to have searched 

)
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

Keywords

FAQs

Package last updated on 20 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc