npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

idem-weak-iterable

v0.1.7

Published

An idempotent WeakMap with iterable capabilities.

Readme

IdemWeakMapIterable

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.

Motivation

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.

Features

  • 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.

Installation

You can install the library using npm or yarn:

npm install idem-weak-iterable

or

yarn add idem-weak-iterable

Usage

Create an IdemWeakMapIterable instance

import IdemWeakMapIterable from 'idem-weak-iterable';

// Create a new IdemWeakMapIterable instance
const map = IdemWeakMapIterable();

Set and get operations

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'

Idempotent set operation

// 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 operation

// Delete a key-value pair
map.delete(key);
console.log(map.get(key)); // Output: undefined

Has operation

// Check if a key exists
console.log(map.has(key)); // Output: false

Iteration

// 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'
}

API

IdemWeakMapIterable<K, V>()

  • Returns: A new IdemWeakMapIterable instance.
  • Parameters:
    • K - Type for the key.
    • V - Type for the value.

map.set(key: K, value: V)

  • Sets the value for the given key.
  • Returns: The instance itself for chaining.

map.get(key: K): V | undefined

  • Gets the value for the given key, or undefined if the key doesn't exist.

map.delete(key: K): boolean

  • Deletes the key-value pair.
  • Returns: true if the key was deleted, false if the key wasn't found.

map.has(key: K): boolean

  • Checks if the key exists in the map.
  • Returns: true if the key exists, false otherwise.

map.keys()

  • Returns: An iterable of the keys in the map.

map.values()

  • Returns: An iterable of the values in the map.

Testing

We use Jest for testing the functionality of this library. To run the tests, follow these steps:

  1. Install dependencies:

    npm install
  2. Run the tests:

    npm test

Contributing

We welcome contributions to IdemWeakMapIterable. If you'd like to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or fix.
  3. Make your changes and commit them.
  4. Open a pull request with a description of your changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.