defu icon indicating copy to clipboard operation
defu copied to clipboard

fix: merge symbol keys instead of overwriting them

Open MFA-G opened this issue 5 months ago β€’ 1 comments

Summary

Fixes #145.

_defu iterated over the base object with Object.keys(baseObject), which skips symbol keys. The result object is built from { ...defaults }, and spread does copy enumerable symbol keys. So symbol-keyed properties from later (lower-priority) arguments overwrote earlier (higher-priority) ones instead of being merged.

const [a, b, c] = [Symbol("a"), Symbol("b"), Symbol("c")];
defu({ [a]: "a", [c]: ["a", "b"] }, { [a]: "bbb", [b]: "c", [c]: ["c", "d"] });

// before: { [a]: "bbb", [b]: "c", [c]: ["c", "d"] }   ❌ symbol keys not merged
// after:  { [a]: "a",   [b]: "c", [c]: ["a","b","c","d"] }  βœ…

Changes

  • Add getOwnEnumerableKeys to src/_utils.ts β€” returns own enumerable keys including symbols, mirroring object-spread semantics. Non-enumerable symbols stay excluded (consistent with how Object.keys excludes non-enumerable string keys and with the spread used to build the result).
  • Use it in _defu instead of Object.keys.

This keeps recursive merging, array concat, and the merger callback working for symbol keys too.

Tests

Added three regression tests to test/defu.test.ts:

  • merging symbol keys across arguments (the issue's reproduction)
  • recursive merge of nested symbol-keyed objects
  • non-enumerable symbol keys are ignored

All checks pass locally: pnpm lint, pnpm test:types, and pnpm vitest run (26 tests).

Summary by CodeRabbit

  • New Features

    • Default merging now includes enumerable symbol-keyed properties, matching normal object spread behavior more closely.
    • Added support for retrieving all own enumerable keys, including symbol keys.
  • Bug Fixes

    • Improved handling of nested and array-based merges when symbol keys are used.
    • Non-enumerable symbol properties are still excluded from merge results.

MFA-G avatar Jun 27 '26 13:06 MFA-G

Review Change Stack

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b4155fbf-c217-4396-a764-1803ec784cf3

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 266209f16c0bb006978ab47616f5acb7676b4942 and ca3a6b1361b0e621c132fc3c49b4d1952e950dc5.

πŸ“’ Files selected for processing (1)
  • test/defu.test.ts

πŸ“ Walkthrough

Walkthrough

defu now enumerates own enumerable symbol keys during merging via a new helper. Tests cover enumerable symbol keys, nested symbol-keyed objects, custom symbol-key merging, and non-enumerable symbol properties.

Changes

Symbol-key merge support

Layer / File(s) Summary
Enumerable key helper
src/_utils.ts
A helper collects own enumerable string keys and own enumerable symbol keys.
Merge loop uses helper
src/defu.ts
defu imports the helper and iterates over enumerable own keys while preserving the existing merge flow.
Symbol-key tests
test/defu.test.ts
Tests cover enumerable symbol merging, nested symbol-keyed objects, custom merge logic, and ignored non-enumerable symbol properties.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

πŸ‡ I hopped through keys both bright and sly,
Symbols now merge as they float by.
Enumerable paws, in a tidy line,
Make defu's bundles match just fine.

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title concisely and accurately describes the main change: merging symbol keys instead of overwriting them.
Linked Issues check βœ… Passed The changes address #145 by iterating enumerable symbol keys during merging and add tests for the reported symbol-key cases.
Out of Scope Changes check βœ… Passed The helper extraction and symbol-key tests are directly tied to the reported bug and do not introduce unrelated functionality.
Docstring Coverage βœ… Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jun 27 '26 13:06 coderabbitai[bot]