[Pkg-javascript-commits] [node-clone] 01/06: New upstream version 2.0.0

Julien Puydt julien.puydt at laposte.net
Sun Oct 2 14:11:50 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 7fe609cfa0dc75d65d9ebd101845020875e68232
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun Oct 2 15:52:09 2016 +0200

    New upstream version 2.0.0
---
 .gitignore   |   1 +
 .npmignore   |   5 +-
 .travis.yml  |   5 +-
 README.md    |   8 ++--
 clone.js     |  85 ++++++++++++++++++++++++++++++----
 package.json |   6 ++-
 test.js      | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 7 files changed, 240 insertions(+), 18 deletions(-)

diff --git a/.gitignore b/.gitignore
index c2658d7..d4ede7c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 node_modules/
+*.iml
diff --git a/.npmignore b/.npmignore
index c2658d7..2ff84f0 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1 +1,4 @@
-node_modules/
+/node_modules/
+/test.js
+/.travis.yml
+*.html
diff --git a/.travis.yml b/.travis.yml
index 20fd86b..aa73da6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,3 +1,6 @@
 language: node_js
 node_js:
-  - 0.10
+  - "0.10"
+  - "0.12"
+  - "4"
+  - "iojs"
diff --git a/README.md b/README.md
index 0b6ceca..1c7ec78 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,8 @@
 # clone
 
-[![build status](https://secure.travis-ci.org/pvorb/node-clone.png)](http://travis-ci.org/pvorb/node-clone)
+[![build status](https://secure.travis-ci.org/pvorb/node-clone.svg)](http://travis-ci.org/pvorb/node-clone)
 
-[![info badge](https://nodei.co/npm/clone.png?downloads=true&downloadRank=true&stars=true)](http://npm-stat.com/charts.html?package=clone)
+[![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.
 
@@ -11,7 +11,9 @@ offers foolproof _deep cloning_ of objects, arrays, numbers, strings etc. in Jav
 
     npm install clone
 
-(It also works with browserify, ender or standalone.)
+(It also works with browserify, ender or standalone. You may want to use the
+option `noParse` in browserify to reduce the resulting file size, since usually
+`Buffer`s are not needed in browsers.)
 
 
 ## Example
diff --git a/clone.js b/clone.js
index 6263759..64fdd79 100644
--- a/clone.js
+++ b/clone.js
@@ -1,6 +1,29 @@
 var clone = (function() {
 'use strict';
 
+var nativeMap;
+try {
+  nativeMap = Map;
+} catch(_) {
+  // maybe a reference error because no `Map`. Give it a dummy value that no
+  // value will ever be an instanceof.
+  nativeMap = function() {};
+}
+
+var nativeSet;
+try {
+  nativeSet = Set;
+} catch(_) {
+  nativeSet = function() {};
+}
+
+var nativePromise;
+try {
+  nativePromise = Promise;
+} catch(_) {
+  nativePromise = function() {};
+}
+
 /**
  * Clones (copies) an Object using deep copying.
  *
@@ -25,7 +48,7 @@ function clone(parent, circular, depth, prototype) {
     depth = circular.depth;
     prototype = circular.prototype;
     filter = circular.filter;
-    circular = circular.circular
+    circular = circular.circular;
   }
   // maintain two arrays for circular references, where corresponding parents
   // and children have the same index
@@ -46,7 +69,7 @@ function clone(parent, circular, depth, prototype) {
     if (parent === null)
       return null;
 
-    if (depth == 0)
+    if (depth === 0)
       return parent;
 
     var child;
@@ -55,7 +78,19 @@ function clone(parent, circular, depth, prototype) {
       return parent;
     }
 
-    if (clone.__isArray(parent)) {
+    if (parent instanceof nativeMap) {
+      child = new nativeMap();
+    } else if (parent instanceof nativeSet) {
+      child = new nativeSet();
+    } else if (parent instanceof nativePromise) {
+      child = new nativePromise(function (resolve, reject) {
+        parent.then(function(value) {
+          resolve(_clone(value, depth - 1));
+        }, function(err) {
+          reject(_clone(err, depth - 1));
+        });
+      });
+    } else if (clone.__isArray(parent)) {
       child = [];
     } else if (clone.__isRegExp(parent)) {
       child = new RegExp(parent.source, __getRegExpFlags(parent));
@@ -87,6 +122,30 @@ function clone(parent, circular, depth, prototype) {
       allChildren.push(child);
     }
 
+    if (parent instanceof nativeMap) {
+      var keyIterator = parent.keys();
+      while(true) {
+        var next = keyIterator.next();
+        if (next.done) {
+          break;
+        }
+        var keyChild = _clone(next.value, depth - 1);
+        var valueChild = _clone(parent.get(next.value), depth - 1);
+        child.set(keyChild, valueChild);
+      }
+    }
+    if (parent instanceof nativeSet) {
+      var iterator = parent.keys();
+      while(true) {
+        var next = iterator.next();
+        if (next.done) {
+          break;
+        }
+        var entryChild = _clone(next.value, depth - 1);
+        child.add(entryChild);
+      }
+    }
+
     for (var i in parent) {
       var attrs;
       if (proto) {
@@ -99,6 +158,16 @@ function clone(parent, circular, depth, prototype) {
       child[i] = _clone(parent[i], depth - 1);
     }
 
+    if (Object.getOwnPropertySymbols) {
+      var symbols = Object.getOwnPropertySymbols(parent);
+      for (var i = 0; i < symbols.length; i++) {
+        // Don't need to worry about cloning a symbol because it is a primitive,
+        // like a number or string.
+        var symbol = symbols[i];
+        child[symbol] = _clone(parent[symbol], depth - 1);
+      }
+    }
+
     return child;
   }
 
@@ -125,22 +194,22 @@ clone.clonePrototype = function clonePrototype(parent) {
 
 function __objToStr(o) {
   return Object.prototype.toString.call(o);
-};
+}
 clone.__objToStr = __objToStr;
 
 function __isDate(o) {
   return typeof o === 'object' && __objToStr(o) === '[object Date]';
-};
+}
 clone.__isDate = __isDate;
 
 function __isArray(o) {
   return typeof o === 'object' && __objToStr(o) === '[object Array]';
-};
+}
 clone.__isArray = __isArray;
 
 function __isRegExp(o) {
   return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
-};
+}
 clone.__isRegExp = __isRegExp;
 
 function __getRegExpFlags(re) {
@@ -149,7 +218,7 @@ function __getRegExpFlags(re) {
   if (re.ignoreCase) flags += 'i';
   if (re.multiline) flags += 'm';
   return flags;
-};
+}
 clone.__getRegExpFlags = __getRegExpFlags;
 
 return clone;
diff --git a/package.json b/package.json
index b0987ec..16158e5 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "function",
     "date"
   ],
-  "version": "1.0.2",
+  "version": "2.0.0",
   "repository": {
     "type": "git",
     "url": "git://github.com/pvorb/node-clone.git"
@@ -34,7 +34,9 @@
     "Nathan Zadoks (https://github.com/nathan7)",
     "Róbert Oroszi <robert+gh at oroszi.net> (https://github.com/oroce)",
     "Aurélio A. Heckert (http://softwarelivre.org/aurium)",
-    "Guy Ellis (http://www.guyellisrocks.com/)"
+    "Guy Ellis (http://www.guyellisrocks.com/)",
+    "fscherwi (https://fscherwi.github.io)",
+    "rictic (https://github.com/rictic)"
   ],
   "license": "MIT",
   "engines": {
diff --git a/test.js b/test.js
index e8b65b3..00c2879 100644
--- a/test.js
+++ b/test.js
@@ -3,7 +3,7 @@ var clone = require('./');
 function inspect(obj) {
   seen = [];
   return JSON.stringify(obj, function (key, val) {
-    if (val != null && typeof val == "object") {
+    if (val !== null && typeof val == "object") {
       if (seen.indexOf(val) >= 0) {
         return '[cyclic]';
       }
@@ -75,7 +75,7 @@ exports["clone number"] = function (test) {
 exports["clone date"] = function (test) {
   test.expect(3); // how many tests?
 
-  var a = new Date;
+  var a = new Date ();
   var c = clone(a);
   test.ok(!!a.getUTCDate && !!a.toUTCString);
   test.ok(!!c.getUTCDate && !!c.toUTCString);
@@ -303,7 +303,7 @@ exports['clone object with null children'] = function (test) {
 
 exports['clone instance with getter'] = function (test) {
   test.expect(1);
-  function Ctor() {};
+  function Ctor() {}
   Object.defineProperty(Ctor.prototype, 'prop', {
     configurable: true,
     enumerable: true,
@@ -319,6 +319,33 @@ exports['clone instance with getter'] = function (test) {
   test.done();
 };
 
+if (Object.getOwnPropertySymbols) {
+  exports['clone object with symbol properties'] = function (test) {
+    var symbol = Symbol();
+    var obj = {};
+    obj[symbol] = 'foo';
+
+    var child = clone(obj);
+
+    test.notEqual(child, obj);
+    test.equal(child[symbol], 'foo');
+
+    test.done();
+  }
+
+  exports['symbols are treated as primitives'] = function (test) {
+    var symbol = Symbol();
+    var obj = {foo: symbol};
+
+    var child = clone(obj);
+
+    test.notEqual(child, obj);
+    test.equal(child.foo, obj.foo);
+
+    test.done();
+  }
+}
+
 exports['get RegExp flags'] = function (test) {
   test.strictEqual(clone.__getRegExpFlags(/a/),   ''  );
   test.strictEqual(clone.__getRegExpFlags(/a/i),  'i' );
@@ -370,3 +397,118 @@ exports["recognize RegExp object"] = function (test) {
     test.done();
   });
 };
+
+var nativeMap;
+try {
+  nativeMap = Map;
+} catch(_) {}
+if (nativeMap) {
+  exports["clone a native Map"] = function (test) {
+    var map = new Map();
+    // simple key/value
+    map.set('foo', 'bar');
+    // circular object key/property
+    map.set(map, map);
+    // regular expando property
+    map.bar = 'baz';
+    // regular circular expando property
+    map.circle = map;
+
+
+    var clonedMap = clone(map);
+    test.notEqual(map, clonedMap);
+    test.equal(clonedMap.get('foo'), 'bar');
+    test.equal(clonedMap.get(clonedMap), clonedMap);
+    test.equal(clonedMap.bar, 'baz');
+    test.equal(clonedMap.circle, clonedMap);
+
+    test.done();
+  }
+}
+
+var nativeSet;
+try {
+  nativeSet = Set;
+} catch(_) {}
+if (nativeSet) {
+  exports["clone a native Set"] = function (test) {
+    var set = new Set();
+    // simple entry
+    set.add('foo');
+    // circular entry
+    set.add(set);
+    // regular expando property
+    set.bar = 'baz';
+    // regular circular expando property
+    set.circle = set;
+
+
+    var clonedSet = clone(set);
+    test.notEqual(set, clonedSet);
+    test.ok(clonedSet.has('foo'));
+    test.ok(clonedSet.has(clonedSet));
+    test.ok(!clonedSet.has(set));
+    test.equal(clonedSet.bar, 'baz');
+    test.equal(clonedSet.circle, clonedSet);
+
+    test.done();
+  }
+}
+
+var nativePromise;
+try {
+  nativePromise = Promise;
+} catch(_) {}
+if (nativePromise) {
+  exports["clone a native Promise"] = function (test) {
+    test.expect(9);
+
+    var allDonePromises = [];
+
+    // Resolving to a value
+    allDonePromises.push(
+      clone(Promise.resolve('foo')).then(function (value) {
+        test.equal(value, 'foo');
+      })
+    );
+
+    // Rejecting to a value
+    allDonePromises.push(
+      clone(Promise.reject('bar')).catch(function (value) {
+        test.equal(value, 'bar');
+      })
+    );
+
+    // Resolving to a promise
+    allDonePromises.push(
+      clone(Promise.resolve(Promise.resolve('baz'))).then(function (value) {
+        test.equal(value, 'baz');
+      })
+    );
+
+    // Resolving to a circular value
+    var circle = {};
+    circle.circle = circle;
+    allDonePromises.push(
+      clone(Promise.resolve(circle)).then(function (value) {
+        test.notEqual(circle, value);
+        test.equal(value.circle, value);
+      })
+    );
+
+    var expandoPromise = Promise.resolve('ok');
+    expandoPromise.circle = expandoPromise;
+    expandoPromise.prop = 'val';
+    var clonedPromise = clone(expandoPromise);
+    test.notEqual(expandoPromise, clonedPromise);
+    test.equal(clonedPromise.prop, 'val');
+    test.equal(clonedPromise.circle, clonedPromise);
+    allDonePromises.push(clonedPromise.then(function(value) {
+      test.equal(value, 'ok');
+    }));
+
+    Promise.all(allDonePromises).then(function() {
+      test.done();
+    });
+  }
+}
\ No newline at end of file

-- 
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