Skip to content

Commit

Permalink
v1.1.2 - add support for es6 set and map
Browse files Browse the repository at this point in the history
  • Loading branch information
clarketm committed Oct 26, 2018
1 parent 6204ef8 commit b32b634
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 20 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ Light-weight deep clone implementation for JavaScript.
## Installation
```bash
$ npm install @clarketm/deepclone
```

## Modules

<dl>
Expand Down
2 changes: 1 addition & 1 deletion dist/DeepClone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(global.DeepClone = factory());
}(this, (function () { 'use strict';

function DeepClone(a,b={}){const{includeNonEnumerable:c=!1}=b;return function a(b){if(null===b||"object"!=typeof b)return b;if(b instanceof Date)return new Date(b.valueOf());if(b instanceof Array){let c=[];return b.forEach((d,e)=>c[e]=a(b[e])),c}if(b instanceof Object){let d={};return Object.getOwnPropertySymbols(b).forEach(c=>d[c]=a(b[c])),c?Object.getOwnPropertyNames(b).forEach(c=>d[c]=a(b[c])):Object.keys(b).forEach(c=>d[c]=a(b[c])),d}throw new Error(`Unable to copy object: ${b}`)}(a)}
function DeepClone(a,b={}){const{includeNonEnumerable:c=!1}=b;return function a(b){if(null===b||"object"!=typeof b)return b;if(b instanceof Date)return new Date(b.valueOf());if(b instanceof Array){let c=[];return b.forEach((d,e)=>c[e]=a(b[e])),c}if(b instanceof Set){let c=new Set;return b.forEach(b=>c.add(a(b))),c}if(b instanceof Map){let c=new Map;return b.forEach((b,d)=>c.set(d,a(b))),c}if(b instanceof Object){let d={};return Object.getOwnPropertySymbols(b).forEach(c=>d[c]=a(b[c])),c?Object.getOwnPropertyNames(b).forEach(c=>d[c]=a(b[c])):Object.keys(b).forEach(c=>d[c]=a(b[c])),d}throw new Error(`Unable to copy object: ${b}`)}(a)}

return DeepClone;

Expand Down
30 changes: 23 additions & 7 deletions docs/DeepClone.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,39 @@ <h1 class="page-title">DeepClone.js</h1>
return copy;
}

// Handle:
// * Set
if (item instanceof Set) {
let copy = new Set();

item.forEach(v => copy.add(_DeepClone(v)));

return copy;
}

// Handle:
// * Map
if (item instanceof Map) {
let copy = new Map();

item.forEach((v, k) => copy.set(k, _DeepClone(v)));

return copy;
}

// Handle:
// * Object
if (item instanceof Object) {
let copy = {};

// Handle:
// * Object.symbol
Object.getOwnPropertySymbols(item).forEach(
s => (copy[s] = _DeepClone(item[s]))
);
Object.getOwnPropertySymbols(item).forEach(s => (copy[s] = _DeepClone(item[s])));

// Handle:
// * Object.name (other)
if (includeNonEnumerable) {
Object.getOwnPropertyNames(item).forEach(
k => (copy[k] = _DeepClone(item[k]))
);
Object.getOwnPropertyNames(item).forEach(k => (copy[k] = _DeepClone(item[k])));
} else {
Object.keys(item).forEach(k => (copy[k] = _DeepClone(item[k])));
}
Expand Down Expand Up @@ -169,7 +185,7 @@ <h1 class="page-title">DeepClone.js</h1>
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 01:12:14 GMT-0700 (PDT) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Oct 26 2018 00:26:10 GMT-0700 (Pacific Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ <h5>Type:</h5>
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 01:12:14 GMT-0700 (PDT) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Oct 26 2018 00:26:10 GMT-0700 (Pacific Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 01:12:14 GMT-0700 (PDT) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Oct 26 2018 00:26:10 GMT-0700 (Pacific Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/module-DeepClone.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h1 class="page-title">DeepClone</h1>
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 01:12:14 GMT-0700 (PDT) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Oct 26 2018 00:26:10 GMT-0700 (Pacific Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clarketm/deepclone",
"version": "1.0.2",
"version": "1.1.2",
"description": "JavaScript deep clone implementation",
"main": "dist/DeepClone.js",
"scripts": {
Expand Down
28 changes: 22 additions & 6 deletions src/DeepClone.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,39 @@ function DeepClone(obj, config = {}) {
return copy;
}

// Handle:
// * Set
if (item instanceof Set) {
let copy = new Set();

item.forEach(v => copy.add(_DeepClone(v)));

return copy;
}

// Handle:
// * Map
if (item instanceof Map) {
let copy = new Map();

item.forEach((v, k) => copy.set(k, _DeepClone(v)));

return copy;
}

// Handle:
// * Object
if (item instanceof Object) {
let copy = {};

// Handle:
// * Object.symbol
Object.getOwnPropertySymbols(item).forEach(
s => (copy[s] = _DeepClone(item[s]))
);
Object.getOwnPropertySymbols(item).forEach(s => (copy[s] = _DeepClone(item[s])));

// Handle:
// * Object.name (other)
if (includeNonEnumerable) {
Object.getOwnPropertyNames(item).forEach(
k => (copy[k] = _DeepClone(item[k]))
);
Object.getOwnPropertyNames(item).forEach(k => (copy[k] = _DeepClone(item[k])));
} else {
Object.keys(item).forEach(k => (copy[k] = _DeepClone(item[k])));
}
Expand Down
4 changes: 4 additions & 0 deletions test/DeepClone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ describe("#DeepClone", () => {
f: [1, 2, { b: 4 }],
g: [1, 2, { b: 4 }],
[Symbol("abc")]: 3,
m: new Map([[1, 2]]),
nm: new Map([[1, new Map([[2, 3]])]]),
s: new Set([1, 2]),
ns: new Set([{ a: 1 }, 2]),
[123]: { [123]: ["k", false] },
$: new Date(),
[new Map([["@", "#"]])]: () => 3,
Expand Down

0 comments on commit b32b634

Please sign in to comment.