Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > ProtectableMap

ProtectableMap class

The ProtectableMap provides an easy way for an API to expose a Map<K, V> property while intercepting and validating any write operations that are performed by consumers of the API.

Signature:

export declare class ProtectableMap<K, V> 

Remarks

The ProtectableMap itself is intended to be a private object that only its owner can access directly. Any operations performed directly on the ProtectableMap will bypass the hooks and any validation they perform. The public property that is exposed to API consumers should return ProtectableMap.protectedView instead.

For example, suppose you want to share your Map<string, number> data structure, but you want to enforce that the key must always be an upper case string: You could use the onSet() hook to validate the keys and throw an exception if the key is not uppercase.

Constructors

ConstructorModifiersDescription
(constructor)(parameters)Constructs a new instance of the ProtectableMap class

Properties

PropertyModifiersTypeDescription
protectedViewreadonlyMap<K, V>The owner of the protectable map should return this object via its public API.
sizereadonlynumberReturns the number of (key, value) entries in the map.

Methods

MethodModifiersDescription
clear()Removes all entries from the map. This operation does NOT invoke the ProtectableMap onClear() hook.
delete(key)Removes the specified key from the map. This operation does NOT invoke the ProtectableMap onDelete() hook.
forEach(callbackfn, thisArg)Performs an operation for each (key, value) entries in the map.
get(key)Retrieves the value for the specified key.
has(key)Returns true if the specified key belongs to the map.
set(key, value)Sets a value for the specified key. This operation does NOT invoke the ProtectableMap onSet() hook.