Socket
Book a DemoInstallSign in
Socket

binary-search-party

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-search-party

Asynchronous binary search

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

binary-search-party

Asynchronous or Synchronous binary search. This can be useful for seraching through really useless databases like a pipermail archive. If you essentially just have a key value store and a list of keys that are known to be in order, you can efficiently search for a specific value using this module.

Build Status Dependency Status NPM version

Installation

npm install binary-search-party

API

Synchronous

var search = require('binary-search-party')

var haystack = [1, 2, 3, 4, 5]
var needle = 4

var index = search(haystack, function (val) {
  if (val === needle) return 0
  if (val < needle) return -1
  if (val > needle) return +1
})
asssert(haystack[index] === needle)

Asynchronous

var search = require('binary-search-party')

var haystack = [1, 2, 3, 4, 5]
var needle = 4

search(haystack, function (val, callback) {
  if (val === needle) return callback(null, 0)
  if (val < needle) return callback(null, -1)
  if (val > needle) return callback(null, +1)
}, function (err, index) {
  if (err) throw err
  asssert(haystack[index] === needle)
})

Promised

var search = require('binary-search-party')

var haystack = [1, 2, 3, 4, 5]
var needle = 4

search(haystack, function (val) {
  if (val === needle) return Promise.from(null, 0)
  if (val < needle) return Promise.from(null, -1)
  if (val > needle) return Promise.from(null, +1)
}).done(function (index) {
  asssert(haystack[index] === needle)
})

N.B. You will get back the type of promise returned by calling the comparison function. This makes it easy to use your own promise library :)

N.B. If you pass an empty array, the comparison function can never be called so the result will be the literal number -1. If the array might be empty and you rely on the result being a promise you should assimilate the result into being a promise using something like Promise.from(search(arr, comparison))

License

MIT

FAQs

Package last updated on 22 Aug 2013

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