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-treemap

v1.1.0

Published

a TypeScript implementation of TreeMap

Downloads

2,900

Readme

🇯🇵 日本語はここ

ts-treemap

a TypeScript implementation of TreeMap

CI codecov

You can use some features of TreeMap in Java with TypeScript.

Installation

npm i ts-treemap --save

Usage

Create New TreeMap and Add Entries

import TreeMap from 'ts-treemap'

const treeMap = new TreeMap<number, string>()

// add new entry
treeMap.set(10, 'abc')
treeMap.set(5, 'def')
treeMap.set(0, 'ghi')

// you can also create new TreeMap with iterable
const treeMap2 = new TreeMap<number, string>([[1, 'foo'], [2, 'bar']])

Get Entry from TreeMap

// get first entry
treeMap.firstEntry() // [0, 'ghi']

// get entry nearest to key '7'
treeMap.floorEntry(7) // [5, 'def']
treeMap.ceilingEntry(7) // [10, 'abc']

treeMap.higherEntry(5) // [10, 'abc']
treeMap.lowerEntry(5) // [0, 'ghi']

Duplicate a Map

// copy map
const treeMap = new TreeMap<number, string>()
const copiedMap = treeMap.duplicate()

// copy as Map object
const map: Map<number, string> = treeMap.toMap()

// create TreeMap from Map
const treeMap2 = TreeMap.from(map)

Note

In order to sort keys, you need to define a comparison function, and TreeMap has an internal comparison function that automatically sorts keys each time you add them.

The ES2015 Map uses the "Same-value-zero" algorithm to determine if there are duplicate keys when an entry is added. The algorithm uses "===" to determine equivalence when comparing objects (but +0 and -0 are considered equal). This means that when you add multiple entries with the same key, the duplicate check will not work correctly if the type of key is object (such as Date).

To avoid this problem, TreeMap does not use that algorithm when adding keys, but uses a comparison function. If the return value of the comparison function is 0, the key is considered to be a duplicate.

This comparison function conforms to the compare function used in Array.prototype.sort().

You don’t have to define the compare function if the type of the key is number, string or Date.

If you want to use other types as keys, you can use one of the following methods to generate a TreeMap.

Method 1: Pass the comparison function to the constructor to create a map

import TreeMap from 'ts-treemap'

interface IKeyObject {
  value: number
}

const objectMap = new TreeMap<IKeyObject, string>((a, b) => a.value - b.value)
objectMap.set({ value: 1 }, 'foo') // OK

Method 2: Use the class which has the comparison function compare() as a key

import TreeMap, { Comparable } from 'ts-treemap'

class ExampleObject implements Comparable<ExampleObject> {
  value: number

  constructor(value: number) {
    this.value = value
  }

  compare(object: ExampleObject) {
    return this.value - object.value
  }
}

const map = new TreeMap<ExampleObject, string>()
map.set(new ExampleObject(1), 'a') // OK

(If both are satisfied, method 1 takes precedence.)

If TreeMap is created without passing parameters in the above case, Error will be thrown when the first entry is added.

✅ Do:

import TreeMap from 'ts-treemap'
import Day from 'dayjs'

const numberMap = new TreeMap<number, string>()
numberMap.set(1, 'foo') // OK

const stringMap = new TreeMap<string, string>()
stringMap.set('1', 'foo') // OK

const dateMap = new TreeMap<Date, string>()
dateMap.set(new Date('2019-01-01'), 'foo') // OK

// compareFn is defined
const objectMap = new TreeMap<Day.Dayjs, string>((a, b) => a.unix() - b.unix())
objectMap.set(Day('2019-01-01'), 'foo') // OK

const objectMap2 = new TreeMap<Day.Dayjs, string>([[Day('2019-01-01'), 'foo']], (a, b) => a.unix() - b.unix())

🛑 Don’t:

import TreeMap from 'ts-treemap'
import Day from 'dayjs'

// compareFn is not defined
const errMap = new TreeMap<Day.Dayjs, string>()
errMap.set(Day('2019-01-01'), 'foo') // throws error