Skip to content

Commit

Permalink
add indestructible support for Array, Map and Set objects (flag won't…
Browse files Browse the repository at this point in the history
… be alter thei construction anymore)
  • Loading branch information
jfet97 committed Jan 7, 2019
1 parent ca7bc7b commit cf1f4c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ res instanceof Test; // true
res.foo instanceof Test; // true
```

It is actually a default enabled setting, but you can disable it (loosing the ability to properly clone array, map and set objects).\
It is actually a default enabled setting, but you can disable it (Array, Map and Set objects will be properly cloned anyway).\
__It is highly discouraged to disable this flag__, do it only if you know what you are doing.

If the `invokeConstructors` flag is set to `false`, a plain new object will be created for each object prop and for the resulting object as well. So the `constructor` prop will be set to the `Object` function, and the `[[Prototype]]` prop will be `Object.prototype`.\
Expand All @@ -74,6 +74,7 @@ Unless you use the `setPrototype` flag.

### setPrototype (default false)
If the `invokeConstructors` flag is setted to `false` we could anyway share the `[[Prototype]]` object between the source object and the resulting object thanks to the `setPrototype` flag, __without calling the constructors__.\
(Array, Map and Set objects will be properly cloned anyway because for them the constructor will be always called).\
This means that the `constructor` prop will be shared as well because it is related to the `[[Prototype]]` prop.\
This flag affects all the object properties as weel, like the previous flag.\
If the `invokeConstructors` flag is setted to `true`, the `setPrototype` flag will be is ignored.
Expand Down
2 changes: 1 addition & 1 deletion dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omniclone",
"version": "0.5.2",
"version": "0.5.3",
"description": "deep cloning function for js objects",
"main": "dist/main.js",
"scripts": {
Expand Down
21 changes: 19 additions & 2 deletions src/deepClone.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function deepClone(source, config) {
// result value
let res = null;

// invokeConstructors flag indicates if the source constructor
// must be invocated.
if (invokeConstructors) {
// invokeConstructors flag indicates if the source constructor
// must be invocated.
res = new source.constructor();
// if so, the [[Prototype]] prop is set to constructor.protoype
// so it could be different from the source [[Prototype]]
Expand All @@ -44,7 +44,19 @@ function deepClone(source, config) {
res = {};
}

// special case: Array
// to properly create arrays even when invokeConstructors flag is false
// and/or when setPrototype flag is false too
if (source instanceof Array) {
res = [];
}

if (source instanceof Map) {
// special case: Map
// to properly create maps even when invokeConstructors flag is false
// and/or when setPrototype flag is false too
res = new Map();

// get the entries array
const mapEntries = [...source.entries()];

Expand All @@ -58,6 +70,11 @@ function deepClone(source, config) {
innerDeepClone
);
} else if (source instanceof Set) {
// special case: Set
// to properly create sets even when invokeConstructors flag is false
// and/or when setPrototype flag is false too
res = new Set();

// get the values array
const setEntries = [...source.values()];

Expand Down

0 comments on commit cf1f4c2

Please sign in to comment.