Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 26, 2025

cloneDeepWith in es-toolkit/compat preserves null prototype when cloning objects created with Object.create(null), while lodash returns an object with Object.prototype.

import { cloneDeepWith } from 'es-toolkit/compat';

const obj = Object.create(null);
obj.a = 1;

const cloned = cloneDeepWith(obj);
// Before: Object.getPrototypeOf(cloned) === null, cloned.toString === undefined
// After:  Object.getPrototypeOf(cloned) === Object.prototype, cloned.toString === [Function]

Changes

  • Added objectTag case in compat customizer to detect plain objects with null prototype
  • Create new object with Object.prototype instead of preserving null prototype
  • Handle circular references for null prototype objects
  • Added tests for basic, nested, customizer, and circular reference cases
Original prompt

This section details on the original issue you should resolve

<issue_title>Minor behaviour difference in cloneDeepWith compared to lodash-es</issue_title>
<issue_description>Given the following code:

const data = cloneDeepWith( Object.create( null ) );
console.log( data.toString );

The object returned by lodash-es has an Object prototype, even though the original object was created using Object.create( null ), so the output to the console is [Function: toString]. However, the same function in es-toolkit/compat returns an object without a prototype, so the output to the console is undefined.

While the es-toolkit behavior makes more sense to me, you may consider this a bug if you care about full backward compatibility with lodash.


Reproduction: https://stackblitz.com/edit/node-e1e3y8ws?file=index.js

In the console, run node index.js.</issue_description>

<agent_instructions>Could you update cloneDeepWith to match lodash's behavior? Currently, when cloning an object created with Object.create(null), es-toolkit preserves the null prototype, while lodash returns an object with the standard Object prototype.
For full backward compatibility with lodash, the cloned object should have the Object prototype regardless of the source object's prototype.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@vercel
Copy link

vercel bot commented Nov 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Nov 26, 2025 2:59pm

Copilot AI changed the title [WIP] Fix behavior of cloneDeepWith to match lodash-es Fix cloneDeepWith to match lodash behavior for null prototype objects Nov 26, 2025
Copilot AI requested a review from dayongkr November 26, 2025 14:54
Copilot finished work on behalf of dayongkr November 26, 2025 14:54
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.

Minor behaviour difference in cloneDeepWith compared to lodash-es

2 participants