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

Adding Support for _.cloneDeep using recursiveClone #306

Open
wants to merge 3 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ objects can easily be converted to an array by use of the
1. [_.some](#_some)
1. [_.sortBy](#_sortby-and-_orderby)
1. [_.uniq](#_uniq)
1. [_.cloneDeep](#_cloneDeep)

**[Function](#function)**

Expand Down Expand Up @@ -1704,6 +1705,84 @@ Produces a duplicate-free version of the array, using === to test object equalit

**[⬆ back to top](#quick-links)**

### _.cloneDeep

Produces an exact replica of the original (source) array or object. Unlike, JSON.stringify+parse this will retain functions.
The intention of cloneDeep is to be able to modify values without effecting the original object on the heap.
```js
var original = {
method: (inp1, inp2) => inp1*inp2,
nestedObject: {
someKey: {
someValues: [
{obj: 1},
{obj: {
furtherNested: { moreNested: { extremelyNested: (inp1, inp2) => inp1 ** inp2}}
}
}
]
}
}
} // or [original, original]

// Lodash/Underscore
var result = _.cloneDeep(original)
console.log(result)
// output:
// {
// method: (inp1, inp2) => inp1*inp2,
// nestedObject: {
// someKey: {
// someValues: [
// {obj: 1},
// {obj: {
// furtherNested: { moreNested: { extremelyNested: (inp1, inp2) => inp1 ** inp2}}
// }
// }
// ]
// }
// }
// }

// Native
const recursiveClone = (src) => {
if (Array.isArray(src)) { // for arrays
return src.map(recursiveClone)
}
if (src === null || typeof src !== 'object') { // for primitives / functions / non-references/pointers
return src
}
return Object.fromEntries(
Object.entries(src).map(
([key, val]) => ([key, recursiveClone(val)])
)
)
}
const result = recursiveClone(original)
// output:
// {
// method: (inp1, inp2) => inp1*inp2,
// nestedObject: {
// someKey: {
// someValues: [
// {obj: 1},
// {obj: {
// furtherNested: { moreNested: { extremelyNested: (inp1, inp2) => inp1 ** inp2}}
// }
// }
// ]
// }
// }
// }
```
#### Browser Support for Object.fromEntries (Object.entries is much earlier) can also be polyfilled

![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image]
:-: | :-: | :-: | :-: | :-: | :-: |
73.0 ✔ | 79.0 ✔ | 63.0 ✔ | ✖ | 60.0 ✔ | 12.1 ✔ |

**[⬆ back to top](#quick-links)**

## Function

### _.after
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,48 @@ describe('code snippet example', () => {
)
})
})

describe('cloneDeep', () => {
const original = {
method: (inp1, inp2) => inp1*inp2,
nestedObject: {
someKey: {
someValues: [
{obj: 1},
{obj: {
furtherNested: { moreNested: { extremelyNested: (inp1, inp2) => inp1 ** inp2}}
}
}
]
}
}
}
const recursiveClone = (src) => {
if (Array.isArray(src)) { // for arrays
return src.map(recursiveClone)
}
if (src === null || typeof src !== 'object') { // for primitives / functions / non-references/pointers
return src
}
return Object.fromEntries(
Object.entries(src).map(
([key, val]) => ([key, recursiveClone(val)])
)
)
}
const result = recursiveClone(original)

// check equality
assert.deepEqual(original, result)

// check methods
assert.equal(original.method(232, 59), result.method(232, 59))
assert.equal(original.nestedObject.someKey.someValues[0][1].obj.furtherNested.moreNested.extremelyNested(232, 4), result.nestedObject.someKey.someValues[0][1].obj.furtherNested.moreNested.extremelyNested(232, 4))

// check modifying reference
result.nestedObject.someKey.someValues[0].obj = 99
assert.equals(original.nestedObject.someKey.someValues.obj, 1)
})
describe('extend', () => {
function Foo() {
this.c = 3;
Expand Down