npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-binary-search

v1.1.0

Published

A TypeScript implementation of binary search algorithm for browsers and Node.js

Downloads

11

Readme

ts-binary-search

A TypeScript implementation of binary search algorithm for browsers and Node.js

A lightweight library that supports exact match, predecessor and successor queries on sorted arrays, based on the binary search algorithm.

npm install size gzip size npm

Install

npm install ts-binary-search

Usage

ES modules

import * as search from 'ts-binary-search';
// or
import {eq, ge, gte, lt, lte} from 'ts-binary-search';

CommonJS

const search = require('ts-binary-search');

HTML

<script src="https://cdn.jsdelivr.net/npm/ts-binary-search@1/dist/ts-binary-search.min.js"/>
// or
<script src="https://cdn.jsdelivr.net/npm/ts-binary-search@1/dist/ts-binary-search.es5.min.js"/>

Examples

import * as search from 'ts-binary-search';


const list = [3, 4, 7, 11, 14, 14, 14, 26, 26, 26, 26, 34];   // length: 12

search.eq(list, 1);                      // index: -1
search.eq(list, 7);                      // index: 2
search.eq(list, 14);                     // index: 4
search.eq(list, 14, {lower: 5});         // index: 5
search.eq(list, 14, {upper: 5});         // index: 4
search.eq(list, 26);                     // index: 7
search.eq(list, 26);                     // index: 7
search.eq(list, 26, {rightmost: true});  // index: 10

search.lt(list, 3);    // index: -1
search.lt(list, 7);    // index: 1
search.lte(list, 3);   // index: 0
search.lte(list, 15);  // index: 6

search.gt(list, 89);   // index: -1
search.gt(list, 4);    // index: 2
search.gte(list, 34);  // index: 11
search.gte(list, 20);  // index: 7

const vegetables100g = [
  {name: 'salad', kcal: 14},
  {name: 'tomato', kcal: 20},
  {name: 'carrot', kcal: 35, boiled: true},
  {name: 'carrot', kcal: 39},
  {name: 'garlic', kcal: 143},
  {name: 'lentils', kcal: 331},
];

const cmp = (item, target) => item.kcal - target;

search.gte(vegetables100g, 100, {compareFn: cmp});  // index: 4
search.gte(vegetables100g, 200, {compareFn: cmp});  // index: 5

search.lte(vegetables100g, 50, {compareFn: cmp});  // index: 3
search.lte(vegetables100g, 10, {compareFn: cmp});  // index: -1

More examples here.

API

search.eq<T,R>(list: T[], target: R, options?: Options): number

Exact match query
This returns the index of the matching item, or -1 if not present.

search.gt<T,R>(list: T[], target: R, options?: Options): number

search.gte<T,R>(list: T[], target: R, options?: Options): number

Successor queries
These return the index of the smallest item which is greater than (or equal to) target, or -1.
In case of multiple matching items, they always return the leftmost one.

search.lt<T,R>(list: T[], target: R, options?: Options ): number

search.lte<T,R>(list: T[], target: R, options?: Options): number

Predecessor queries
These return the index of the greatest item which is lower than (or equal to) target, or -1.
In case of multiple matching items, they always return the rightmost one.

Options

compareFn: (item: T, target: R) => number

A comparison function along the lines of Array.prototype.sort() method.
It could be the same function you adopted for sorting the array.

| compareFn(item, target) return value | | |:-------------------------------------|:----------------| | > 0 | item > target | | < 0 | item < target | | === 0 | item === target |

lower: number     // default: 0

Lower bound (inclusive): do not search for items with index lower than this.

upper: number     // default: list.length - 1

Upper bound (inclusive): do not search for items with index greater than this.

rightmost: boolean  // default: false

(used only by search.eq)
In case of multiple matching items, this allows you to take the rightmost or the leftmost one.

License

MIT License
© 2023 Diego Bogni