FunctionMap class

Wraps a function and a domain as a map

JavaScript usage #

// function.js

import { FunctionMap } from "@weborigami/async-tree";

// Wrap an object to create a map.
export default new FunctionMap(
  (key) => `Hello, ${key}.`,
  ["Alice", "Bob", "Carol"]
);

The ori tool will display the contents of the resulting FunctionMap.

$ ori function.js/
Alice: Hello, Alice.
Bob: Hello, Bob.
Carol: Hello, Carol.

A rough implementation of the core interface methods in FunctionMap is:

// Rough implementation
class FunctionMap {
  constructor(fn, domain = []) {
    this.fn = fn;
    this.domain = domain;
  }

  // Return the value for a given key.
  async get(key) {
    return this.fn(key);
  }

  // Return the function's domain as its keys.
  async keys() {
    return this.domain;
  }
}

Unlike classes like ObjectMap, an FunctionMap can often accept keys which it does not make public in its keys iterator. The sample FunctionMap defined above exposes only three keys (“Alice”, “Bob”, “Carol”), but will actually accept any key.

$ ori keys function.js/
- Alice
- Bob
- Carol
$ ori function.js/David
Hello, David.

API #

new FunctionMap(fn, [domain])

  • fn: any
  • domain: unknown

get(key)

  • key: any

Returns: any

Return the application of the function to the given key.

keys()

Returns: any

Returns a new Iterator object that contains the keys for each element in the map in insertion order.

clear()

Returns: void

Removes all key/value entries from the map.

Unlike the standard Map.prototype.clear(), this method invokes an overridden keys() and delete() to ensure proper behavior in subclasses.

If the readOnly property is true, calling this method throws a TypeError.

delete(key)

  • key: any

Returns: any

Removes the entry for the given key, return true if an entry was removed and false if there was no entry for the key.

If the readOnly property is true, calling this method throws a TypeError.

entries()

Returns: MapIterator<[any, any]>

Returns a new Iterator object that contains a two-member array of [key, value] for each element in the map in insertion order.

Unlike the standard Map.prototype.clear(), this method invokes an overridden keys() and get() to ensure proper behavior in subclasses.

forEach(callback, [thisArg])

  • callback: (value: any, key: any, thisArg: any) => void
  • thisArg: any

Returns: void

Calls callback once for each key/value pair in the map, in insertion order.

Unlike the standard Map.prototype.forEach(), this method invokes an overridden entries() to ensure proper behavior in subclasses.

has(key)

  • key: any

Returns: any

Returns true if the given key appears in the set returned by keys().

It doesn’t matter whether the value returned by get() is defined or not.

If the requested key has a trailing slash but has no associated value, but the alternate form with a slash does appear, this returns true.

parent

Type: SyncMap

The parent of this node in a tree.

readOnly

Type: boolean

True if the object is read-only. This will be true if the get() method has been overridden but set() and delete() have not.

set(key, value)

  • key: any
  • value: any

Returns: any

Adds a new entry with a specified key and value to this Map, or updates an existing entry if the key already exists.

If the readOnly property is true, calling this method throws a TypeError.

size

Type: any

Returns the number of keys in the map.

The size property invokes an overridden keys() to ensure proper behavior in subclasses. Because a subclass may not enforce a direct correspondence between keys() and get(), the size may not reflect the number of values that can be retrieved.

values()

Returns: MapIterator<[any]>

Returns a new Iterator object that contains the values for each element in the map in insertion order.