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-bin-heap

v1.1.6

Published

A flexible [binary-heap](https://en.wikipedia.org/wiki/Binary_heap). This data-structure is optimized for the retrieval of either the maximum or the minimum

Downloads

6

Readme

ts-bin-heap

A flexible binary-heap. This data-structure is optimized for the retrieval of either the maximum or the minimum

This package contains functions for creating binary-heaps conforming to the BinaryHeap<T> interface

The main methods of BinaryHeap<T> are simply push and pop. You can push items into the heap, and you can pop items from it. The item that is popped will always be either the maximum or the minimum item in the collection.

npm version Build Status

Auto-generated documentation

Usage

import { createBinaryHeap } from 'ts-bin-heap'

createBinaryHeap

The createBinaryHeap function should be all you need to get started.

Play with this example on codesandbox.io.

Say we have a collection of people:

const people: Person[] = [
  {
    name: 'will',
    priority: 10
  },
  {
    name: 'jack',
    priority: 7
  },
  {
    name: 'nicole',
    priority: 8
  },
  {
    name: 'poppy',
    priority: 44
  }
]

Now we can make a binary heap to hold these people, using the rankSelector parameter to pass in a function that calculates the order ranking of the items that are inserted.

const heap: BinaryHeap<Person> = createBinaryHeap<Person>(person => person.priority)
people.forEach(p => heap.push(p))
const lowestPriorityUser = heap.pop() // returns {name: 'jack', priority: 7}

The full definition of createBinaryHeap is as follows:

createBinaryHeap<T>(
  rankSelector: (item: T) => number,
  type: 'min' | 'max' = 'min',
  stable: boolean = true
): BinaryHeap<T>

rankSelector

Required : A method that takes an item and selects from it an order ranking.

type

One of "min" or "max". Changes the behaviour of pop to return either the minimum or the maximum item in the collection. Defaults to "min"

stable

Binary heaps are inherently unstable. This means that items that are inserted with equal order ranking will be popped in an indeterminate order. It is possible to make the heap stable by tagging each entry with an "order-of-insertion" field, and using this as a secondary comparison when selecting the maximum/minimum item. This option is switched on by default. Supplying false here will revert to the faster, unstable version.

acknowledgements

Created using the wonderful https://github.com/alexjoverm/typescript-library-starter.