Skip to main content
Module

x/bettermap/mod.ts>BetterMap

An extension of the Map class with more Array-like features.
Latest
class BetterMap
extends Map<K, V>
import { BetterMap } from "https://deno.land/x/bettermap@v1.3.0/mod.ts";

Constructors

new
BetterMap(name?: string)

Create a new BetterMap

const map1 = new BetterMap();
const map2 = new BetterMap<string, number>();
const map3 = new BetterMap<string, number>("People");

Properties

name: string

Methods

array(): V[]

Convert the map into an array of values / keys

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.array());
array(keys: boolean): K[]
at(pos: number): V | undefined

Return the nth element of the map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.at(-1)); // 28
combine(...maps: Map<K, V>[]): BetterMap<K, V>

Add all non-similar keys from another Map to this Map.

import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
  type: "json",
};

interface PokemonData {
  name: string;
  id: number;
  tier: string;
}

const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
  b.set(`${pokemon.name}`, pokemon);
}

const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
  c.set(`${pokemon.name}`, pokemon);
}

console.log(c.size)
c.combine(b)
console.log(c.size)
every(fn: (v: V, k: K) => boolean): boolean

Array#every but for a Map

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.every(v => v > 10)); // false
console.log(map.every((_v, k) => k.startsWith("Dora"))); // true
filter(fn: (v: V, k: K) => boolean): BetterMap<K, V>

Array#filter but for a Map

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.filter(v => v > 10));
console.log(map.filter((_v, k) => k.startsWith("Dora")))
find(fn: (v: V, k: K) => boolean): V | undefined

Find an element from the Map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.find(v => v > 10)); // 28
findKey(fn: (v: V, k: K) => boolean): K | undefined

Find a key from the Map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.findKey(v => v > 10)); // Dora
first(): V | undefined

Get the first element(s) from the map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.first()); // 10
first(n: number): V[]
firstKey(): K | undefined

Get the first (n) element's key from the map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.firstKey()); // Doraemon
firstKey(n: number): K[]
json(): Record<string, V>

Convert the key-value pairs into key-value pairs... I mean a JavaScript object.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.json());
// {Doraemon: 10, Dora: 28}
keyAt(pos: number): K | undefined

Return the nth key of the map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.keyAt(-1)); // Dora
last(): V | undefined

Get last value(s) in the Map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.last()); // 28
last(n: number): V[]
lastKey(): K | undefined

Get last key(s) in the Map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.lastKey()); // Dora
lastKey(n: number): K[]
map<T>(fn: (v: V, k: K) => T): T[] | []

Map the Map into an Array.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.map(v => v - 10)); // [0, 18]
console.log(map.every((v, k) => k+v))); // ["Doraemon10", "Dora28"]
random(): V | undefined

Get a random element from the BetterMap.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.random());
random(count: number): V[]
randomKey(): K | undefined

Get a random key from the BetterMap.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.randomKey());
randomKey(count: number): K[]
reduce<T>(fn: (acc: T, val: [K, V]) => T, first: T): T

Reduce data in the map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.reduce((acc, val) => acc + (val[1] > 10 ? "a" : "b") + val[0], ""));
// bDoraemonaDora
some(fn: (val: V, key: K) => boolean): boolean

Check if at least one entry from the Map passes the function.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.some(v => v > 10)); // True
console.log(map.some(_v, k) => k.startsWith("Doras"))); // False
sort(fn?: (
v1: V,
v2: V,
k1: K,
k2: K,
) => number
): BetterMap<K, V>

Sort elements in the better map.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
map.set("Pikachu", 7);
console.log(map.sort((v1, v2) => v1 - v2)); // Sorts in the order Pikachu -> Doraemon -> Dora.
split(fn: (v: V, k: K) => boolean): [BetterMap<K, V>, BetterMap<K, V>]

Split the Map into two Maps using a filtering function.

const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.split(v => v > 10));
// [{Doraemon => 28}, {Doraemon => 10}]
toJSON(): Record<string, V>

Duplicate of BetterMap#json

toString(): string
transform<T>(fn: (v: V, k: K) => T): BetterMap<K, T>

Transform values of the map. Similar to map() but returns a BetterMap instead. Check map() for usage.

Static Methods

from<K1, V1>(data: Map<K1, V1> | [K1, V1][]): BetterMap<K1, V1>

Create a new map from an existing Map or an array of key-value pairs.

import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
  type: "json",
};

interface PokemonData {
  name: string;
  id: number;
  tier: string;
}

const b = BetterMap.from(Pokemon.map(x => [x.name, x]))
console.log(b)
intersect<K1, V1>(...maps: Map<K1, V1>[]): BetterMap<K1, V1>

Return a Map with elements common to all maps supplied in the parameters.

import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
  type: "json",
};

interface PokemonData {
  name: string;
  id: number;
  tier: string;
}

const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
  b.set(`${pokemon.name}`, pokemon);
}

const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
  c.set(`${pokemon.name}`, pokemon);
}

console.log(c.size, b.size)
const a = BetterMap.intersect(c, b)
console.log(a.size)
union<K1, V1>(...maps: Map<K1, V1>[]): BetterMap<K1, V1>

Return the union of two maps. Maps are united by key and not by value.

import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
  type: "json",
};

interface PokemonData {
  name: string;
  id: number;
  tier: string;
}

const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
  b.set(`${pokemon.name}`, pokemon);
}

const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
  c.set(`${pokemon.name}`, pokemon);
}

console.log(c.size, b.size)
const a = BetterMap.union(c, b)
console.log(a.size)