Maps
Module Core.Maps
A host of functions for working with maps. See the link above for a complete overview.
Here are just a few examples.
Adding
Section titled “Adding”Core.Maps.getOrGenerate wraps a common pattern where you want to add something to a map or create it if it doesn’t exist.
import { Maps } from 'https://unpkg.com/@ixfx/core/bundle';// Wraps a map, using the provided function to generate// a value based on a key if the key does not existconst m = Maps.getOrGenerate(new Map(), (key) => { return key.toUppercase();});// The key `hello` does not exist, so the function above will// be invoked once, and value returnedconst v = await m(`hello`); // 'HELLO'
Maps.addObject adds the data from an object into a map, treating its top-level properties as keys.
import { Maps } from 'https://unpkg.com/@ixfx/core/bundle';const data = { Sally: { colour: `red` }, Bob: { colour: `pink` }};const map = new Map();Maps.addObject(map, data);
map.get(`Sally`); // { colour: `red` }
Sorting
Section titled “Sorting”import { Maps } from 'https://unpkg.com/@ixfx/core/bundle';const m = new Map();m.set(`4491`, { name: `Bob` });m.set(`2319`, { name: `Alice` });
// Compare by nameconst comparer = (a, b) => defaultComparer(a.name, b.name);
// Get sorted valuesconst s1 = Maps.sortByValue(m, comparer);
const s2 = Maps.sortByValueProperty(m, `name`);