Skip to content

Map

Module Collections.Maps

Maps are used to associate a value with a key. Much like a dictionary.

Immutable

Maps.immutable creates a map of Interface Maps.IMapImmutable

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js"
let m = Maps.immutable();
// Assign a key-value ('bob' being the key, 'red' the value)
m = m.set(`bob`, `red`);
// Returns true/false if key exists
m.has(`bob`);
// Get value stored under key `bob`
const value = m.get(`bob`);
// Remove data associated with key `bob`
m = m.delete(`bob`);

Mutable

Use the in in-built Javascript Map object for a mutable Map.

const m = new Map();
// Assign a key-value ('bob' being the key, 'red' the value)
m.set(`bob`, `red`);
// Returns true/false if key exists
m.has(`bob`);
// Get value stored under key `bob`
const value = m.get(`bob`);
// Remove data associated with key `bob`
m.delete(`bob`);

ExpiringMap

Maps.ExpiringMap is a map that forgets its values after some period. It can automatically cleaning up data in cases where there is not a determinate signal for when to delete it. It also keeps track of when keys are last set and accessed.

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js"
// Eg: delete data not accessed for more than one second
const map = new Maps.ExpiringMap({
autoDeleteElapsed: 1000,
autoDeletePolicy: `get`
});

Another use is its capacity-limiting, set using options . It can evict items based on time, for example:

// Create a map that only holds 10 items
// when a new item is added, the item that was set the longest
// time ago is deleted
const map = new Maps.ExpiringMap({
capacity: 10,
evictPolicy: `oldestSet`
})

Maps of

The basic semantics of a map is a single key to a single value. Sometimes however you want to keep track of multiple values per key.

Extended

There are three IMapOfMutableExtended implementations. These all have events allowing you to be notified when data changes.

ofArrayMutable stores values as an array under each key.

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js"
const map = Maps.ofArrayMutable();
// Add several values under the one key 'hello'
map.addKeyedValues(`hello`, [1,2,3,2,1]);
map.get(`hello`); // [1,2,3,2,1]

ofSetMutable stores unique values under each key

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js"
const map = Maps.ofSetMutable();
// Add several values under the one key 'hello'
map.addKeyedValues(`hello`, [1,2,3,2,1]);
map.get(`hello`); // [1,2,3]

ofCircularMutable stores a limited number of items under each key

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js"
const map = Maps.ofCircularMutable({ capacity: 3 });
map.add(`hello`, [1, 2, 3, 4, 5]);
ap.get(`hello`); // [3, 4, 5]

Basic

MapOfSimpleMutable stores multiple values per key, but is a leaner implementation, with no events notifying of changes.