ObjectMap class

Wraps a plain JavaScript object or array as a map

JavaScript usage #

Create a new ObjectMap by passing an object or array to the constructor:

// object.js

import { ObjectMap } from "@weborigami/origami";

// Wrap an object to create a map.
export default new ObjectMap({
  Alice: "Hello, Alice.",
  Bob: "Hello, Bob.",
  Carol: "Hello, Carol.",
});

This defines a map whose keys are the object’s keys, and whose values are the object’s values:

g Alice Hello, Alice. ->Alice Alice Bob Hello, Bob. ->Bob Bob Carol Hello, Carol. ->Carol Carol

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

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

API #

new ObjectMap([object])

  • object: unknown

delete(key)

  • key: any

Returns: boolean

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.

get(key)

  • key: any

Returns: any

Returns the value associated with the key, or undefined if there is none.

keys()

Returns: any

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

set(key, value)

  • key: any
  • value: any

Returns: ObjectMap

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.

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.

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.

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.

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.