pax_global_header00006660000000000000000000000064132545445200014516gustar00rootroot0000000000000052 comment=765941803d27761a6008cb1aa32b199f0c42deb2 clone-2.1.2/000077500000000000000000000000001325454452000126205ustar00rootroot00000000000000clone-2.1.2/.gitignore000066400000000000000000000000321325454452000146030ustar00rootroot00000000000000node_modules/ *.iml .idea clone-2.1.2/.npmignore000066400000000000000000000000541325454452000146160ustar00rootroot00000000000000/node_modules/ /test.js /.travis.yml *.html clone-2.1.2/.travis.yml000066400000000000000000000001371325454452000147320ustar00rootroot00000000000000language: node_js node_js: - "0.10" - "0.12" - "4" - "5" - "6" - "node" - "iojs" clone-2.1.2/LICENSE000066400000000000000000000020561325454452000136300ustar00rootroot00000000000000Copyright © 2011-2015 Paul Vorbach Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clone-2.1.2/README.md000066400000000000000000000120671325454452000141050ustar00rootroot00000000000000# clone [![build status](https://secure.travis-ci.org/pvorb/clone.svg)](http://travis-ci.org/pvorb/clone) [![downloads](https://img.shields.io/npm/dt/clone.svg)](http://npm-stat.com/charts.html?package=clone) offers foolproof _deep cloning_ of objects, arrays, numbers, strings, maps, sets, promises, etc. in JavaScript. **XSS vulnerability detected** ## Installation npm install clone (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 ~~~ javascript var clone = require('clone'); var a, b; a = { foo: { bar: 'baz' } }; // initial value of a b = clone(a); // clone a -> b a.foo.bar = 'foo'; // change a console.log(a); // show a console.log(b); // show b ~~~ This will print: ~~~ javascript { foo: { bar: 'foo' } } { foo: { bar: 'baz' } } ~~~ **clone** masters cloning simple objects (even with custom prototype), arrays, Date objects, and RegExp objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example. ## API `clone(val, circular, depth)` * `val` -- the value that you want to clone, any type allowed * `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`. * `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)` * `obj` -- the object that you want to clone Does a prototype clone as [described by Oran Looney](http://oranlooney.com/functional-javascript/). ## Circular References ~~~ javascript var a, b; a = { hello: 'world' }; a.myself = a; b = clone(a); console.log(b); ~~~ This will print: ~~~ javascript { hello: "world", myself: [Circular] } ~~~ So, `b.myself` points to `b`, not `a`. Neat! ## Test npm test ## Changelog ### v2.1.2 #### 2018-03-21 - Use `Buffer.allocUnsafe()` on Node >= 4.5.0 (contributed by @ChALkeR) ### v2.1.1 #### 2017-03-09 - Fix build badge in README - Add support for cloning Maps and Sets on Internet Explorer ### 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.3 #### 2017-11-08 - Close XSS vulnerability in the NPM package, which included the file `test-apart-ctx.html`. This vulnerability was disclosed by Juho Nurminen of 2NS - Second Nature Security. ### v1.0.2 (deprecated) #### 2015-03-25 - Fix call on getRegExpFlags - Refactor utilities - Refactor test suite ### v1.0.1 (deprecated) #### 2015-03-04 - Fix nodeunit version - Directly call getRegExpFlags ### v1.0.0 (deprecated) #### 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 be cloneable. If you find other objects that cannot be cloned, please [open an issue](https://github.com/pvorb/clone/issues/new). ## Bugs and Issues If you encounter any bugs or issues, feel free to [open an issue at github](https://github.com/pvorb/clone/issues) or send me an email to . I also always like to hear from you, if you’re using my code. ## License Copyright © 2011-2016 [Paul Vorbach](https://paul.vorba.ch/) and [contributors](https://github.com/pvorb/clone/graphs/contributors). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clone-2.1.2/clone.js000066400000000000000000000161751325454452000142700ustar00rootroot00000000000000var clone = (function() { 'use strict'; function _instanceof(obj, type) { return type != null && obj instanceof type; } 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. * * This function supports circular references by default, but if you are certain * there are no circular references in your object, you can save some CPU time * by calling clone(obj, false). * * Caution: if `circular` is false and `parent` contains circular references, * your program may enter an infinite loop and crash. * * @param `parent` - the object to be cloned * @param `circular` - set to true if the object to be cloned may contain * circular references. (optional - true by default) * @param `depth` - set to a number if the object is only to be cloned to * 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, includeNonEnumerable) { if (typeof circular === 'object') { depth = circular.depth; prototype = circular.prototype; includeNonEnumerable = circular.includeNonEnumerable; circular = circular.circular; } // maintain two arrays for circular references, where corresponding parents // and children have the same index var allParents = []; var allChildren = []; var useBuffer = typeof Buffer != 'undefined'; if (typeof circular == 'undefined') circular = true; if (typeof depth == 'undefined') depth = Infinity; // recurse this function so we don't reset allParents and allChildren function _clone(parent, depth) { // cloning null always returns null if (parent === null) return null; if (depth === 0) return parent; var child; var proto; if (typeof parent != 'object') { return parent; } if (_instanceof(parent, nativeMap)) { child = new nativeMap(); } else if (_instanceof(parent, nativeSet)) { child = new nativeSet(); } else if (_instanceof(parent, 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)); if (parent.lastIndex) child.lastIndex = parent.lastIndex; } else if (clone.__isDate(parent)) { child = new Date(parent.getTime()); } else if (useBuffer && Buffer.isBuffer(parent)) { if (Buffer.allocUnsafe) { // Node.js >= 4.5.0 child = Buffer.allocUnsafe(parent.length); } else { // Older Node.js versions child = new Buffer(parent.length); } parent.copy(child); return child; } else if (_instanceof(parent, Error)) { child = Object.create(parent); } else { if (typeof prototype == 'undefined') { proto = Object.getPrototypeOf(parent); child = Object.create(proto); } else { child = Object.create(prototype); proto = prototype; } } if (circular) { var index = allParents.indexOf(parent); if (index != -1) { return allChildren[index]; } allParents.push(parent); allChildren.push(child); } if (_instanceof(parent, nativeMap)) { parent.forEach(function(value, key) { var keyChild = _clone(key, depth - 1); var valueChild = _clone(value, depth - 1); child.set(keyChild, valueChild); }); } if (_instanceof(parent, nativeSet)) { parent.forEach(function(value) { var entryChild = _clone(value, depth - 1); child.add(entryChild); }); } for (var i in parent) { var attrs; if (proto) { attrs = Object.getOwnPropertyDescriptor(proto, i); } if (attrs && attrs.set == null) { continue; } 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]; 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 }); } } return child; } return _clone(parent, depth); } /** * Simple flat clone using prototype, accepts only objects, usefull for property * override on FLAT configuration object (no nested props). * * USE WITH CAUTION! This may not behave as you wish if you do not know how this * works. */ clone.clonePrototype = function clonePrototype(parent) { if (parent === null) return null; var c = function () {}; c.prototype = parent; return new c(); }; // private utility functions 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) { var flags = ''; if (re.global) flags += 'g'; if (re.ignoreCase) flags += 'i'; if (re.multiline) flags += 'm'; return flags; } clone.__getRegExpFlags = __getRegExpFlags; return clone; })(); if (typeof module === 'object' && module.exports) { module.exports = clone; } clone-2.1.2/package.json000066400000000000000000000035361325454452000151150ustar00rootroot00000000000000{ "name": "clone", "description": "deep cloning of objects and arrays", "tags": [ "clone", "object", "array", "function", "date" ], "version": "2.1.2", "repository": { "type": "git", "url": "git://github.com/pvorb/node-clone.git" }, "bugs": { "url": "https://github.com/pvorb/node-clone/issues" }, "main": "clone.js", "author": "Paul Vorbach (http://paul.vorba.ch/)", "contributors": [ "Blake Miner (http://www.blakeminer.com/)", "Tian You (http://blog.axqd.net/)", "George Stagas (http://stagas.com/)", "Tobiasz Cudnik (https://github.com/TobiaszCudnik)", "Pavel Lang (https://github.com/langpavel)", "Dan MacTough (http://yabfog.com/)", "w1nk (https://github.com/w1nk)", "Hugh Kennedy (http://twitter.com/hughskennedy)", "Dustin Diaz (http://dustindiaz.com)", "Ilya Shaisultanov (https://github.com/diversario)", "Nathan MacInnes (http://macinn.es/)", "Benjamin E. Coe (https://twitter.com/benjamincoe)", "Nathan Zadoks (https://github.com/nathan7)", "Róbert Oroszi (https://github.com/oroce)", "Aurélio A. Heckert (http://softwarelivre.org/aurium)", "Guy Ellis (http://www.guyellisrocks.com/)", "fscherwi (https://fscherwi.github.io)", "rictic (https://github.com/rictic)", "Martin Jurča (https://github.com/jurca)", "Misery Lee (https://github.com/miserylee)", "Clemens Wolff (https://github.com/c-w)" ], "license": "MIT", "engines": { "node": ">=0.8" }, "dependencies": {}, "devDependencies": { "nodeunit": "~0.9.0" }, "optionalDependencies": {}, "scripts": { "test": "nodeunit test.js" } } clone-2.1.2/test-apart-ctx.html000066400000000000000000000011101325454452000163570ustar00rootroot00000000000000 Clone Test-Suite (Browser) clone-2.1.2/test.html000066400000000000000000000107071325454452000144720ustar00rootroot00000000000000 Clone Test-Suite (Browser)

Clone Test-Suite (Browser)

Tests started: ; Tests finished: .
    clone-2.1.2/test.js000066400000000000000000000360671325454452000141510ustar00rootroot00000000000000var clone = require('./'); function inspect(obj) { seen = []; return JSON.stringify(obj, function (key, val) { if (val !== null && typeof val == "object") { if (seen.indexOf(val) >= 0) { return '[cyclic]'; } seen.push(val); } return val; }); } // Creates a new VM in node, or an iframe in a browser in order to run the // script function apartContext(context, script, callback) { var vm = require('vm'); if (vm) { var ctx = vm.createContext({ ctx: context }); callback(vm.runInContext(script, ctx)); } else if (document && document.createElement) { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; document.body.appendChild(iframe); var myCtxId = 'tmpCtx' + Math.random(); window[myCtxId] = context; iframe.src = 'test-apart-ctx.html?' + myCtxId + '&' + encodeURIComponent(script); iframe.onload = function() { try { callback(iframe.contentWindow.results); } catch (e) { throw e; } }; } else { console.log('WARNING: cannot create an apart context.'); } } exports["clone string"] = function (test) { test.expect(2); // how many tests? var a = "foo"; test.strictEqual(clone(a), a); a = ""; test.strictEqual(clone(a), a); test.done(); }; exports["clone number"] = function (test) { test.expect(5); // how many tests? var a = 0; test.strictEqual(clone(a), a); a = 1; test.strictEqual(clone(a), a); a = -1000; test.strictEqual(clone(a), a); a = 3.1415927; test.strictEqual(clone(a), a); a = -3.1415927; test.strictEqual(clone(a), a); test.done(); }; exports["clone date"] = function (test) { test.expect(3); // how many tests? var a = new Date (); var c = clone(a); test.ok(!!a.getUTCDate && !!a.toUTCString); test.ok(!!c.getUTCDate && !!c.toUTCString); test.equal(a.getTime(), c.getTime()); test.done(); }; exports["clone object"] = function (test) { test.expect(1); // how many tests? var a = { foo: { bar: "baz" } }; var b = clone(a); test.deepEqual(b, a); 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? var a = [ { foo: "bar" }, "baz" ]; var b = clone(a); test.ok(b instanceof Array); test.deepEqual(b, a); test.done(); }; exports["clone buffer"] = function (test) { if (typeof Buffer == 'undefined') { return test.done(); } test.expect(1); var a = new Buffer("this is a test buffer"); var b = clone(a); // no underscore equal since it has no concept of Buffers test.deepEqual(b, a); test.done(); }; exports["clone regexp"] = function (test) { test.expect(5); var a = /abc123/gi; var b = clone(a); test.deepEqual(b, a); var c = /a/g; test.ok(c.lastIndex === 0); c.exec('123a456a'); test.ok(c.lastIndex === 4); var d = clone(c); test.ok(d.global); test.ok(d.lastIndex === 4); test.done(); }; exports["clone object containing array"] = function (test) { test.expect(1); // how many tests? var a = { arr1: [ { a: '1234', b: '2345' } ], arr2: [ { c: '345', d: '456' } ] }; var b = clone(a); test.deepEqual(b, a); test.done(); }; exports["clone object with circular reference"] = function (test) { test.expect(8); // how many tests? var c = [1, "foo", {'hello': 'bar'}, function () {}, false, [2]]; var b = [c, 2, 3, 4]; var a = {'b': b, 'c': c}; a.loop = a; a.loop2 = a; c.loop = c; c.aloop = a; var aCopy = clone(a); test.ok(a != aCopy); test.ok(a.c != aCopy.c); test.ok(aCopy.c == aCopy.b[0]); test.ok(aCopy.c.loop.loop.aloop == aCopy); test.ok(aCopy.c[0] == a.c[0]); test.ok(eq(a, aCopy)); aCopy.c[0] = 2; test.ok(!eq(a, aCopy)); aCopy.c = "2"; test.ok(!eq(a, aCopy)); function eq(x, y) { return inspect(x) === inspect(y); } test.done(); }; exports['clone prototype'] = function (test) { test.expect(3); // how many tests? var a = { a: "aaa", x: 123, y: 45.65 }; var b = clone.clonePrototype(a); test.strictEqual(b.a, a.a); test.strictEqual(b.x, a.x); test.strictEqual(b.y, a.y); test.done(); }; exports['clone within an apart context'] = function (test) { var results = apartContext({ clone: clone }, "results = ctx.clone({ a: [1, 2, 3], d: new Date(), r: /^foo$/ig })", function (results) { test.ok(results.a.constructor.toString() === Array.toString()); test.ok(results.d.constructor.toString() === Date.toString()); test.ok(results.r.constructor.toString() === RegExp.toString()); test.done(); }); }; exports['clone object with no constructor'] = function (test) { test.expect(3); var n = null; var a = { foo: 'bar' }; a.__proto__ = n; test.ok(typeof a === 'object'); test.ok(typeof a !== null); var b = clone(a); test.ok(a.foo, b.foo); test.done(); }; exports['clone object with depth argument'] = function (test) { test.expect(6); var a = { foo: { bar : { baz : 'qux' } } }; var b = clone(a, false, 1); test.deepEqual(b, a); test.notEqual(b, a); test.strictEqual(b.foo, a.foo); b = clone(a, true, 2); test.deepEqual(b, a); test.notEqual(b.foo, a.foo); test.strictEqual(b.foo.bar, a.foo.bar); test.done(); }; exports['maintain prototype chain in clones'] = function (test) { test.expect(1); function T() {} var a = new T(); var b = clone(a); test.strictEqual(Object.getPrototypeOf(a), Object.getPrototypeOf(b)); test.done(); }; exports['parent prototype is overriden with prototype provided'] = function (test) { test.expect(1); function T() {} var a = new T(); var b = clone(a, true, Infinity, null); test.strictEqual(b.__defineSetter__, undefined); test.done(); }; exports['clone object with null children'] = function (test) { test.expect(1); var a = { foo: { bar: null, baz: { qux: false } } }; var b = clone(a); test.deepEqual(b, a); test.done(); }; exports['clone instance with getter'] = function (test) { test.expect(1); function Ctor() {} Object.defineProperty(Ctor.prototype, 'prop', { configurable: true, enumerable: true, get: function() { return 'value'; } }); var a = new Ctor(); var b = clone(a); test.strictEqual(b.prop, 'value'); 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' ); test.strictEqual(clone.__getRegExpFlags(/a/g), 'g' ); test.strictEqual(clone.__getRegExpFlags(/a/gi), 'gi'); test.strictEqual(clone.__getRegExpFlags(/a/m), 'm' ); test.done(); }; exports["recognize Array object"] = function (test) { var results = apartContext(null, "results = [1, 2, 3]", function(alien) { var local = [4, 5, 6]; test.ok(clone.__isArray(alien)); // recognize in other context. test.ok(clone.__isArray(local)); // recognize in local context. test.ok(!clone.__isDate(alien)); test.ok(!clone.__isDate(local)); test.ok(!clone.__isRegExp(alien)); test.ok(!clone.__isRegExp(local)); test.done(); }); }; exports["recognize Date object"] = function (test) { var results = apartContext(null, "results = new Date()", function(alien) { var local = new Date(); test.ok(clone.__isDate(alien)); // recognize in other context. test.ok(clone.__isDate(local)); // recognize in local context. test.ok(!clone.__isArray(alien)); test.ok(!clone.__isArray(local)); test.ok(!clone.__isRegExp(alien)); test.ok(!clone.__isRegExp(local)); test.done(); }); }; exports["recognize RegExp object"] = function (test) { var results = apartContext(null, "results = /foo/", function(alien) { var local = /bar/; test.ok(clone.__isRegExp(alien)); // recognize in other context. test.ok(clone.__isRegExp(local)); // recognize in local context. test.ok(!clone.__isArray(alien)); test.ok(!clone.__isArray(local)); test.ok(!clone.__isDate(alien)); test.ok(!clone.__isDate(local)); 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(); }); } } 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(); };