diff --git a/src/scripts/lib/clone.js b/src/scripts/lib/clone.js new file mode 100644 index 0000000..a6b9ae6 --- /dev/null +++ b/src/scripts/lib/clone.js @@ -0,0 +1,12 @@ +module.exports = function(obj) { + if (null === obj || "object" != typeof obj) { + return obj + } + var copy = obj.constructor() + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) { + copy[attr] = obj[attr] + } + } + return copy +} diff --git a/tests/scripts/lib/clone.js b/tests/scripts/lib/clone.js new file mode 100644 index 0000000..d991e24 --- /dev/null +++ b/tests/scripts/lib/clone.js @@ -0,0 +1,17 @@ +var tape = require("tape") + +var clone = require("../../../src/scripts/lib/clone") + +tape("test clone method", function(t) { + var obj = {one: 1, two: 2} + var cloned = clone(obj) + + t.notEqual(obj, cloned, "cloned object isn't the object") + + t.same(obj, cloned, "cloned object have the same values than object") + + cloned.tree = 3 + t.notSame(obj, cloned, "modified cloned object haven't the same values than object") + + t.end() +})