New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@practicaljs/priority-queue

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@practicaljs/priority-queue

Javascript / Typescript priority queue ( max / min heap )

Source
npmnpm
Version
0.1.2
Version published
Weekly downloads
22
Maintainers
2
Weekly downloads
 
Created
Source

Table of Contents

About The Project

Javascript Priority Queue ( Min / Max Heap ).

MethodTime Complexity
PeekO(1)
EnqueueO(logn)
DequeueO(logn)
ClearO(1)

(back to top)

Getting Started

Installation

npm i @practicaljs/priority-queue

Usage

Create a new priority queue class

  const queue = new PriorityQueue<number>((a, b) => a - b);

To prioritize lower values use a-b, for higher values use b-a

example

  const nums = [3, 5, 9, 4, 1, 6, 2, 7, 8];
  const queue = new PriorityQueue<number>((a, b) => a - b);
  // [1, 2, 3....]
  const queue = new PriorityQueue<number>((a, b) => b - a);
  // [9, 8, 7....]

The same can be done for objects

  const foodLikes = [
    { name: 'sushi', rating: 4 },
    { name: 'chicken', rating: 4 },
    { name: 'beef', rating: 5 },
    { name: 'pork', rating: 1 }
  ];

  // prioritize by lower rating
  const queue = new PriorityQueue<typeof foodLikes[0]>((a, b) => a.rating - b.rating);
  // [{ name: 'pork', rating: 1 }, { name: 'sushi', rating: 4 }...]
  const queue = new PriorityQueue<typeof foodLikes[0]>((a, b) => b.rating - a.rating);
  // [ { name: 'beef', rating: 5 }, { name: 'chicken', rating: 4 }...]

You can also prioritize object by special logic, in this case the object you want to prioritize give it a lower value

  const events = [
    { name: 'Dinner', time: 19 },
    { name: 'Special - House Music', time: 22 },
    { name: 'lunch', time: 12 },
    { name: 'breakfast', time: 7 },
    { name: 'Special - Live Music', time: 23 }
  ];
  const queue = new PriorityQueue<typeof events[0]>((a, b) => {
    const aRating = a.name.startsWith('Special') ? 0 : 2;
    const bRating = b.name.startsWith('Special') ? 0 : 2;
    // if a == 0 and b == 2 it will be prioritized
    // if you want to prioritize non special events change the
    // order to bRating - aRating;
    return aRating - bRating;
  });

  //[{ name: 'Special - House Music', time: 22 }, { name: 'Special - Live Music', time: 23 }...]

You can also prioritize by secondary vaules

  const events = [
    { name: 'Dinner', time: 19 },
    { name: 'Special - House Music', time: 22 },
    { name: 'lunch', time: 12 },
    { name: 'breakfast', time: 7 },
    { name: 'Special - Live Music', time: 23 }
  ];
  const queue = new PriorityQueue<typeof events[0]>((a, b) => {
    const aRating = a.name.startsWith('Special') ? 0 : 2;
    const bRating = b.name.startsWith('Special') ? 0 : 2;
    if (aRating == bRating) {
      // Here I want earliest time first
      return a.time - b.time
    }
    return aRating - bRating;
  });

  //[{ name: 'Special - House Music', time: 22 }, { name: 'Special - Live Music', time: 23 },  { name: 'breakfast', time: 7}...]

Keywords

typescript

FAQs

Package last updated on 21 Jan 2023

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