umap (micro map) is a minimalist JavaScript utility designed to simplify common patterns when working with Map and WeakMap instances, specifically addressing the fact that their `set()` method returns the map itself rather than the value being set. This package provides a wrapper that modifies the `set()` behavior to return the *value* instead, allowing for more concise "get or set" operations, often seen in caching scenarios. It is currently at version 1.0.2 and appears to be a stable, focused utility with no apparent active development cycle beyond its initial stable release, as its purpose is singular and fulfilled. Its key differentiator is its tiny footprint and single-purpose design, providing a small but impactful quality-of-life improvement for developers frequently using Map or WeakMap for caching or memoization where an immediate return of the set value is desired.
npm install umapVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to wrap `Map` and `WeakMap` instances with `umap` to enable a concise get-or-set pattern, leveraging `umap`'s modified `set()` behavior.
If storing falsy values is necessary, use an explicit `if (map.has(key)) { /* get */ } else { /* set */ }` check instead of the `||` operator to correctly handle existing falsy values.Be aware that `umap(new Map()).set(key, value)` returns `value`, not the `Map` instance. Adjust your code to account for this modified return behavior or avoid `umap` if chainability of `set()` is critical.
Consider creating a local `umap.d.ts` file with basic type declarations for `UMap` to improve type inference in your TypeScript project, or rely on JSDoc type hints as demonstrated in imports.
Ensure `umap` is imported as a default export (`import umap from 'umap';` or `const umap = require('umap');`) and then called as a function, passing a `Map` or `WeakMap` instance: `const myWrappedMap = umap(new Map());`.Always initialize `umap` with a valid `Map` or `WeakMap` instance: `const cache = umap(new Map());` or `const weakCache = umap(new WeakMap());`.
No dependency data recorded yet.