Skip to content

Commit

Permalink
v 0.3.5 - WeakMap and WeakSet support
Browse files Browse the repository at this point in the history
  • Loading branch information
jfet97 committed Jan 6, 2019
1 parent b88741f commit b513f61
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import omniclone from 'omniclone';
2. let you to share the `[[Prototype]]` object between source and the resulting object (customizable behavior)
3. let you to clone objects with circular references (customizable behavior)
4. let you to copy getters and setters, non enumerables properties and also symbols (customizable behavior)
5. correct handling of String, Boolean, Number, Error and Promise objects
5. correct handling of String, Boolean, Number, Error, Promise, WeakMapm and WeakSet objects
6. safe similar sibilings references are not duplicated
7. correct cloning of Array objects (if the `invokeConstructors` flag is setted)
8. correct cloning of RegExp and Date objects
Expand Down Expand Up @@ -168,7 +168,7 @@ omniclone(source, {
});
```

## what about String, Boolean, Number, Error and Promise objects?
## what about String, Boolean, Number, Error, Promise, WeakMap and WeakSet objects?

String, Boolean and Number objects passed to `omniclone` as sources will produce `null`.\
Error objects passed to `omniclone` as sources will produce `null` if the `discardErrorObjects` is set to `true` (as default).\
Expand All @@ -178,8 +178,8 @@ String, Boolean and Number objects props will be unwrapped.\
Error objects props will be discarded if the `discardErrorObjects` is set to `true` (as default).\
Error objects props will produce a `TypeError` if the `discardErrorObjects` is set to `false` (not the predefined behaviour).

Promise objects will be returned if passed to `omniclone` as sources.\
Promise objects props will be shallow copied.
Promise, WeakMap and WeakSet objects will be returned if passed to `omniclone` as sources.\
Promise, WeakMap and WeakSet objects props will be shallow copied.

## what about the 6th strength?

Expand All @@ -201,4 +201,4 @@ When you will use `JSON.parse()`, an `{"foo":"bar"}` object will be created for
2. `super` is statically bound to a class heirarchy, remember it
3. `Error` objects cannot be properly copied because of js limitations
4. currently there is no isomorphic way to detect if an object is a `Proxy` nor is possible to access the handler object. Because of transparent virtualization, `omniclone` will copy each properties, the `constructor` and the `[[Prototype]]` directly from the proxed object.
5. currenlty there is a lack of support for Map, Set, WeakMap and WeakSet objects. I'm working on this.
5. currenlty there is a lack of support for Map and Set objects. I'm working on this.
26 changes: 26 additions & 0 deletions __test__/omniclone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,39 @@ describe("omniclone", () => {
expect(res).toBe(p);
});

it("should return the weakmap when a WeakMap object is passed as source", () => {
const wm = new WeakMap();
const res = omniclone(wm);
expect(res).toBe(wm);
});

it("should return the weakset when a WeakSet object is passed as source", () => {
const ws = new WeakSet();
const res = omniclone(ws);
expect(res).toBe(ws);
});

it("should shallow copy a Promise prop", () => {
const p = Promise.resolve();
const ob1 = { p };
const res = omniclone(ob1);
expect(res.p).toBe(ob1.p);
});

it("should shallow copy a WeakMap prop", () => {
const wm = new WeakMap();
const ob1 = { wm };
const res = omniclone(ob1);
expect(res.vm).toBe(ob1.vm);
});

it("should shallow copy a WeakSet prop", () => {
const ws = new WeakSet();
const ob1 = { ws };
const res = omniclone(ob1);
expect(res.ws).toBe(ob1.ws);
});

it("should clone a RegExp if it is passed as source", () => {
(() => {
const r = new RegExp("foo", "g");
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.3.4",
"version": "0.3.5",
"description": "deep cloning function for js objects",
"main": "dist/main.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/deepclone.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ function deepClone(source, config) {
return;
}

// WeakMaps are shallow cloned
if (value instanceof WeakMap) {
Object.defineProperty(res, prop, descriptor);
return;
}

// WeakSets are shallow cloned
if (value instanceof WeakSet) {
Object.defineProperty(res, prop, descriptor);
return;
}

// recursive deep copy for the others object props
// eslint-disable-next-line no-use-before-define
res[prop] = innerDeepClone(value, config, references, start);
Expand Down
6 changes: 5 additions & 1 deletion src/omniclone.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ function omniclone(
return null;
}

if (obj instanceof Promise) {
if (
obj instanceof Promise ||
obj instanceof WeakMap ||
obj instanceof WeakSet
) {
return obj;
}

Expand Down

0 comments on commit b513f61

Please sign in to comment.