Skip to content

fix: preserve undefined values in default maps - #267

Closed
fbirds5230 wants to merge 1 commit into
Yomguithereal:masterfrom
fbirds5230:fix/preserve-undefined-default-map-values
Closed

fbirds5230 wants to merge 1 commit into
Yomguithereal:masterfrom
fbirds5230:fix/preserve-undefined-default-map-values

Conversation

@fbirds5230

Copy link
Copy Markdown

DefaultMap and DefaultWeakMap run the factory again when an existing entry contains undefined. This can overwrite an explicitly stored value or repeat the factory's side effects. In DefaultMap, it also increments size without adding a key, so subsequent factory calls receive the wrong index.

Check whether the key exists before creating a default value when get() returns undefined.

The regression tests cover explicitly stored and factory-produced undefined values, factory call counts, map size and factory indices. They also check that deleting an entry allows its default value to be created again. All four new tests fail on the original implementation.

Validation on Node.js 24.22.3:

  • npm run lint passes.
  • npm test: 570 passing, 1 existing pending test.
  • npm run test:exports passes, including TypeScript and the CommonJS/ESM checks.

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.

2 participants