Skip to content

Fix getItem/setItem/clear edge cases with falsy and empty-string values#30

Open
mc21fly wants to merge 1 commit into
Download:masterfrom
mc21fly:fix/getitem-empty-string-and-string-coercion
Open

Fix getItem/setItem/clear edge cases with falsy and empty-string values#30
mc21fly wants to merge 1 commit into
Download:masterfrom
mc21fly:fix/getitem-empty-string-and-string-coercion

Conversation

@mc21fly

@mc21fly mc21fly commented Jul 15, 2026

Copy link
Copy Markdown

Problem

Three edge cases where MemoryStorage's behavior diverges from the real
Web Storage API:

  1. getItem() returns null for a stored empty string (""), even
    though the key genuinely exists. Real localStorage.getItem()
    returns null only when the key doesn't exist at all.
  2. setItem() doesn't coerce values to strings. Real localStorage
    always stores/returns strings, even if you pass a number or other type.
  3. clear() uses a loop that terminates early if any key happens to be
    an empty string, since "" is falsy in the loop's key=keys[i] condition.

Fix

Minimal, targeted changes to getItem, setItem, and clear in
src/memorystorage.js, plus regression tests covering all three cases.

Testing

All existing tests still pass; added 3 new tests specifically covering
these edge cases.

- getItem no longer returns null for a legitimately stored empty string
- setItem now coerces values to string, matching real Storage API behavior
- clear() no longer stops early when a key is an empty string
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.

1 participant