An extension of the Map class with more Array-like features.
There ain't no installation. It's a Deno module.
import { BetterMap } from "https://deno.land/x/bettermap/mod.ts";
const b = new BetterMap<string, unknown>();
class BetterMap<K, V> extends Map<K, V>
Create a new BetterMap
Param: name: string - A friendly name for the BetterMap
Returns: Array of items in the map
Convert the map into an array of values / keys
b.array();
// Array of values
Return the nth element of the map.
Param: pos: number - Position to get data.
Returns: {V} - Item at specified index.
b.at(-9);
{ name: "Spectrier", tier: "normal", id: 897 }
Array#every but for a Map
Param: fn - Function to run on every element.
Returns: {boolean} - True or false
b.every((x) => x.tier === "mythic");
false;
Filter elements that do not pass the condition.
Param: fn - Function to be passed.
Returns: {BetterMap} - BetterMap with elements that passed.
b.filter((x) => x.tier === "mythic");
Map {
"Mew" => { name: "Mew", tier: "mythic", id: 151 },
"Celebi" => { name: "Celebi", tier: "mythic", id: 251 },
"Jirachi" => { name: "Jirachi", tier: "mythic", id: 385 },
"Deoxys" => { name: "Deoxys", tier: "mythic", id: 386 },
"Phione" => { name: "Phione", tier: "mythic", id: 489 },
"Manaphy" => { name: "Manaphy", tier: "mythic", id: 490 },
"Darkrai" => { name: "Darkrai", tier: "mythic", id: 491 },
"Shaymin" => { name: "Shaymin", tier: "mythic", id: 492 },
"Arceus" => { name: "Arceus", tier: "mythic", id: 493 },
"Victini" => { name: "Victini", tier: "mythic", id: 494 },
"Keldeo" => { name: "Keldeo", tier: "mythic", id: 647 },
"Meloetta" => { name: "Meloetta", tier: "mythic", id: 648 },
"Genesect" => { name: "Genesect", tier: "mythic", id: 649 },
"Diancie" => { name: "Diancie", tier: "mythic", id: 719 },
"Hoopa" => { name: "Hoopa", tier: "mythic", id: 720 },
"Volcanion" => { name: "Volcanion", tier: "mythic", id: 721 },
"Magearna" => { name: "Magearna", tier: "mythic", id: 801 },
"Marshadow" => { name: "Marshadow", tier: "mythic", id: 802 },
"Zeraora" => { name: "Zeraora", tier: "mythic", id: 807 },
"Meltan" => { name: "Meltan", tier: "mythic", id: 808 },
"Melmetal" => { name: "Melmetal", tier: "mythic", id: 809 },
"Zarude" => { name: "Zarude", tier: "mythic", id: 893 }
}
Param: fn - Function to be passed.
Returns: A value from the map. If none found, returns undefined.
b.find((x) => x.tier === "mythic");
{ name: "Mew", tier: "mythic", id: 151 }
Param: fn - Function to be passed.
Returns: A key from the map. If none found, returns undefined.
b.findKey((x) => x.tier === "mythic");
"Mew";
Get the first element(s) from the map.
Param: n: number - Number of elements to fetch.
Returns: The first element / undefined.
b.first();
{ name: "Bulbasaur", tier: "normal", id: 1 }
Get the first (n) element's key from the map.
Param: n: number - Number of elements to fetch.
Returns: The first element's key / undefined.
b.first();
"Bulbasaur";
Convert the key-value pairs into key-value pairs... I mean a JavaScript object.
Returns: {Record<string, V>} - keyAt(pos: number): K | undefined` Return the nth key of the map.
Param: pos: number - Position to get data.
Returns: {K} - Key at specified index.
Get last value(s) in the Map.
b.last();
{ name: "Enamorus", tier: "normal", id: 905 }
Get last key(s) in the Map.
b.first();
"Enamorus";
Map the Map into an Array.
Param: fn - Function for mapping.
Returns: {T[]} - Array.
b.map((x) => x.id);
Get a random element from the BetterMap.
Returns: {boolean} - True or false
b.random();
b.random(5);
Get a random key from the BetterMap.
Returns: {boolean} - True or false
Reduce data in the map.
Param: fn - Reducer function.
Returns: {T} - Reduced data.
b.randomKey();
b.randomKey(5);
Check if at least one entry from the Map passes the function.
Param: fn - Function to run on every element.
Returns: {boolean} - True or false
b.some((x) => x.tier === "mythic");
true;
Sort elements in the better map.
Param: fn - Function to use for sorting.
Returns: {BetterMap<K, V>} - sorted BetterMap.
b.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
Map {
"Abomasnow" => { name: "Abomasnow", tier: "normal", id: 460 },
"Abra" => { name: "Abra", tier: "normal", id: 63 },
"Absol" => { name: "Absol", tier: "normal", id: 359 },
"Accelgor" => { name: "Accelgor", tier: "normal", id: 617 },
"Aegislash" => { name: "Aegislash", tier: "normal", id: 681 },
"Aerodactyl" => { name: "Aerodactyl", tier: "normal", id: 142 },
"Aggron" => { name: "Aggron", tier: "normal", id: 306 },
"Aipom" => { name: "Aipom", tier: "normal", id: 190 },
"Alakazam" => { name: "Alakazam", tier: "normal", id: 65 },
"Alcremie" => { name: "Alcremie", tier: "normal", id: 869 },
"Alomomola" => { name: "Alomomola", tier: "normal", id: 594 },
...
}
Stringify the map.
Duplicate of BetterMap#json
Returns: {Record<string, V>} -
Transform values of the map.
Similar to map()
but returns a BetterMap instead.
Param: fn - Function for mapping.
Returns: BetterMap<K, T>
Create a new map from an existing Map or an array of key-value pairs
Param: data - Existing Map / Array of Key-Value pairs.
Returns: {BetterMap<K1, V1>}
const obj = {
"Mew": { name: "Mew", tier: "mythic", id: 151 },
"Celebi": { name: "Celebi", tier: "mythic", id: 251 },
"Jirachi": { name: "Jirachi", tier: "mythic", id: 385 },
};
BetterMap.from(Object.entries(obj))
Map {
"Mew" => { name: "Mew", tier: "mythic", id: 151 },
"Celebi" => { name: "Celebi", tier: "mythic", id: 251 },
"Jirachi" => { name: "Jirachi", tier: "mythic", id: 385 },
}
Do open a new issue or pr regarding bugs or improvements.