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

binary-sorted-array

v1.0.4

Published

Binary sorted array. Implements array initialization, insertion, finding index of element, item removal, clearing the array.

Downloads

372

Readme

Binary Sorted Array

An array sorted using binary search algorithm. Binary search is used when calling indexOf or insert (to find position in the array where the item should be inserted).

Build Status

Installation

There are 2 options:

  1. Download lib/index.js file
  2. use npm npm i --save binary-sorted-array

Usage

Basic usage example

import BinarySortedArray from 'binary-sorted-array'
let array = new BinarySortedArray([1, 5, 6, 3, 5, 1])
array.insert(0.99)
array.remove(3)
array.indexOf(6)
let sortedArray = array.getArray()
array.clear()

Example with objects and custom compare function

import BinarySortedArray from 'binary-sorted-array'
let compare = (a, b) => {
    if (a.start === b.start) return 0
    return a.start < b.start ? -1 : 1
}
let array = new BinarySortedArray([
    { id: 1, start: 1.123, title: 'some item'},
    { id: 2, start: 5, title: 'another item'},
    { id: 3, start: 6.89, title: 'yet another item'},
    { id: 4, start: 3.99 },
    { id: 5, start: 5 },
    { id: 6, start: 1.121 },
], comparator)
array.insert({ start: 0.99 })
array.remove({ id: 4, start: 3.99 })
array.indexOf({ start: 5 })
array.slice(2, 4)
let sortedArray = array.getArray()
array.clear()

Documentation

new BinarySortedArray(arr: Array, compare: function)

Constructor takes 2 parameters and if given array is not empty sorts the array during initialization.

.getArray()

Returns copy of sorted array

.insert(item)

Adds a new item to the array in the proper position

.indexOf(item, returnPossiblePlace)

Returns index of the item or -1 if it doesn't exist in the array. If returnPossiblePlace is set to true instead of -1 it returns a place in the array where the item could be placed (watch out - you don't know if the item is in the array in this case!)

.slice(start, end)

Performs slice operation on internal array and returns the result

.remove(item)

Removes first found item for which compare function (see constructor) returns 0

.clear()

Clears the array