Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use native structuredClone() in _.cloneDeep() #5855

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/cloneDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ const CLONE_SYMBOLS_FLAG = 4;

/**
* This method is like `clone` except that it recursively clones `value`.
* Object inheritance is preserved.
* Object inheritance is preserved. The method will attempt to use the native
* [`structuredClone`](https://developer.mozilla.org/docs/Web/API/structuredClone)
* function, if `value` [is supported](https://developer.mozilla.org/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types)
* (and the native implementation is available). Otherwise it will fallback to a
* custom implementation.
*
* @since 1.0.0
* @category Lang
* @param {*} value The value to recursively clone.
* @param {object} [options]
* @param {boolean} [options.skipNativeCheck]
* Skip the native check and use the custom implementation. This is useful when
* `value` is known to be incompatible `structuredClone`.
* @returns {*} Returns the deep cloned value.
* @see clone
* @example
Expand All @@ -20,9 +28,23 @@ const CLONE_SYMBOLS_FLAG = 4;
* const deep = cloneDeep(objects)
* console.log(deep[0] === objects[0])
* // => false
*
* // The `skipNativeCheck` flag
* const unsupportedNativeObject = { fn: () => 'a' };
*
* const deep = cloneDeep(unsupportedNativeObject, { skipNativeCheck: true });
* console.log(deep === unsupportedNativeObject);
* // => false
*/
function cloneDeep(value) {
return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG);
function cloneDeep(value, options = {}) {
try {
if (!options.skipNativeCheck && structuredClone) {
return structuredClone(value);
}
throw new Error('structuredClone unsupported or skipped');
} catch {
return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG);
}
}

export default cloneDeep;
8 changes: 8 additions & 0 deletions test/clone-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ xdescribe('clone methods', function () {
assert.notStrictEqual(actual, cyclical[`v${LARGE_ARRAY_SIZE - 1}`]);
});

it('`_.cloneDeep` should accept the `skipNativeCheck` flag', () => {
const object = { primitive: 'a', fn: () => 'b' };
const actual = cloneDeep(object, { skipNativeCheck: true });

expect(actual).not.toEqual(object);
expect(actual.primitive).not.toEqual(object.primitive);
});

it('`_.cloneDeepWith` should provide `stack` to `customizer`', () => {
let actual;

Expand Down