[Pkg-javascript-commits] [node-clone] 01/04: New upstream version 2.1.0

Julien Puydt julien.puydt at laposte.net
Wed Dec 7 15:50:29 UTC 2016


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-clone.

commit 4aceb95d553ca166b969ee456c926a4fa8f01a54
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Wed Dec 7 16:47:35 2016 +0100

    New upstream version 2.1.0
---
 .gitignore   |   1 +
 .travis.yml  |   3 ++
 README.md    |  53 ++++++++++++++++--
 clone.js     |  34 ++++++++++--
 package.json |   6 ++-
 test.js      | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 261 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index d4ede7c..b44644b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 node_modules/
 *.iml
+.idea
diff --git a/.travis.yml b/.travis.yml
index aa73da6..ff4f713 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,4 +3,7 @@ node_js:
   - "0.10"
   - "0.12"
   - "4"
+  - "5"
+  - "6"
+  - "node"
   - "iojs"
diff --git a/README.md b/README.md
index 1c7ec78..dc37936 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,8 @@
 
 [![info badge](https://nodei.co/npm/clone.svg?downloads=true&downloadRank=true&stars=true)](http://npm-stat.com/charts.html?package=clone)
 
-offers foolproof _deep cloning_ of objects, arrays, numbers, strings etc. in JavaScript.
+offers foolproof _deep cloning_ of objects, arrays, numbers, strings, maps,
+sets, promises, etc. in JavaScript.
 
 
 ## Installation
@@ -52,10 +53,15 @@ can clone dates in arrays in objects, for example.
   * `circular` -- boolean
 
     Call `clone` with `circular` set to `false` if you are certain that `obj`
-    contains no circular references. This will give better performance if needed.
-    There is no error if `undefined` or `null` is passed as `obj`.
+    contains no circular references. This will give better performance if
+    needed. There is no error if `undefined` or `null` is passed as `obj`.
   * `depth` -- depth to which the object is to be cloned (optional,
     defaults to infinity)
+  * `prototype` -- sets the prototype to be used when cloning an object.
+    (optional, defaults to parent prototype).
+  * `includeNonEnumerable` -- set to `true` if the non-enumerable properties
+    should be cloned as well. Non-enumerable properties on the prototype chain
+    will be ignored. (optional, defaults to `false`)
 
 `clone.clonePrototype(obj)`
 
@@ -92,6 +98,45 @@ So, `b.myself` points to `b`, not `a`. Neat!
     npm test
 
 
+## Changelog
+
+### v2.1.0
+
+#### 2016-11-22
+
+  - Add support for cloning Errors
+  - Exclude non-enumerable symbol-named object properties from cloning
+  - Add option to include non-enumerable own properties of objects
+
+### v2.0.0
+
+#### 2016-09-28
+
+  - Add support for cloning ES6 Maps, Sets, Promises, and Symbols
+
+### v1.0.2
+
+#### 2015-03-25
+
+  - Fix call on getRegExpFlags
+  - Refactor utilities
+  - Refactor test suite
+
+### v1.0.1
+
+#### 2015-03-04
+
+  - Fix nodeunit version
+  - Directly call getRegExpFlags
+
+### v1.0.0
+
+#### 2015-02-10
+
+  - Improve browser support
+  - Improve browser testability
+  - Move helper methods to private namespace
+
 ## Caveat
 
 Some special objects like a socket or `process.stdout`/`stderr` are known to not
@@ -107,7 +152,7 @@ github](https://github.com/pvorb/node-clone/issues) or send me an email to
 
 ## License
 
-Copyright © 2011-2015 [Paul Vorbach](http://paul.vorba.ch/) and
+Copyright © 2011-2016 [Paul Vorbach](http://paul.vorba.ch/) and
 [contributors](https://github.com/pvorb/node-clone/graphs/contributors).
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of
diff --git a/clone.js b/clone.js
index 64fdd79..c3bf162 100644
--- a/clone.js
+++ b/clone.js
@@ -41,13 +41,15 @@ try {
  *    a particular depth. (optional - defaults to Infinity)
  * @param `prototype` - sets the prototype to be used when cloning an object.
  *    (optional - defaults to parent prototype).
+ * @param `includeNonEnumerable` - set to true if the non-enumerable properties
+ *    should be cloned as well. Non-enumerable properties on the prototype
+ *    chain will be ignored. (optional - false by default)
 */
-function clone(parent, circular, depth, prototype) {
-  var filter;
+function clone(parent, circular, depth, prototype, includeNonEnumerable) {
   if (typeof circular === 'object') {
     depth = circular.depth;
     prototype = circular.prototype;
-    filter = circular.filter;
+    includeNonEnumerable = circular.includeNonEnumerable;
     circular = circular.circular;
   }
   // maintain two arrays for circular references, where corresponding parents
@@ -101,6 +103,8 @@ function clone(parent, circular, depth, prototype) {
       child = new Buffer(parent.length);
       parent.copy(child);
       return child;
+    } else if (parent instanceof Error) {
+      child = Object.create(parent);
     } else {
       if (typeof prototype == 'undefined') {
         proto = Object.getPrototypeOf(parent);
@@ -164,7 +168,31 @@ function clone(parent, circular, depth, prototype) {
         // Don't need to worry about cloning a symbol because it is a primitive,
         // like a number or string.
         var symbol = symbols[i];
+        var descriptor = Object.getOwnPropertyDescriptor(parent, symbol);
+        if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
+          continue;
+        }
         child[symbol] = _clone(parent[symbol], depth - 1);
+        if (!descriptor.enumerable) {
+          Object.defineProperty(child, symbol, {
+            enumerable: false
+          });
+        }
+      }
+    }
+
+    if (includeNonEnumerable) {
+      var allPropertyNames = Object.getOwnPropertyNames(parent);
+      for (var i = 0; i < allPropertyNames.length; i++) {
+        var propertyName = allPropertyNames[i];
+        var descriptor = Object.getOwnPropertyDescriptor(parent, propertyName);
+        if (descriptor && descriptor.enumerable) {
+          continue;
+        }
+        child[propertyName] = _clone(parent[propertyName], depth - 1);
+        Object.defineProperty(child, propertyName, {
+          enumerable: false
+        });
       }
     }
 
diff --git a/package.json b/package.json
index 16158e5..0584117 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "function",
     "date"
   ],
-  "version": "2.0.0",
+  "version": "2.1.0",
   "repository": {
     "type": "git",
     "url": "git://github.com/pvorb/node-clone.git"
@@ -36,7 +36,9 @@
     "Aurélio A. Heckert (http://softwarelivre.org/aurium)",
     "Guy Ellis (http://www.guyellisrocks.com/)",
     "fscherwi (https://fscherwi.github.io)",
-    "rictic (https://github.com/rictic)"
+    "rictic (https://github.com/rictic)",
+    "Martin Jurča (https://github.com/jurca)",
+    "Misery Lee <miserylee at foxmail.com> (https://github.com/miserylee)"
   ],
   "license": "MIT",
   "engines": {
diff --git a/test.js b/test.js
index 00c2879..fb4ba67 100644
--- a/test.js
+++ b/test.js
@@ -95,6 +95,20 @@ exports["clone object"] = function (test) {
   test.done();
 };
 
+exports["clone error"] = function (test) {
+  test.expect(4);
+
+  var a = new Error('Boom!!!');
+  var b = clone(a);
+
+  test.deepEqual(b, a);
+  test.notEqual(b, a);
+  test.ok(b instanceof Error);
+  test.equal(b.message, a.message);
+
+  test.done();
+};
+
 exports["clone array"] = function (test) {
   test.expect(2); // how many tests?
 
@@ -511,4 +525,162 @@ if (nativePromise) {
       test.done();
     });
   }
-}
\ No newline at end of file
+}
+
+var nativeSymbol;
+try {
+  nativeSymbol = Symbol
+} catch(_) {}
+if (nativeSymbol) {
+  exports["clone only enumerable symbol properties"] = function (test) {
+    test.expect(3);
+
+    var source = {};
+    var symbol1 = nativeSymbol('the first symbol');
+    var symbol2 = nativeSymbol('the second symbol');
+    var symbol3 = nativeSymbol('the third symbol');
+    source[symbol1] = 1;
+    source[symbol2] = 2;
+    source[symbol3] = 3;
+    Object.defineProperty(source, symbol2, {
+      enumerable: false
+    });
+
+    var cloned = clone(source);
+    test.equal(cloned[symbol1], 1);
+    test.equal(cloned.hasOwnProperty(symbol2), false);
+    test.equal(cloned[symbol3], 3);
+
+    test.done();
+  };
+}
+
+exports["clone should ignore non-enumerable properties by default"] = function (test) {
+  test.expect(5);
+
+  var nativeSymbol;
+  try {
+    nativeSymbol = Symbol
+  } catch(_) {
+    nativeSymbol = function(id) {
+      return '__symbol__:' + id
+    }
+  }
+
+  var source = {
+    x: 1,
+    y: 2
+  };
+  Object.defineProperty(source, 'y', {
+    enumerable: false
+  });
+  Object.defineProperty(source, 'z', {
+    value: 3
+  });
+  var symbol1 = nativeSymbol('a');
+  var symbol2 = nativeSymbol('b');
+  source[symbol1] = 4;
+  source[symbol2] = 5;
+  Object.defineProperty(source, symbol2, {
+    enumerable: false
+  });
+
+  var cloned = clone(source);
+  test.equal(cloned.x, 1);
+  test.equal(Object.hasOwnProperty(cloned, 'y'), false);
+  test.equal(Object.hasOwnProperty(cloned, 'z'), false);
+  test.equal(cloned[symbol1], 4);
+  test.equal(Object.hasOwnProperty(cloned, symbol2), false);
+
+  test.done();
+};
+
+exports["clone should support cloning non-enumerable properties"] = function (test) {
+  test.expect(6);
+
+  var nativeSymbol;
+  try {
+    nativeSymbol = Symbol
+  } catch(_) {
+    nativeSymbol = function(id) {
+      return '__symbol__:' + id
+    }
+  }
+
+  var source = { x: 1, b: [2] };
+  Object.defineProperty(source, 'b', {
+    enumerable: false
+  });
+  var symbol = nativeSymbol('a');
+  source[symbol] = { x: 3 };
+  Object.defineProperty(source, symbol, {
+    enumerable: false
+  });
+
+  var cloned = clone(source, false, Infinity, undefined, true);
+  test.equal(cloned.x, 1);
+  test.equal(cloned.b instanceof Array, true);
+  test.equal(cloned.b.length, 1);
+  test.equal(cloned.b[0], 2);
+  test.equal(cloned[symbol] instanceof Object, true);
+  test.equal(cloned[symbol].x, 3);
+
+  test.done();
+};
+
+exports["clone should allow enabling the cloning of non-enumerable properties via an options object"] = function (test) {
+  test.expect(1);
+
+  var source = { x: 1 };
+  Object.defineProperty(source, 'x', {
+    enumerable: false
+  });
+
+  var cloned = clone(source, {
+    includeNonEnumerable: true
+  });
+  test.equal(cloned.x, 1);
+
+  test.done();
+};
+
+exports["clone should mark the cloned non-enumerable properties as non-enumerable"] = function (test) {
+  test.expect(4);
+
+  var nativeSymbol;
+  try {
+    nativeSymbol = Symbol
+  } catch(_) {
+    nativeSymbol = function(id) {
+      return '__symbol__:' + id
+    }
+  }
+
+  var source = { x: 1, y: 2 };
+  Object.defineProperty(source, 'y', {
+    enumerable: false
+  });
+  var symbol1 = nativeSymbol('a');
+  var symbol2 = nativeSymbol('b');
+  source[symbol1] = 3;
+  source[symbol2] = 4;
+  Object.defineProperty(source, symbol2, {
+    enumerable: false
+  });
+
+  var cloned = clone(source, {
+    includeNonEnumerable: true
+  });
+  test.equal(Object.getOwnPropertyDescriptor(cloned, 'x').enumerable, true);
+  test.equal(Object.getOwnPropertyDescriptor(cloned, 'y').enumerable, false);
+  test.equal(
+    Object.getOwnPropertyDescriptor(cloned, symbol1).enumerable,
+    true
+  );
+  test.equal(
+    Object.getOwnPropertyDescriptor(cloned, symbol2).enumerable,
+    false
+  );
+
+  test.done();
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-clone.git



More information about the Pkg-javascript-commits mailing list