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

binsert

v1.0.0

Published

Uses binary search to insert a value into any sorted collection.

Downloads

8

Readme

binsert

Uses binary search to insert a value into any sorted collection.

Installation

Requires Node.js 8.3.0 or above.

npm i binsert

API

The module exports a single function.

Parameters

  1. Object argument:
    • Optional: compare (function, array, or any):
      • If a function: When passed two arguments a and b, expected to return -1 if a is less than b, 1 if a is greater than b, and 0 if they are equal.
      • If an array: An array of Map/object keys, the values of which can be used to compare two Maps/objects. The first key is checked first, and if the two values for that key are equal, the next key in the array is checked, and so on. If any given element is itself an array, it is interpreted as a nested keychain.
      • Otherwise: A single Map/object key.
      • If omitted: Will compare numbers and strings. Will coerce everything else into strings.
    • get (function): A callback that should return a value for a given index from 0 to length - 1.
    • Optional: insert (function): A callback that accepts the index at which value should be inserted. The callback is not expected to return anything. If insert is omitted, it is assumed you will take care of insertion later using the index return value.
    • length (positive integer): The length of the collection.
    • Optional: multiple (string): Only applies if unique is false or undefined. Specifies behavior in the event that more than one existing collection item is sort-equivalent with value. If set to first or last, then value will be inserted before/after the first/last sort-equivalent item, respectively. (This will slow down the insert operation.) Otherwise, value will be inserted anywhere in the range of sort-equivalent items.
    • Optional: set (function): A callback that accepts the index at which an existing value should be overwritten with value. This only applies if unique is true. The callback is not expected to return anything.
    • Optional: unique (bool): If set to true, then no item in the collection may be sort-equivalent with another; so if an existing item is sort-equivalent with value, it will either be overwritten with the set callback (if one is provided) or else nothing will happen (if set is not specified). If set to false, multiple sort-equivalent items are allowed, so value will always be inserted. Defaults to false.
    • value (any): The value to insert.

Return Value

Returns an object:

  • found (boolean): true if compare reported that the collection already contained a value with the same sort value as value; false otherwise.
  • index (positive integer): The index at which value was (or should be) inserted.

Example

const binsert = require('binsert')

const arr = ['a', 'c', 'e']
binsert({get: i => arr[i], insert: (i, v) => { arr.splice(i, 0, v) }, length: arr.length, value: 'b'}) // {found: false,  index: 1}
arr // ['a', 'b', 'c', 'e']

Related

This module is part of the “b” family of binary search modules.