@esfx/collections-sortedmap
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@esfx/collections-sortedmap

The @esfx/collections-sortedmap package provides SortedMap, a collection class that utilizes @esfx/collection-core and @esfx/equatable.

Overview

Installation

npm i @esfx/collections-sortedmap

Usage

NOTE: The examples below use the following definition of Person:

import { Equatable, Equaler, Comparable, Comparer } from "@esfx/equatable";

class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    toString() {
        return `${this.firstName} ${this.lastName}`;
    }

    [Equatable.equals](other) {
        return other instanceof Person
            && this.lastName === other.lastName
            && this.firstName === other.firstName;
    }

    [Equatable.hash]() {
        return Equaler.defaultEqualer.hash(this.lastName)
             ^ Equaler.defaultEqualer.hash(this.firstName);
    }

    [Comparable.compareTo](other) {
        if (!(other instanceof Person)) throw new TypeError();
        return Comparer.defaultComparer.compare(this.lastName, other.lastName)
            || Comparer.defaultComparer.compare(this.firstName, other.firstName);
    }
}

SortedMap

import { SortedMap } from "@esfx/collections-sortedmap";

// NOTE: see definition of Person above
const obj1 = new Person("Alice", "Johnson");
const obj2 = new Person("Bob", "Clark");

// ECMAScript native map iterates in insertion order
const map = new Map(); // native ECMAScript Map
set.set(obj1, "obj1");
set.set(obj2, "obj2");
[...set.keys()]; // Alice Johnson,Bob Clark

// SortedMap uses Comparable.compareTo if available
const sortedMap = new SortedMap();
sortedMap.set(obj1, "obj1");
sortedMap.set(obj2, "obj2");
[...sortedMap.keys()]; // Bob Clark,Alice Johnson

API

You can read more about the API here.

Readme

Keywords

none

Package Sidebar

Install

npm i @esfx/collections-sortedmap

Weekly Downloads

4,004

Version

1.0.2

License

Apache-2.0

Unpacked Size

44.3 kB

Total Files

8

Last publish

Collaborators

  • rbuckton