{"id":14184,"library":"umap","title":"umap Map and WeakMap Utility","description":"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.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/WebReflection/umap","tags":["javascript","map","weakmap","get","set","shortcut"],"install":[{"cmd":"npm install umap","lang":"bash","label":"npm"},{"cmd":"yarn add umap","lang":"bash","label":"yarn"},{"cmd":"pnpm add umap","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"umap is a default export, not a named export. This is the standard ESM import for modern JavaScript environments.","wrong":"import { umap } from 'umap';","symbol":"umap","correct":"import umap from 'umap';"},{"note":"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.","wrong":"const { umap } = require('umap');","symbol":"umap","correct":"const umap = require('umap');"},{"note":"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.","symbol":"umap (Type)","correct":"/** @type {import('umap').UMap<Key, Value>} */"}],"quickstart":{"code":"import umap from 'umap';\n\n// Using umap to wrap a standard Map\nconst mapCache = umap(new Map());\nconsole.assert(\n  (mapCache.get(1) || mapCache.set(1, Math.random())) ===\n  (mapCache.get(1) || mapCache.set(1, Math.random())),\n  'Map: The set() method now returns the value, enabling concise get-or-set patterns.'\n);\n\n// Using umap to wrap a WeakMap\nconst myObject = {};\nconst weakMapCache = umap(new WeakMap());\nconsole.assert(\n  (weakMapCache.get(myObject) || weakMapCache.set(myObject, Math.random())) ===\n  (weakMapCache.get(myObject) || weakMapCache.set(myObject, Math.random())),\n  'WeakMap: Same concise pattern applies, useful for object-keyed caches.'\n);\n\nconsole.log('umap example asserts passed.');","lang":"javascript","description":"Demonstrates how to wrap `Map` and `WeakMap` instances with `umap` to enable a concise get-or-set pattern, leveraging `umap`'s modified `set()` behavior."},"warnings":[{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.2':77 'activ':89 'address':27 'allow':59 'appar':88 'appear':79 'behavior':53 'beyond':92 'cach':70,134 'common':17 'concis':62 'current':74 'cycl':91 'design':14,115 'desir':146 'develop':90,127 'differenti':106 'fact':29 'focus':84 'footprint':110 'frequent':128 'fulfil':103 'get':63,150 'immedi':139 'impact':120 'improv':125 'initi':94 'instanc':25 'instead':58 'javascript':12,147 'key':105 'life':124 'map':2,8,22,36,130,148 'memoiz':136 'method':33 'micro':7 'minimalist':11 'modifi':50 'often':67 'oper':66 'packag':45 'pattern':18 'provid':46,116 'purpos':99,114 'qualiti':122 'quality-of-lif':121 'rather':38 'releas':96 'return':34,55,140 'scenario':71 'seen':68 'set':32,43,52,65,143,151 'shortcut':152 'simplifi':16 'singl':113 'single-purpos':112 'singular':101 'small':118 'specif':26 'stabl':83,95 'tini':109 'umap':1,6 'use':129 'util':5,13,85 'valu':41,57,144 'version':76 'weakmap':4,24,132,149 'work':20 'wrapper':48","created_at":"2026-04-20T01:58:22.465042+00:00","updated_at":"2026-04-20T01:58:22.465042+00:00","problems":[{"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());`.","cause":"`umap` is imported as a named export or incorrectly invoked as a constructor (e.g., `new umap()`).","error":"TypeError: umap is not a function"},{"fix":"Always initialize `umap` with a valid `Map` or `WeakMap` instance: `const cache = umap(new Map());` or `const weakCache = umap(new WeakMap());`.","cause":"`umap` was called without a `Map` or `WeakMap` instance, or with `null`/`undefined`, leading to `get` being called on an invalid object.","error":"TypeError: Cannot read properties of undefined (reading 'get')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/WebReflection/umap","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/umap","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}