Skip to content

Maps

Module Data.Maps

A host of functions for working with maps. See the link above for a complete overview.

Here are just a few examples.

Adding

Data.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/dist/data.js';
// 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/dist/data.js';
const data = {
Sally: { colour: `red` },
Bob: { colour: `pink` }
};
const map = new Map();
Maps.addObject(map, data);
map.get(`Sally`); // { colour: `red` }

Sorting

import { Arrays } from 'https://unpkg.com/ixfx/dist/data.js';
const v = [
{ colour:`blue`, size: 10 },
{ colour:`red`, size: 5 },
{ colour:`orange`, size: 7 },
{ colour:`pink`, size: 1 }
];
// Sort objects by their 'colour' property
const byColour = Arrays.sortByProperty(v, `colour`);
// Sort objects by their 'size' property
const bySize = Arrays.sortByNumericProperty(v, `size`);