Skip to content

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.

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 exist
const 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 returned
const 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` }
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 name
const comparer = (a, b) => defaultComparer(a.name, b.name);
// Get sorted values
const s1 = Maps.sortByValue(m, comparer);
const s2 = Maps.sortByValueProperty(m, `name`);