Skip to content

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

Merged
jaredwray merged 3 commits into
jaredwray:mainfrom
bardrop-oss:fix-memory-has-take-falsy
Nov 28, 2026
Merged

jaredwray merged 3 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, "", null, NaN, and 0n. 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 API note states that undefined means no value. Falsy values remain valid cache values.

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

Verification

  • pnpm build passes.
  • node scripts/test-build.mjs passes.
  • pnpm test passes all 1,409 tests across 64 files on Node.js 24.
  • The memory package passes all 163 tests.
  • The memory package reports 100% coverage for statements, branches, functions, and lines.
  • All three regression tests fail against the original source at a51d2b8 for the expected defects.
  • The adapter test fails before its fix at 6a3b56d: expected undefined to be +0.
  • Test audit: all three tests pass the gate below. Their existing tables now include NaN and 0n.
  • git diff --check passes.

Test audit

  1. CacheableMemory Has > should return true for falsy values
    • Contract: has() and hasMany() return true for 0, false, "", null, NaN, and 0n.
    • Regression: a truthiness check treats a stored value as an absent key. This test fails against the original source.
    • Gap: tests on the base branch use truthy values. This PR extends the table in the new regression test.
    • Table change: add NaN and 0n to protect the same contract for numeric and BigInt values.
    • Boundary: the test uses public methods. It needs no new export, flag, or hook.
  2. CacheableMemory Take > should take and delete falsy values
    • Contract: takeMany(), through take(), returns each falsy value and removes its entry.
    • Regression: a truthiness check returns undefined and leaves the entry in the cache. This test fails against the original source.
    • Gap: tests on the base branch use truthy values. This PR extends the table in the new regression test.
    • Table change: add NaN and 0n to check their return values and entry removal.
    • Boundary: the test uses public methods. It needs no new export, flag, or hook.
  3. Keyv Cacheable Memory > should keep falsy values in has(), get(), and getMany()
    • Contract: the adapter returns consistent results from has(), get(), and getMany() for all six values.
    • Regression: a truthiness check in get() returns undefined for a stored value. This test fails before the adapter fix.
    • Gap: tests on the base branch use truthy values. This PR extends the table in the new regression test.
    • Table change: add NaN and 0n to check the adapter with numeric and BigInt values.
    • Boundary: the test uses public adapter methods. It needs no new export, flag, or hook.

This PR keeps all three tests. It changes no test from the base branch and deletes no test.

…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
Comment thread packages/memory/README.md

## CacheableMemory - API

A returned `undefined` means no value. The cache treats `0`, `false`, `""`, `null`, `NaN`, and `0n` as valid values.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low - BigInt values are omitted from documented value-size statistics

The new API documentation declares 0n a valid cached value, but an enabled cache records zero value bytes for every BigInt because the size estimator has no BigInt case. As a result, stats.vsize undercounts caches that store BigInt values and remains inconsistent with the documented current-content size counter.

Show fix

Handle bigint in Stats.roughSizeOfObject with an explicit size estimate, and add a statistics-enabled test that verifies vsize after setting, overwriting, and deleting a BigInt value.

More info - Reply on this comment to give feedback or ignore the issue.

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a51d2b8) to head (8b7e29e).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1767   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           30        30           
  Lines         3702      3697    -5     
  Branches       832       832           
=========================================
- Hits          3702      3697    -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jaredwray
jaredwray merged commit de2124b into jaredwray:main Nov 28, 2026
12 checks passed
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

2 participants