Skip to content

memory - fix: keep falsy values in has(), hasMany(), take(), and takeMany() - #1767

Open
bardrop wants to merge 2 commits into
jaredwray:mainfrom
bardrop-oss:fix-memory-has-take-falsy
Open

bardrop wants to merge 2 commits into
jaredwray:mainfrom
bardrop-oss:fix-memory-has-take-falsy

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Please check if the PR fulfills these requirements

  • Followed the Contributing guidelines and Code of Conduct
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Bug fix. Fixes #1766.

Problem

CacheableMemory treats a stored falsy value as a missing key in four methods:

const cache = new CacheableMemory();
cache.set("k", 0);
cache.get("k");       // 0
cache.has("k");       // false, expected true
cache.hasMany(["k"]); // [false], expected [true]
cache.take("k");      // undefined, expected 0
cache.get("k");       // 0, expected undefined

The result is the same for false, "", and null. has() and hasMany() use Boolean(item) on the value. take() uses if (!item), so it also does not delete the entry. takeMany() calls take().

The KeyvCacheableMemory adapter has the same problem in get(). It uses if (result), so it returns undefined for a stored falsy value. After the has() fix, the adapter has() returns true for the same key.

Fix

  • has() and hasMany() return this.get(key) !== undefined.
  • take() returns early only when the value is undefined.
  • KeyvCacheableMemory.get() returns the result of the store get() directly.

The methods still read through get(). Thus stats, hooks, LRU order, and expiry stay the same.

Verification

  • New tests fail on main: 2 failed, 138 passed (140).
  • The new adapter test fails before the get() fix: expected undefined to be +0.
  • With the fix: pnpm test:ci in packages/memory passes 163 of 163 tests.
  • Coverage of packages/memory stays at 100% for statements, branches, functions, and lines.
  • Biome check: no findings.

Test audit

  1. CacheableMemory Has > should return true for falsy values

    • Contract: has() and hasMany() return true for a key that holds 0, false, "", or null.
    • Regression: a truthy check on the value, as on main. The test fails on main.
    • Gap: the existing has tests use only truthy values.
    • Export or hook: none. The test uses the public API.
  2. CacheableMemory Take > should take and delete falsy values

    • Contract: takeMany() (and through it take()) returns falsy values and removes their entries.
    • Regression: a falsy check in take(), as on main. The test fails on main.
    • Gap: the existing take tests use only truthy values.
    • Export or hook: none. The test uses the public API.
  3. Keyv Cacheable Memory > should keep falsy values in has(), get(), and getMany()

    • Contract: the adapter has(), get(), and getMany() agree for 0, false, "", and null.
    • Regression: a truthy check in the adapter get(). The test fails on the code before the fix.
    • Gap: the existing adapter tests use only truthy values.
    • Export or hook: none. The test uses the public adapter API.

No existing test was changed or deleted.

…Many()

has() and hasMany() used Boolean() on the value. take() used a falsy
check. Thus a stored 0, false, "", or null showed as missing, and
take() did not remove the entry. Now each method compares the value
with undefined.
Comment thread packages/memory/src/index.ts Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

memory: has(), hasMany(), take(), and takeMany() ignore falsy values

1 participant