Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-copy

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-copy - npm Package Compare versions

Comparing version 1.1.2 to 1.2.0

4

CHANGELOG.md
# fast-copy CHANGELOG
## 1.2.0
* Add support for multiple realms
## 1.1.2

@@ -4,0 +8,0 @@

101

dist/fast-copy.js

@@ -8,34 +8,10 @@ (function (global, factory) {

/**
* @constant {boolean} HAS_ARRAYBUFFER_SUPPORT
*/
var HAS_ARRAYBUFFER_SUPPORT = typeof ArrayBuffer === 'function';
/**
* @constant {boolean} HAS_BUFFER_SUPPORT
*/
var HAS_BUFFER_SUPPORT = typeof Buffer === 'function';
/**
* @constant {boolean} HAS_MAP_SUPPORT
*/
var HAS_MAP_SUPPORT = typeof Map === 'function';
/**
* @constant {boolean} HAS_PROPERTY_SYMBOL_SUPPORT
*/
var HAS_PROPERTY_SYMBOL_SUPPORT = typeof Object.getOwnPropertySymbols === 'function';
var HAS_PROPERTY_SYMBOL_SUPPORT = typeof global.Object.getOwnPropertySymbols === 'function';
/**
* @constant {boolean} HAS_SET_SUPPORT
*/
var HAS_SET_SUPPORT = typeof Set === 'function';
/**
* @constant {boolean} HAS_WEAKMAP_SUPPORT
*/
var HAS_WEAKMAP_SUPPORT = typeof WeakMap === 'function';
/**
* @constant {boolean} HAS_WEAKSET_SUPPORT
*/
var HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function';
var HAS_WEAKSET_SUPPORT = typeof global.WeakSet === 'function';

@@ -129,6 +105,7 @@ // constants

* @param {any} object the object to test
* @param {any} realm the realm to check instanceof in
* @returns {boolean} should the object be copied
*/
var shouldObjectBeCopied = function shouldObjectBeCopied(object) {
return typeof object.then !== 'function' && !(object instanceof Error) && !(HAS_WEAKMAP_SUPPORT && object instanceof WeakMap) && !(HAS_WEAKSET_SUPPORT && object instanceof WeakSet);
var shouldObjectBeCopied = function shouldObjectBeCopied(object, realm) {
return typeof object.then !== 'function' && !(object instanceof realm.Error) && !(realm.WeakMap && object instanceof realm.WeakMap) && !(realm.WeakSet && object instanceof realm.WeakSet);
};

@@ -144,9 +121,10 @@

* @param {function} copy the function to copy values
* @param {any} realm the realm to check instanceof in
* @returns {Array<any>} the copied array
*/
var copyArray = function copyArray(array, copy) {
var copyArray = function copyArray(array, copy, realm) {
var newArray = new array.constructor();
for (var index = 0; index < array.length; index++) {
newArray.push(copy(array[index]));
newArray.push(copy(array[index], realm));
}

@@ -177,6 +155,7 @@

* @param {Buffer} buffer the buffer to copy
* @param {any} realm the realm to check instanceof in
* @returns {Buffer} the copied buffer
*/
var copyBuffer = function copyBuffer(buffer) {
var newBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe(buffer.length) : new Buffer(buffer.length);
var copyBuffer = function copyBuffer(buffer, realm) {
var newBuffer = realm.Buffer.allocUnsafe ? realm.Buffer.allocUnsafe(buffer.length) : new realm.Buffer(buffer.length);

@@ -196,6 +175,7 @@ buffer.copy(newBuffer);

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isMap is the iterable a map
* @returns {Map|Set} the copied iterable
*/
var copyIterable = function copyIterable(iterable, copy, isMap) {
var copyIterable = function copyIterable(iterable, copy, realm, isMap) {
var newIterable = new iterable.constructor();

@@ -205,5 +185,5 @@

if (isMap) {
newIterable.set(key, copy(value));
newIterable.set(key, copy(value, realm));
} else {
newIterable.add(copy(value));
newIterable.add(copy(value, realm));
}

@@ -223,6 +203,7 @@ });

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isPlainObject is the object to copy a plain object
* @returns {Object} the copied object
*/
var copyObject = function copyObject(object, copy, isPlainObject) {
var copyObject = function copyObject(object, copy, realm, isPlainObject) {
var newObject = isPlainObject ? {} : object.constructor ? new object.constructor() : Object.create(null);

@@ -237,3 +218,3 @@ var keys = Object.keys(object);

newObject[key] = copy(object[key]);
newObject[key] = copy(object[key], realm);
}

@@ -250,3 +231,3 @@ }

newObject[symbol] = copy(object[symbol]);
newObject[symbol] = copy(object[symbol], realm);
}

@@ -265,6 +246,7 @@ }

* @param {RegExp} regExp the RegExp to copy
* @param {any} realm the realm to check instanceof in
* @returns {RegExp} the copied RegExp
*/
var copyRegExp = function copyRegExp(regExp) {
var newRegExp = new RegExp(regExp.source, getRegExpFlags(regExp));
var copyRegExp = function copyRegExp(regExp, realm) {
var newRegExp = new realm.RegExp(regExp.source, getRegExpFlags(regExp));

@@ -289,3 +271,3 @@ newRegExp.lastIndex = regExp.lastIndex;

// constants
// utils

@@ -299,5 +281,8 @@ /**

* @param {any} object the object to copy
* @param {any} [realm=global] the realm to check instanceof in
* @returns {any} the copied object
*/
function copy(object) {
var realm = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : global;
var cache = getNewCache();

@@ -313,41 +298,41 @@

return copyArray(object, handleCopy);
return copyArray(object, handleCopy, realm);
}
if (object.constructor === Object) {
if (object.constructor === realm.Object) {
cache.add(object);
return copyObject(object, handleCopy, true);
return copyObject(object, handleCopy, realm, true);
}
if (object instanceof Date) {
if (object instanceof realm.Date) {
return new Date(object.getTime());
}
if (object instanceof RegExp) {
return copyRegExp(object);
if (object instanceof realm.RegExp) {
return copyRegExp(object, realm);
}
if (HAS_MAP_SUPPORT && object instanceof Map) {
if (realm.Map && object instanceof realm.Map) {
cache.add(object);
return copyIterable(object, handleCopy, true);
return copyIterable(object, handleCopy, realm, true);
}
if (HAS_SET_SUPPORT && object instanceof Set) {
if (realm.Set && object instanceof realm.Set) {
cache.add(object);
return copyIterable(object, handleCopy, false);
return copyIterable(object, handleCopy, realm, false);
}
if (HAS_BUFFER_SUPPORT && Buffer.isBuffer(object)) {
return copyBuffer(object);
if (realm.Buffer && realm.Buffer.isBuffer(object)) {
return copyBuffer(object, realm);
}
if (HAS_ARRAYBUFFER_SUPPORT) {
if (ArrayBuffer.isView(object)) {
if (realm.ArrayBuffer) {
if (realm.ArrayBuffer.isView(object)) {
return copyTypedArray(object);
}
if (object instanceof ArrayBuffer) {
if (object instanceof realm.ArrayBuffer) {
return copyArrayBuffer(object);

@@ -357,6 +342,6 @@ }

if (shouldObjectBeCopied(object)) {
if (shouldObjectBeCopied(object, realm)) {
cache.add(object);
return copyObject(object, handleCopy);
return copyObject(object, handleCopy, realm);
}

@@ -363,0 +348,0 @@

@@ -1,1 +0,1 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.fastCopy={})}(this,function(e){"use strict";var n="function"==typeof ArrayBuffer,t="function"==typeof Buffer,r="function"==typeof Map,f="function"==typeof Object.getOwnPropertySymbols,o="function"==typeof Set,u="function"==typeof WeakMap,i="function"==typeof WeakSet,c=Object.prototype.propertyIsEnumerable,a=function(){return i?new WeakSet:Object.create({_values:[],add:function(e){this._values.push(e)},has:function(e){return!!~this._values.indexOf(e)}})},s=function(e,n){return"object"==typeof e&&null!==e&&!n.has(e)},l=function(e){return!("function"==typeof e.then||e instanceof Error||u&&e instanceof WeakMap||i&&e instanceof WeakSet)},p=function(e,n){for(var t=new e.constructor,r=0;r<e.length;r++)t.push(n(e[r]));return t},d=function(e){return e.slice()},y=function(e){var n=Buffer.allocUnsafe?Buffer.allocUnsafe(e.length):new Buffer(e.length);return e.copy(n),n},v=function(e,n,t){var r=new e.constructor;return e.forEach(function(e,f){t?r.set(f,n(e)):r.add(n(e))}),r},h=function(e,n,t){var r=t?{}:e.constructor?new e.constructor:Object.create(null),o=Object.keys(e);if(o.length)for(var u=void 0,i=0;i<o.length;i++)r[u=o[i]]=n(e[u]);var a=function(e){return f?Object.getOwnPropertySymbols(e).filter(function(n){return c.call(e,n)}):[]}(e);if(a.length)for(var s=void 0,l=0;l<a.length;l++)r[s=a[l]]=n(e[s]);return r},b=function(e){var n=new RegExp(e.source,function(e){var n="";return e.global&&(n+="g"),e.ignoreCase&&(n+="i"),e.multiline&&(n+="m"),n}(e));return n.lastIndex=e.lastIndex,n},g=function(e){return new e.constructor(e.buffer)};e.default=function(e){var f=a();return function e(u){if(!s(u,f))return u;if(Array.isArray(u))return f.add(u),p(u,e);if(u.constructor===Object)return f.add(u),h(u,e,!0);if(u instanceof Date)return new Date(u.getTime());if(u instanceof RegExp)return b(u);if(r&&u instanceof Map)return f.add(u),v(u,e,!0);if(o&&u instanceof Set)return f.add(u),v(u,e,!1);if(t&&Buffer.isBuffer(u))return y(u);if(n){if(ArrayBuffer.isView(u))return g(u);if(u instanceof ArrayBuffer)return d(u)}return l(u)?(f.add(u),h(u,e)):u}(e)},Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.fastCopy={})}(this,function(e){"use strict";var n="function"==typeof global.Object.getOwnPropertySymbols,t="function"==typeof global.WeakSet,r=Object.prototype.propertyIsEnumerable,f=function(){return t?new WeakSet:Object.create({_values:[],add:function(e){this._values.push(e)},has:function(e){return!!~this._values.indexOf(e)}})},o=function(e,n){return"object"==typeof e&&null!==e&&!n.has(e)},u=function(e,n){return!("function"==typeof e.then||e instanceof n.Error||n.WeakMap&&e instanceof n.WeakMap||n.WeakSet&&e instanceof n.WeakSet)},i=function(e,n,t){for(var r=new e.constructor,f=0;f<e.length;f++)r.push(n(e[f],t));return r},a=function(e){return e.slice()},c=function(e,n){var t=n.Buffer.allocUnsafe?n.Buffer.allocUnsafe(e.length):new n.Buffer(e.length);return e.copy(t),t},s=function(e,n,t,r){var f=new e.constructor;return e.forEach(function(e,o){r?f.set(o,n(e,t)):f.add(n(e,t))}),f},l=function(e,t,f,o){var u=o?{}:e.constructor?new e.constructor:Object.create(null),i=Object.keys(e);if(i.length)for(var a=void 0,c=0;c<i.length;c++)u[a=i[c]]=t(e[a],f);var s=function(e){return n?Object.getOwnPropertySymbols(e).filter(function(n){return r.call(e,n)}):[]}(e);if(s.length)for(var l=void 0,d=0;d<s.length;d++)u[l=s[d]]=t(e[l],f);return u},d=function(e,n){var t=new n.RegExp(e.source,function(e){var n="";return e.global&&(n+="g"),e.ignoreCase&&(n+="i"),e.multiline&&(n+="m"),n}(e));return t.lastIndex=e.lastIndex,t},p=function(e){return new e.constructor(e.buffer)};e.default=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:global,t=f();return function e(r){if(!o(r,t))return r;if(Array.isArray(r))return t.add(r),i(r,e,n);if(r.constructor===n.Object)return t.add(r),l(r,e,n,!0);if(r instanceof n.Date)return new Date(r.getTime());if(r instanceof n.RegExp)return d(r,n);if(n.Map&&r instanceof n.Map)return t.add(r),s(r,e,n,!0);if(n.Set&&r instanceof n.Set)return t.add(r),s(r,e,n,!1);if(n.Buffer&&n.Buffer.isBuffer(r))return c(r,n);if(n.ArrayBuffer){if(n.ArrayBuffer.isView(r))return p(r);if(r instanceof n.ArrayBuffer)return a(r)}return u(r,n)?(t.add(r),l(r,e,n)):r}(e)},Object.defineProperty(e,"__esModule",{value:!0})});
/**
* @constant {boolean} HAS_ARRAYBUFFER_SUPPORT
*/
export var HAS_ARRAYBUFFER_SUPPORT = typeof ArrayBuffer === 'function';
/**
* @constant {boolean} HAS_BUFFER_SUPPORT
*/
export var HAS_BUFFER_SUPPORT = typeof Buffer === 'function';
/**
* @constant {boolean} HAS_MAP_SUPPORT
*/
export var HAS_MAP_SUPPORT = typeof Map === 'function';
/**
* @constant {boolean} HAS_PROPERTY_SYMBOL_SUPPORT
*/
export var HAS_PROPERTY_SYMBOL_SUPPORT = typeof Object.getOwnPropertySymbols === 'function';
export var HAS_PROPERTY_SYMBOL_SUPPORT = typeof global.Object.getOwnPropertySymbols === 'function';
/**
* @constant {boolean} HAS_SET_SUPPORT
*/
export var HAS_SET_SUPPORT = typeof Set === 'function';
/**
* @constant {boolean} HAS_TYPEDARRAY_SUPPORT
*/
export var HAS_TYPEDARRAY_SUPPORT = typeof TypedArray === 'function';
/**
* @constant {boolean} HAS_WEAKMAP_SUPPORT
*/
export var HAS_WEAKMAP_SUPPORT = typeof WeakMap === 'function';
/**
* @constant {boolean} HAS_WEAKSET_SUPPORT
*/
export var HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function';
export var HAS_WEAKSET_SUPPORT = typeof global.WeakSet === 'function';

@@ -1,4 +0,1 @@

// constants
import { HAS_ARRAYBUFFER_SUPPORT, HAS_BUFFER_SUPPORT, HAS_MAP_SUPPORT, HAS_SET_SUPPORT } from './constants';
// utils

@@ -14,5 +11,8 @@ import { copyArray, copyArrayBuffer, copyBuffer, copyIterable, copyObject, copyRegExp, copyTypedArray, getNewCache, isObjectCopyable, shouldObjectBeCopied } from './utils';

* @param {any} object the object to copy
* @param {any} [realm=global] the realm to check instanceof in
* @returns {any} the copied object
*/
export default function copy(object) {
var realm = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : global;
var cache = getNewCache();

@@ -28,41 +28,41 @@

return copyArray(object, handleCopy);
return copyArray(object, handleCopy, realm);
}
if (object.constructor === Object) {
if (object.constructor === realm.Object) {
cache.add(object);
return copyObject(object, handleCopy, true);
return copyObject(object, handleCopy, realm, true);
}
if (object instanceof Date) {
if (object instanceof realm.Date) {
return new Date(object.getTime());
}
if (object instanceof RegExp) {
return copyRegExp(object);
if (object instanceof realm.RegExp) {
return copyRegExp(object, realm);
}
if (HAS_MAP_SUPPORT && object instanceof Map) {
if (realm.Map && object instanceof realm.Map) {
cache.add(object);
return copyIterable(object, handleCopy, true);
return copyIterable(object, handleCopy, realm, true);
}
if (HAS_SET_SUPPORT && object instanceof Set) {
if (realm.Set && object instanceof realm.Set) {
cache.add(object);
return copyIterable(object, handleCopy, false);
return copyIterable(object, handleCopy, realm, false);
}
if (HAS_BUFFER_SUPPORT && Buffer.isBuffer(object)) {
return copyBuffer(object);
if (realm.Buffer && realm.Buffer.isBuffer(object)) {
return copyBuffer(object, realm);
}
if (HAS_ARRAYBUFFER_SUPPORT) {
if (ArrayBuffer.isView(object)) {
if (realm.ArrayBuffer) {
if (realm.ArrayBuffer.isView(object)) {
return copyTypedArray(object);
}
if (object instanceof ArrayBuffer) {
if (object instanceof realm.ArrayBuffer) {
return copyArrayBuffer(object);

@@ -72,6 +72,6 @@ }

if (shouldObjectBeCopied(object)) {
if (shouldObjectBeCopied(object, realm)) {
cache.add(object);
return copyObject(object, handleCopy);
return copyObject(object, handleCopy, realm);
}

@@ -78,0 +78,0 @@

// constants
import { HAS_PROPERTY_SYMBOL_SUPPORT, HAS_WEAKMAP_SUPPORT, HAS_WEAKSET_SUPPORT } from './constants';
import { HAS_PROPERTY_SYMBOL_SUPPORT, HAS_WEAKSET_SUPPORT } from './constants';

@@ -89,6 +89,7 @@ var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;

* @param {any} object the object to test
* @param {any} realm the realm to check instanceof in
* @returns {boolean} should the object be copied
*/
export var shouldObjectBeCopied = function shouldObjectBeCopied(object) {
return typeof object.then !== 'function' && !(object instanceof Error) && !(HAS_WEAKMAP_SUPPORT && object instanceof WeakMap) && !(HAS_WEAKSET_SUPPORT && object instanceof WeakSet);
export var shouldObjectBeCopied = function shouldObjectBeCopied(object, realm) {
return typeof object.then !== 'function' && !(object instanceof realm.Error) && !(realm.WeakMap && object instanceof realm.WeakMap) && !(realm.WeakSet && object instanceof realm.WeakSet);
};

@@ -104,9 +105,10 @@

* @param {function} copy the function to copy values
* @param {any} realm the realm to check instanceof in
* @returns {Array<any>} the copied array
*/
export var copyArray = function copyArray(array, copy) {
export var copyArray = function copyArray(array, copy, realm) {
var newArray = new array.constructor();
for (var index = 0; index < array.length; index++) {
newArray.push(copy(array[index]));
newArray.push(copy(array[index], realm));
}

@@ -137,6 +139,7 @@

* @param {Buffer} buffer the buffer to copy
* @param {any} realm the realm to check instanceof in
* @returns {Buffer} the copied buffer
*/
export var copyBuffer = function copyBuffer(buffer) {
var newBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe(buffer.length) : new Buffer(buffer.length);
export var copyBuffer = function copyBuffer(buffer, realm) {
var newBuffer = realm.Buffer.allocUnsafe ? realm.Buffer.allocUnsafe(buffer.length) : new realm.Buffer(buffer.length);

@@ -156,6 +159,7 @@ buffer.copy(newBuffer);

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isMap is the iterable a map
* @returns {Map|Set} the copied iterable
*/
export var copyIterable = function copyIterable(iterable, copy, isMap) {
export var copyIterable = function copyIterable(iterable, copy, realm, isMap) {
var newIterable = new iterable.constructor();

@@ -165,5 +169,5 @@

if (isMap) {
newIterable.set(key, copy(value));
newIterable.set(key, copy(value, realm));
} else {
newIterable.add(copy(value));
newIterable.add(copy(value, realm));
}

@@ -183,6 +187,7 @@ });

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isPlainObject is the object to copy a plain object
* @returns {Object} the copied object
*/
export var copyObject = function copyObject(object, copy, isPlainObject) {
export var copyObject = function copyObject(object, copy, realm, isPlainObject) {
var newObject = isPlainObject ? {} : object.constructor ? new object.constructor() : Object.create(null);

@@ -197,3 +202,3 @@ var keys = Object.keys(object);

newObject[key] = copy(object[key]);
newObject[key] = copy(object[key], realm);
}

@@ -210,3 +215,3 @@ }

newObject[symbol] = copy(object[symbol]);
newObject[symbol] = copy(object[symbol], realm);
}

@@ -225,6 +230,7 @@ }

* @param {RegExp} regExp the RegExp to copy
* @param {any} realm the realm to check instanceof in
* @returns {RegExp} the copied RegExp
*/
export var copyRegExp = function copyRegExp(regExp) {
var newRegExp = new RegExp(regExp.source, getRegExpFlags(regExp));
export var copyRegExp = function copyRegExp(regExp, realm) {
var newRegExp = new realm.RegExp(regExp.source, getRegExpFlags(regExp));

@@ -231,0 +237,0 @@ newRegExp.lastIndex = regExp.lastIndex;

@@ -5,38 +5,9 @@ 'use strict';

/**
* @constant {boolean} HAS_ARRAYBUFFER_SUPPORT
*/
var HAS_ARRAYBUFFER_SUPPORT = exports.HAS_ARRAYBUFFER_SUPPORT = typeof ArrayBuffer === 'function';
/**
* @constant {boolean} HAS_BUFFER_SUPPORT
*/
var HAS_BUFFER_SUPPORT = exports.HAS_BUFFER_SUPPORT = typeof Buffer === 'function';
/**
* @constant {boolean} HAS_MAP_SUPPORT
*/
var HAS_MAP_SUPPORT = exports.HAS_MAP_SUPPORT = typeof Map === 'function';
/**
* @constant {boolean} HAS_PROPERTY_SYMBOL_SUPPORT
*/
var HAS_PROPERTY_SYMBOL_SUPPORT = exports.HAS_PROPERTY_SYMBOL_SUPPORT = typeof Object.getOwnPropertySymbols === 'function';
var HAS_PROPERTY_SYMBOL_SUPPORT = exports.HAS_PROPERTY_SYMBOL_SUPPORT = typeof global.Object.getOwnPropertySymbols === 'function';
/**
* @constant {boolean} HAS_SET_SUPPORT
*/
var HAS_SET_SUPPORT = exports.HAS_SET_SUPPORT = typeof Set === 'function';
/**
* @constant {boolean} HAS_TYPEDARRAY_SUPPORT
*/
var HAS_TYPEDARRAY_SUPPORT = exports.HAS_TYPEDARRAY_SUPPORT = typeof TypedArray === 'function';
/**
* @constant {boolean} HAS_WEAKMAP_SUPPORT
*/
var HAS_WEAKMAP_SUPPORT = exports.HAS_WEAKMAP_SUPPORT = typeof WeakMap === 'function';
/**
* @constant {boolean} HAS_WEAKSET_SUPPORT
*/
var HAS_WEAKSET_SUPPORT = exports.HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function';
var HAS_WEAKSET_SUPPORT = exports.HAS_WEAKSET_SUPPORT = typeof global.WeakSet === 'function';

@@ -6,4 +6,2 @@ 'use strict';

var _constants = require('./constants');
var _utils = require('./utils');

@@ -18,6 +16,8 @@

* @param {any} object the object to copy
* @param {any} [realm=global] the realm to check instanceof in
* @returns {any} the copied object
*/
// constants
function copy(object) {
var realm = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : global;
var cache = (0, _utils.getNewCache)();

@@ -33,41 +33,41 @@

return (0, _utils.copyArray)(object, handleCopy);
return (0, _utils.copyArray)(object, handleCopy, realm);
}
if (object.constructor === Object) {
if (object.constructor === realm.Object) {
cache.add(object);
return (0, _utils.copyObject)(object, handleCopy, true);
return (0, _utils.copyObject)(object, handleCopy, realm, true);
}
if (object instanceof Date) {
if (object instanceof realm.Date) {
return new Date(object.getTime());
}
if (object instanceof RegExp) {
return (0, _utils.copyRegExp)(object);
if (object instanceof realm.RegExp) {
return (0, _utils.copyRegExp)(object, realm);
}
if (_constants.HAS_MAP_SUPPORT && object instanceof Map) {
if (realm.Map && object instanceof realm.Map) {
cache.add(object);
return (0, _utils.copyIterable)(object, handleCopy, true);
return (0, _utils.copyIterable)(object, handleCopy, realm, true);
}
if (_constants.HAS_SET_SUPPORT && object instanceof Set) {
if (realm.Set && object instanceof realm.Set) {
cache.add(object);
return (0, _utils.copyIterable)(object, handleCopy, false);
return (0, _utils.copyIterable)(object, handleCopy, realm, false);
}
if (_constants.HAS_BUFFER_SUPPORT && Buffer.isBuffer(object)) {
return (0, _utils.copyBuffer)(object);
if (realm.Buffer && realm.Buffer.isBuffer(object)) {
return (0, _utils.copyBuffer)(object, realm);
}
if (_constants.HAS_ARRAYBUFFER_SUPPORT) {
if (ArrayBuffer.isView(object)) {
if (realm.ArrayBuffer) {
if (realm.ArrayBuffer.isView(object)) {
return (0, _utils.copyTypedArray)(object);
}
if (object instanceof ArrayBuffer) {
if (object instanceof realm.ArrayBuffer) {
return (0, _utils.copyArrayBuffer)(object);

@@ -77,6 +77,6 @@ }

if ((0, _utils.shouldObjectBeCopied)(object)) {
if ((0, _utils.shouldObjectBeCopied)(object, realm)) {
cache.add(object);
return (0, _utils.copyObject)(object, handleCopy);
return (0, _utils.copyObject)(object, handleCopy, realm);
}

@@ -88,4 +88,2 @@

return handleCopy(object);
}
// utils
} // utils

@@ -94,6 +94,7 @@ 'use strict';

* @param {any} object the object to test
* @param {any} realm the realm to check instanceof in
* @returns {boolean} should the object be copied
*/
var shouldObjectBeCopied = exports.shouldObjectBeCopied = function shouldObjectBeCopied(object) {
return typeof object.then !== 'function' && !(object instanceof Error) && !(_constants.HAS_WEAKMAP_SUPPORT && object instanceof WeakMap) && !(_constants.HAS_WEAKSET_SUPPORT && object instanceof WeakSet);
var shouldObjectBeCopied = exports.shouldObjectBeCopied = function shouldObjectBeCopied(object, realm) {
return typeof object.then !== 'function' && !(object instanceof realm.Error) && !(realm.WeakMap && object instanceof realm.WeakMap) && !(realm.WeakSet && object instanceof realm.WeakSet);
};

@@ -109,9 +110,10 @@

* @param {function} copy the function to copy values
* @param {any} realm the realm to check instanceof in
* @returns {Array<any>} the copied array
*/
var copyArray = exports.copyArray = function copyArray(array, copy) {
var copyArray = exports.copyArray = function copyArray(array, copy, realm) {
var newArray = new array.constructor();
for (var index = 0; index < array.length; index++) {
newArray.push(copy(array[index]));
newArray.push(copy(array[index], realm));
}

@@ -142,6 +144,7 @@

* @param {Buffer} buffer the buffer to copy
* @param {any} realm the realm to check instanceof in
* @returns {Buffer} the copied buffer
*/
var copyBuffer = exports.copyBuffer = function copyBuffer(buffer) {
var newBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe(buffer.length) : new Buffer(buffer.length);
var copyBuffer = exports.copyBuffer = function copyBuffer(buffer, realm) {
var newBuffer = realm.Buffer.allocUnsafe ? realm.Buffer.allocUnsafe(buffer.length) : new realm.Buffer(buffer.length);

@@ -161,6 +164,7 @@ buffer.copy(newBuffer);

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isMap is the iterable a map
* @returns {Map|Set} the copied iterable
*/
var copyIterable = exports.copyIterable = function copyIterable(iterable, copy, isMap) {
var copyIterable = exports.copyIterable = function copyIterable(iterable, copy, realm, isMap) {
var newIterable = new iterable.constructor();

@@ -170,5 +174,5 @@

if (isMap) {
newIterable.set(key, copy(value));
newIterable.set(key, copy(value, realm));
} else {
newIterable.add(copy(value));
newIterable.add(copy(value, realm));
}

@@ -188,6 +192,7 @@ });

* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isPlainObject is the object to copy a plain object
* @returns {Object} the copied object
*/
var copyObject = exports.copyObject = function copyObject(object, copy, isPlainObject) {
var copyObject = exports.copyObject = function copyObject(object, copy, realm, isPlainObject) {
var newObject = isPlainObject ? {} : object.constructor ? new object.constructor() : Object.create(null);

@@ -202,3 +207,3 @@ var keys = Object.keys(object);

newObject[key] = copy(object[key]);
newObject[key] = copy(object[key], realm);
}

@@ -215,3 +220,3 @@ }

newObject[symbol] = copy(object[symbol]);
newObject[symbol] = copy(object[symbol], realm);
}

@@ -230,6 +235,7 @@ }

* @param {RegExp} regExp the RegExp to copy
* @param {any} realm the realm to check instanceof in
* @returns {RegExp} the copied RegExp
*/
var copyRegExp = exports.copyRegExp = function copyRegExp(regExp) {
var newRegExp = new RegExp(regExp.source, getRegExpFlags(regExp));
var copyRegExp = exports.copyRegExp = function copyRegExp(regExp, realm) {
var newRegExp = new realm.RegExp(regExp.source, getRegExpFlags(regExp));

@@ -236,0 +242,0 @@ newRegExp.lastIndex = regExp.lastIndex;

@@ -101,3 +101,3 @@ {

"types": "index.d.ts",
"version": "1.1.2"
"version": "1.2.0"
}

@@ -12,2 +12,3 @@ # fast-copy

* [Usage](#usage)
* [Multiple realms](#multiple-realms)
* [Types supported](#types-supported)

@@ -38,2 +39,17 @@ * [Benchmarks](#benchmarks)

#### Multiple realms
Under the hood, `fast-copy` uses `instanceof` to determine object types, which can cause false negatives when used in combination with `iframe`-based objects. To handle this edge case, you can pass the optional second parameter of `realm` to the `copy` method, which identifies which realm the object comes from and will use that realm to drive both comparisons and constructors for the copies.
```html
<iframe srcdoc="<script>var arr = ['foo', 'bar'];</script>"></iframe>
```
```javascript
const iframe = document.querySelector("iframe");
const arr = iframe.contentWindow.arr;
console.log(copy(arr, iframe.contentWindow)); // ['foo', 'bar']
```
## Types supported

@@ -40,0 +56,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc