IdemWeakMapIterable
is a JavaScript utility for creating a WeakMap-like structure that ensures idempotent operations, providing set, get, delete, and has functionality with additional support for weak references and handling multiple values for the same key. This library is perfect for managing collections of weakly referenced objects where changes should not duplicate or modify the state unnecessarily.
I created this library because I needed to iterate over a WeakMap that supports garbage collection. Traditional WeakMaps do not support iteration, which makes it difficult to manage collections of weakly referenced objects. IdemWeakMapIterable
addresses this limitation by providing iteration support while ensuring memory safety through weak references.
- Idempotent set operation: Setting the same value for a key will not alter its state.
- Efficient key management: Weak references to the keys are used to ensure memory safety.
- Delete operation: Safely deletes keys, ensuring that non-existent keys do not cause errors.
- Has operation: Checks if a key exists in the collection.
- Support for multiple values: Allows setting multiple values for the same key, retaining only the last value.
- Iteration support: Supports iterating over keys and values.
You can install the library using npm or yarn:
npm install idem-weak-iterable
or
yarn add idem-weak-iterable
import IdemWeakMapIterable from 'idem-weak-iterable';
// Create a new IdemWeakMapIterable instance
const map = IdemWeakMapIterable();
const key = {};
const value = 'some value';
// Set a key-value pair
map.set(key, value);
// Get the value by key
console.log(map.get(key)); // Output: 'some value'
// Setting the same value multiple times doesn't alter the state
map.set(key, 'some value');
map.set(key, 'some value'); // No change
console.log(map.get(key)); // Output: 'some value'
// Delete a key-value pair
map.delete(key);
console.log(map.get(key)); // Output: undefined
// Check if a key exists
console.log(map.has(key)); // Output: false
// Set multiple values for the same key
map.set(key, 'value1');
map.set(key, 'value2');
map.set(key, 'value3');
// Only the last value will be retained
console.log(map.get(key)); // Output: 'value3'
// Iterating over values
for (const value of map.values()) {
console.log(value); // Output: 'value3'
}
- Returns: A new
IdemWeakMapIterable
instance. - Parameters:
K
- Type for the key.V
- Type for the value.
- Sets the value for the given key.
- Returns: The instance itself for chaining.
- Gets the value for the given key, or
undefined
if the key doesn't exist.
- Deletes the key-value pair.
- Returns:
true
if the key was deleted,false
if the key wasn't found.
- Checks if the key exists in the map.
- Returns:
true
if the key exists,false
otherwise.
- Returns: An iterable of the keys in the map.
- Returns: An iterable of the values in the map.
We use Jest for testing the functionality of this library. To run the tests, follow these steps:
-
Install dependencies:
npm install
-
Run the tests:
npm test
We welcome contributions to IdemWeakMapIterable
. If you'd like to contribute:
- Fork the repository.
- Create a new branch for your feature or fix.
- Make your changes and commit them.
- Open a pull request with a description of your changes.
This project is licensed under the MIT License - see the LICENSE file for details.