Registry / serialization / umap
library1.0.2jsnpmunverified

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 umap
INSTALL
IMPORT
SIG · UMAP
U
umap
serializationjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

umap
✓ import umap from 'umap';
✗ import { umap } from 'umap';
umap is a default export, not a named export. This is the standard ESM import for modern JavaScript environments.
umap
✓ const umap = require('umap');
✗ const { umap } = require('umap');
This is the correct CommonJS import pattern. While ESM is generally preferred in newer projects, CJS is still widely used, especially in Node.js scripts.
umap (Type)
✓ /** @type {import('umap').UMap<Key, Value>} */
This package does not ship its own TypeScript types directly. For TypeScript projects, it relies on implicit `any` or community-provided types if available. JSDoc type hints can be used for better inference.

Demonstrates how to wrap `Map` and `WeakMap` instances with `umap` to enable a concise get-or-set pattern, leveraging `umap`'s modified `set()` behavior.

import umap from 'umap'; // Using umap to wrap a standard Map const mapCache = umap(new Map()); console.assert( (mapCache.get(1) || mapCache.set(1, Math.random())) === (mapCache.get(1) || mapCache.set(1, Math.random())), 'Map: The set() method now returns the value, enabling concise get-or-set patterns.' ); // Using umap to wrap a WeakMap const myObject = {}; const weakMapCache = umap(new WeakMap()); console.assert( (weakMapCache.get(myObject) || weakMapCache.set(myObject, Math.random())) === (weakMapCache.get(myObject) || weakMapCache.set(myObject, Math.random())), 'WeakMap: Same concise pattern applies, useful for object-keyed caches.' ); console.log('umap example asserts passed.');
Debug
Known issues
gotchaWhen using `umap` with the `||` (logical OR) operator for conditional setting, ensure that you do not store `falsy` values (e.g., `0`, `null`, `false`, `undefined`, `''`) in the map. If a key's value is falsy, the `||` operator will evaluate to the right-hand side, leading to an unintended re-setting of the value, even if it already exists.
fix
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.
affects: >=1.0.0
gotchaThe primary function of `umap` is to change the return value of `Map.prototype.set()` and `WeakMap.prototype.set()`. Instead of returning the map instance itself (allowing method chaining), `umap`'s wrapper returns the *value* that was just set. This alters the standard behavior and can break existing code that relies on `set()`'s chainability.
fix
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.
affects: >=1.0.0
gotchaWhile `umap` is a small, stable utility, it currently does not provide official TypeScript type definitions within the package. TypeScript users may encounter `any` types or need to create custom declaration files to get type-safety benefits.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: umap is not a function
`umap` is imported as a named export or incorrectly invoked as a constructor (e.g., `new umap()`).
fix
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());`.
TypeError: Cannot read properties of undefined (reading 'get')
`umap` was called without a `Map` or `WeakMap` instance, or with `null`/`undefined`, leading to `get` being called on an invalid object.
fix
Always initialize `umap` with a valid `Map` or `WeakMap` instance: `const cache = umap(new Map());` or `const weakCache = umap(new WeakMap());`.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
27 hits · last 30 days
node
24
OpenAI (training)
1
Resources
umap — npm install umap · libregistry