fix: merge symbol keys instead of overwriting them
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
getOwnEnumerableKeystosrc/_utils.tsβ returns own enumerable keys including symbols, mirroring object-spread semantics. Non-enumerable symbols stay excluded (consistent with howObject.keysexcludes non-enumerable string keys and with the spread used to build the result). - Use it in
_defuinstead ofObject.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.
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,
Makedefu'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.
Comment @coderabbitai help to get the list of available commands.