Skip to content

Commit

Permalink
fix(loki): cloning method for specific classes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatorus committed Oct 5, 2017
1 parent a8abe99 commit 4f4a182
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/loki/spec/generic/cloning.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ describe("cloning behavior", () => {
});
});

describe("cloning method \"shallow\" save prototype", function () {
it("works", () => {
function Item(name, owner, maker) {
this.name = name;
this.owner = owner;
this.maker = maker;
}

const cdb = new loki("clonetest");
const citems = cdb.addCollection("items", {clone: true, cloneMethod: "shallow"});
const oldObject = new Item("mjolnir", "thor", "dwarves");
const insObject = citems.insert(oldObject);

// cant' have either of these polluting our collection
oldObject.name = "mewmew";
insObject.name = "mewmew";

const result = citems.findOne({"owner": "thor"});
expect(result instanceof Item).toBe(true);
expect(result.name).toBe("mjolnir");
});
});

describe("collection find() cloning works", () => {
it("works", () => {
const cdb = new loki("cloningEnabled");
Expand Down
4 changes: 2 additions & 2 deletions packages/loki/src/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export function clone(data, method) {
break;
case "shallow":
// more compatible method for older browsers
cloned = data.prototype ? Object.create(data.prototype) : {};
cloned = Object.create(data.constructor.prototype);
Object.keys(data).map((i) => {
cloned[i] = data[i];
});
break;
case "shallow-assign":
// should be supported by newer environments/browsers
cloned = data.prototype ? Object.create(data.prototype) : {};
cloned = Object.create(data.constructor.prototype);
Object.assign(cloned, data);
break;
default:
Expand Down

0 comments on commit 4f4a182

Please sign in to comment.