Comparing version 3.0.0-beta.3 to 3.0.0-beta.4
@@ -7,7 +7,7 @@ # fast-copy CHANGELOG | ||
- `copy` is now a named export instead of a default export | ||
- Exports are now always named, so if using CJS the `.default` suffix is required, e.g. `require('fast-copy').default` | ||
- `copy.strict` is no longer available; it is now available as the explicit `copyStrict` named import | ||
- Options have been removed | ||
- `isStrict` has been replaced with importing `copyStrict` | ||
- `realm` has been removed, as `instanceof` is no longer used internally | ||
- `isStrict` option has been replaced with importing the separate `copyStrict` method | ||
- `realm` has been removed entirely, as `instanceof` is no longer used internally | ||
@@ -14,0 +14,0 @@ ## 2.1.7 |
/** | ||
* Copy an value deeply as much as possible. | ||
*/ | ||
export declare function copy<Value>(value: Value): Value; | ||
export default function copy<Value>(value: Value): Value; | ||
/** | ||
@@ -6,0 +6,0 @@ * Copy an value deeply as much as possible, where strict recreation of object properties |
@@ -1,2 +0,1 @@ | ||
declare type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value; | ||
export interface Cache { | ||
@@ -14,17 +13,6 @@ _keys?: any[]; | ||
export declare const createCache: typeof createCacheModern; | ||
export declare function getArrayCloneLoose(array: any[], prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get an empty version of the object with the same prototype it has. | ||
*/ | ||
export declare function getCleanClone(object: any, prototype: any): any; | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
export declare function getObjectCloneLoose(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
export declare function getObjectCloneStrict(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
export declare function getCleanClone(prototype: any): any; | ||
declare function getRegExpFlagsModern(regExp: RegExp): string; | ||
@@ -31,0 +19,0 @@ /** |
/** | ||
* Copy an value deeply as much as possible. | ||
*/ | ||
export declare function copy<Value>(value: Value): Value; | ||
export default function copy<Value>(value: Value): Value; | ||
/** | ||
@@ -6,0 +6,0 @@ * Copy an value deeply as much as possible, where strict recreation of object properties |
@@ -1,2 +0,1 @@ | ||
declare type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value; | ||
export interface Cache { | ||
@@ -14,17 +13,6 @@ _keys?: any[]; | ||
export declare const createCache: typeof createCacheModern; | ||
export declare function getArrayCloneLoose(array: any[], prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get an empty version of the object with the same prototype it has. | ||
*/ | ||
export declare function getCleanClone(object: any, prototype: any): any; | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
export declare function getObjectCloneLoose(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
export declare function getObjectCloneStrict(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
export declare function getCleanClone(prototype: any): any; | ||
declare function getRegExpFlagsModern(regExp: RegExp): string; | ||
@@ -31,0 +19,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["fast-copy"]={})}(this,(function(e){"use strict";var t=Function.prototype.toString,r=Object.create,n=Object.defineProperty,o=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,c=Object.getOwnPropertySymbols,u=Object.prototype,a=u.hasOwnProperty,f=u.propertyIsEnumerable,s="function"==typeof c,l=function(){function e(){this._keys=[],this._values=[]}return e.prototype.has=function(e){return!!~this._keys.indexOf(e)},e.prototype.get=function(e){return this._values[this._keys.indexOf(e)]},e.prototype.set=function(e,t){this._keys.push(e),this._values.push(t)},e}();var y="undefined"!=typeof WeakMap?function(){return new WeakMap}:function(){return new l};function p(e,t,r,n){var o=new t.constructor;n.set(e,o);for(var i=0,c=e.length;i<c;++i)o[i]=r(e[i],n);return o}function b(e,n){if(!n)return r(null);var o=n.constructor;if(o===Object)return n===Object.prototype?{}:r(n);if(~t.call(o).indexOf("[native code]"))try{return new o}catch(e){}return r(n)}function j(e,t,r,n){var o=b(0,t);for(var i in n.set(e,o),e)a.call(e,i)&&(o[i]=r(e[i],n));if(s)for(var u=c(e),l=0,y=u.length,p=void 0;l<y;++l)p=u[l],f.call(e,p)&&(o[p]=r(e[p],n));return o}var v=s?function(e){return i(e).concat(c(e))}:i;function d(e,t,r,i){var c=b(0,t);i.set(e,c);for(var u=v(e),a=0,f=u.length,s=void 0,l=void 0;a<f;++a)if("callee"!==(s=u[a])&&"caller"!==s)if(l=o(e,s)){l.get||l.set||(l.value=r(e[s],i));try{n(c,s,l)}catch(e){c[s]=l.value}}else c[s]=r(e[s],i);return c}var g,h,O="g"===/test/g.flags?function(e){return e.flags}:function(e){var t="";return e.global&&(t+="g"),e.ignoreCase&&(t+="i"),e.multiline&&(t+="m"),e.unicode&&(t+="u"),e.sticky&&(t+="y"),t},w=Array.isArray,A=Object.getPrototypeOf,_=Object.prototype.toString,m=((g={})["[object ArrayBuffer]"]=!0,g["[object Float32Array]"]=!0,g["[object Float64Array]"]=!0,g["[object Int8Array]"]=!0,g["[object Int16Array]"]=!0,g["[object Int32Array]"]=!0,g["[object Uint8Array]"]=!0,g["[object Uint8ClampedArray]"]=!0,g["[object Uint16Array]"]=!0,g["[object Uint32Array]"]=!0,g["[object Uint64Array]"]=!0,g),k=((h={})["[object Error]"]=!0,h["[object Promise]"]=!0,h["[object WeakMap]"]=!0,h["[object WeakSet]"]=!0,h);function x(e,t,r){return function e(n,o){if(!n||"object"!=typeof n)return n;if(o.has(n))return o.get(n);var i=n.__proto__||A(n),c=i&&i.constructor;if(!c||c===Object)return t(n,i,e,o);if(w(n))return r(n,i,e,o);var u=_.call(n);if("[object Date]"===u)return new c(n.getTime());if("[object RegExp]"===u)return(a=new c(n.source,n.flags||O(n))).lastIndex=n.lastIndex,a;if("[object Map]"===u){var a=new c(n.entries());return o.set(n,a),a}if("[object Set]"===u){a=new c(n.values());return o.set(n,a),a}if("[object Blob]"===u)return n.slice(0,n.size,n.type);if("[object DataView]"===u){a=new c(n.buffer.slice(0));return o.set(n,a),a}if(m[u]){a=n.slice(0);return o.set(n,a),a}return"function"==typeof n.then||k[u]?n:t(n,i,e,o)}(e,y())}e.copy=function(e){return x(e,j,p)},e.copyStrict=function(e){return x(e,d,d)},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["fast-copy"]={})}(this,(function(t){"use strict";var e=Function.prototype.toString,r=Object.create,n=function(){function t(){this._keys=[],this._values=[]}return t.prototype.has=function(t){return!!~this._keys.indexOf(t)},t.prototype.get=function(t){return this._values[this._keys.indexOf(t)]},t.prototype.set=function(t,e){this._keys.push(t),this._values.push(e)},t}();var o="undefined"!=typeof WeakMap?function(){return new WeakMap}:function(){return new n};function c(t){if(!t)return r(null);var n=t.constructor;if(n===Object)return t===Object.prototype?{}:r(t);if(~e.call(n).indexOf("[native code]"))try{return new n}catch(t){}return r(t)}var i,a,u="g"===/test/g.flags?function(t){return t.flags}:function(t){var e="";return t.global&&(e+="g"),t.ignoreCase&&(e+="i"),t.multiline&&(e+="m"),t.unicode&&(e+="u"),t.sticky&&(e+="y"),e},f=Array.isArray,s=Object.defineProperty,l=Object.getOwnPropertyDescriptor,y=Object.getOwnPropertyNames,p=Object.getOwnPropertySymbols,b=Object.getPrototypeOf,j=Object.prototype,v=j.hasOwnProperty,d=j.propertyIsEnumerable,g=((i={})["[object ArrayBuffer]"]=!0,i["[object Float32Array]"]=!0,i["[object Float64Array]"]=!0,i["[object Int8Array]"]=!0,i["[object Int16Array]"]=!0,i["[object Int32Array]"]=!0,i["[object Uint8Array]"]=!0,i["[object Uint8ClampedArray]"]=!0,i["[object Uint16Array]"]=!0,i["[object Uint32Array]"]=!0,i["[object Uint64Array]"]=!0,i),h=((a={})["[object Error]"]=!0,a["[object Promise]"]=!0,a["[object WeakMap]"]=!0,a["[object WeakSet]"]=!0,a),O="function"==typeof p;var w=O?function(t){return y(t).concat(p(t))}:y;function A(t,e,r,n){var o=new e.constructor;n.set(t,o);for(var c=0,i=t.length;c<i;++c)o[c]=r(t[c],n);return o}var _=O?function(t,e,r,n){var o=c(e);for(var i in n.set(t,o),t)v.call(t,i)&&(o[i]=r(t[i],n));for(var a=p(t),u=0,f=a.length,s=void 0;u<f;++u)s=a[u],d.call(t,s)&&(o[s]=r(t[s],n));return o}:function(t,e,r,n){var o=c(e);for(var i in n.set(t,o),t)v.call(t,i)&&(o[i]=r(t[i],n));return o};function m(t,e,r,n){var o=c(e);n.set(t,o);for(var i=w(t),a=0,u=i.length,f=void 0,y=void 0;a<u;++a)if("callee"!==(f=i[a])&&"caller"!==f)if(y=l(t,f)){y.get||y.set||(y.value=r(y.value,n));try{s(o,f,y)}catch(t){o[f]=y.value}}else o[f]=r(t[f],n);return o}function k(t){var e=t?m:A,r=t?m:_;return function t(n,o){if(!n||"object"!=typeof n)return n;if(o.has(n))return o.get(n);var c=n.__proto__||b(n),i=c&&c.constructor;if(!i||i===Object)return r(n,c,t,o);if(f(n))return e(n,c,t,o);var a=toString.call(n);return"[object Date]"===a?new i(n.getTime()):"[object RegExp]"===a?function(t,e){var r=new e(t.source,u(t));return r.lastIndex=t.lastIndex,r}(n,i):"[object Map]"===a?function(t,e,r,n){var o=new e;return n.set(t,o),t.forEach((function(t,e){o.set(e,r(t,n))})),t}(n,i,t,o):"[object Set]"===a?function(t,e,r,n){var o=new e;return n.set(t,o),t.forEach((function(t){o.add(r(t,n))})),t}(n,i,t,o):"[object Blob]"===a?n.slice(0,n.size,n.type):"[object DataView]"===a?new i(n.buffer.slice(0)):g[a]?n.slice(0):"function"==typeof n.then||h[a]?n:r(n,c,t,o)}}var x=k(!0),P=k(!1);t.copyStrict=function(t){return x(t,o())},t.default=function(t){return P(t,o())},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.min.js.map |
/** | ||
* Copy an value deeply as much as possible. | ||
*/ | ||
export declare function copy<Value>(value: Value): Value; | ||
export default function copy<Value>(value: Value): Value; | ||
/** | ||
@@ -6,0 +6,0 @@ * Copy an value deeply as much as possible, where strict recreation of object properties |
@@ -1,2 +0,1 @@ | ||
declare type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value; | ||
export interface Cache { | ||
@@ -14,17 +13,6 @@ _keys?: any[]; | ||
export declare const createCache: typeof createCacheModern; | ||
export declare function getArrayCloneLoose(array: any[], prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get an empty version of the object with the same prototype it has. | ||
*/ | ||
export declare function getCleanClone(object: any, prototype: any): any; | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
export declare function getObjectCloneLoose(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
export declare function getObjectCloneStrict(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
export declare function getCleanClone(prototype: any): any; | ||
declare function getRegExpFlagsModern(regExp: RegExp): string; | ||
@@ -31,0 +19,0 @@ /** |
@@ -8,5 +8,3 @@ (function (global, factory) { | ||
var toStringFunction = Function.prototype.toString; | ||
var create = Object.create, defineProperty = Object.defineProperty, getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor, getOwnPropertyNames = Object.getOwnPropertyNames, getOwnPropertySymbols = Object.getOwnPropertySymbols; | ||
var _a$1 = Object.prototype, hasOwnProperty = _a$1.hasOwnProperty, propertyIsEnumerable = _a$1.propertyIsEnumerable; | ||
var SYMBOL_PROPERTIES = typeof getOwnPropertySymbols === 'function'; | ||
var create = Object.create; | ||
var LegacyCache = /** @class */ (function () { | ||
@@ -39,14 +37,6 @@ function LegacyCache() { | ||
var createCache = typeof WeakMap !== 'undefined' ? createCacheModern : createCacheLegacy; | ||
function getArrayCloneLoose(array, prototype, handleCopy, cache) { | ||
var clone = new prototype.constructor(); | ||
cache.set(array, clone); | ||
for (var index = 0, length_1 = array.length; index < length_1; ++index) { | ||
clone[index] = handleCopy(array[index], cache); | ||
} | ||
return clone; | ||
} | ||
/** | ||
* Get an empty version of the object with the same prototype it has. | ||
*/ | ||
function getCleanClone(object, prototype) { | ||
function getCleanClone(prototype) { | ||
if (!prototype) { | ||
@@ -67,67 +57,2 @@ return create(null); | ||
} | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
function getObjectCloneLoose(object, prototype, handleCopy, cache) { | ||
var clone = getCleanClone(object, prototype); | ||
// set in the cache immediately to be able to reuse the object recursively | ||
cache.set(object, clone); | ||
for (var key in object) { | ||
if (hasOwnProperty.call(object, key)) { | ||
clone[key] = handleCopy(object[key], cache); | ||
} | ||
} | ||
if (SYMBOL_PROPERTIES) { | ||
var symbols = getOwnPropertySymbols(object); | ||
for (var index = 0, length_2 = symbols.length, symbol = void 0; index < length_2; ++index) { | ||
symbol = symbols[index]; | ||
if (propertyIsEnumerable.call(object, symbol)) { | ||
clone[symbol] = handleCopy(object[symbol], cache); | ||
} | ||
} | ||
} | ||
return clone; | ||
} | ||
function getStrictPropertiesModern(object) { | ||
return getOwnPropertyNames(object).concat(getOwnPropertySymbols(object)); | ||
} | ||
var getStrictProperties = SYMBOL_PROPERTIES | ||
? getStrictPropertiesModern | ||
: getOwnPropertyNames; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
function getObjectCloneStrict(object, prototype, handleCopy, cache) { | ||
var clone = getCleanClone(object, prototype); | ||
// set in the cache immediately to be able to reuse the object recursively | ||
cache.set(object, clone); | ||
var properties = getStrictProperties(object); | ||
for (var index = 0, length_3 = properties.length, property = void 0, descriptor = void 0; index < length_3; ++index) { | ||
property = properties[index]; | ||
if (property !== 'callee' && property !== 'caller') { | ||
descriptor = getOwnPropertyDescriptor(object, property); | ||
if (descriptor) { | ||
// Only clone the value if actually a value, not a getter / setter. | ||
if (!descriptor.get && !descriptor.set) { | ||
descriptor.value = handleCopy(object[property], cache); | ||
} | ||
try { | ||
defineProperty(clone, property, descriptor); | ||
} | ||
catch (error) { | ||
// Tee above can fail on node in edge cases, so fall back to the loose assignment. | ||
clone[property] = descriptor.value; | ||
} | ||
} | ||
else { | ||
// In extra edge cases where the property descriptor cannot be retrived, fall back to | ||
// the loose assignment. | ||
clone[property] = handleCopy(object[property], cache); | ||
} | ||
} | ||
} | ||
return clone; | ||
} | ||
function getRegExpFlagsLegacy(regExp) { | ||
@@ -162,4 +87,4 @@ var flags = ''; | ||
var isArray = Array.isArray; | ||
var getPrototypeOf = Object.getPrototypeOf; | ||
var toString = Object.prototype.toString; | ||
var defineProperty = Object.defineProperty, getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor, getOwnPropertyNames = Object.getOwnPropertyNames, getOwnPropertySymbols = Object.getOwnPropertySymbols, getPrototypeOf = Object.getPrototypeOf; | ||
var _c = Object.prototype, hasOwnProperty = _c.hasOwnProperty, propertyIsEnumerable = _c.propertyIsEnumerable; | ||
var ARRAY_BUFFER_OBJECT_CLASSES = (_a = {}, | ||
@@ -184,4 +109,113 @@ _a['[object ArrayBuffer]'] = true, | ||
_b); | ||
function performCopy(value, getObjectClone, getArrayClone) { | ||
function handleCopy(value, cache) { | ||
var SUPPORTS_SYMBOL = typeof getOwnPropertySymbols === 'function'; | ||
function getStrictPropertiesModern(object) { | ||
return getOwnPropertyNames(object).concat(getOwnPropertySymbols(object)); | ||
} | ||
var getStrictProperties = SUPPORTS_SYMBOL | ||
? getStrictPropertiesModern | ||
: getOwnPropertyNames; | ||
function copyArrayLoose(array, prototype, copier, cache) { | ||
var clone = new prototype.constructor(); | ||
cache.set(array, clone); | ||
for (var index = 0, length_1 = array.length; index < length_1; ++index) { | ||
clone[index] = copier(array[index], cache); | ||
} | ||
return clone; | ||
} | ||
function copyObjectLooseLegacy(object, prototype, copier, cache) { | ||
var clone = getCleanClone(prototype); | ||
// set in the cache immediately to be able to reuse the object recursively | ||
cache.set(object, clone); | ||
for (var key in object) { | ||
if (hasOwnProperty.call(object, key)) { | ||
clone[key] = copier(object[key], cache); | ||
} | ||
} | ||
return clone; | ||
} | ||
function copyObjectLooseModern(object, prototype, copier, cache) { | ||
var clone = getCleanClone(prototype); | ||
// set in the cache immediately to be able to reuse the object recursively | ||
cache.set(object, clone); | ||
for (var key in object) { | ||
if (hasOwnProperty.call(object, key)) { | ||
clone[key] = copier(object[key], cache); | ||
} | ||
} | ||
var symbols = getOwnPropertySymbols(object); | ||
for (var index = 0, length_2 = symbols.length, symbol = void 0; index < length_2; ++index) { | ||
symbol = symbols[index]; | ||
if (propertyIsEnumerable.call(object, symbol)) { | ||
clone[symbol] = copier(object[symbol], cache); | ||
} | ||
} | ||
return clone; | ||
} | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
var copyObjectLoose = SUPPORTS_SYMBOL | ||
? copyObjectLooseModern | ||
: copyObjectLooseLegacy; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
function copyObjectStrict(object, prototype, copier, cache) { | ||
var clone = getCleanClone(prototype); | ||
// set in the cache immediately to be able to reuse the object recursively | ||
cache.set(object, clone); | ||
var properties = getStrictProperties(object); | ||
for (var index = 0, length_3 = properties.length, property = void 0, descriptor = void 0; index < length_3; ++index) { | ||
property = properties[index]; | ||
if (property === 'callee' || property === 'caller') { | ||
continue; | ||
} | ||
descriptor = getOwnPropertyDescriptor(object, property); | ||
if (!descriptor) { | ||
// In extra edge cases where the property descriptor cannot be retrived, fall back to | ||
// the loose assignment. | ||
clone[property] = copier(object[property], cache); | ||
continue; | ||
} | ||
// Only clone the value if actually a value, not a getter / setter. | ||
if (!descriptor.get && !descriptor.set) { | ||
descriptor.value = copier(descriptor.value, cache); | ||
} | ||
try { | ||
defineProperty(clone, property, descriptor); | ||
} | ||
catch (error) { | ||
// Tee above can fail on node in edge cases, so fall back to the loose assignment. | ||
clone[property] = descriptor.value; | ||
} | ||
} | ||
return clone; | ||
} | ||
function copyMap(value, Constructor, copier, cache) { | ||
var clone = new Constructor(); | ||
cache.set(value, clone); | ||
value.forEach(function (v, k) { | ||
clone.set(k, copier(v, cache)); | ||
}); | ||
return value; | ||
} | ||
function copyRegExp(value, Constructor) { | ||
var clone = new Constructor(value.source, getRegExpFlags(value)); | ||
clone.lastIndex = value.lastIndex; | ||
return clone; | ||
} | ||
function copySet(value, Constructor, copier, cache) { | ||
var clone = new Constructor(); | ||
cache.set(value, clone); | ||
value.forEach(function (v) { | ||
clone.add(copier(v, cache)); | ||
}); | ||
return value; | ||
} | ||
function createCopier(isStrict) { | ||
var copyArray = isStrict ? copyObjectStrict : copyArrayLoose; | ||
var copyObject = isStrict ? copyObjectStrict : copyObjectLoose; | ||
return function copier(value, cache) { | ||
if (!value || typeof value !== 'object') { | ||
@@ -197,7 +231,7 @@ return value; | ||
if (!Constructor || Constructor === Object) { | ||
return getObjectClone(value, prototype, handleCopy, cache); | ||
return copyObject(value, prototype, copier, cache); | ||
} | ||
// arrays | ||
if (isArray(value)) { | ||
return getArrayClone(value, prototype, handleCopy, cache); | ||
return copyArray(value, prototype, copier, cache); | ||
} | ||
@@ -211,17 +245,11 @@ var objectClass = toString.call(value); | ||
if (objectClass === '[object RegExp]') { | ||
var clone = new Constructor(value.source, value.flags || getRegExpFlags(value)); | ||
clone.lastIndex = value.lastIndex; | ||
return clone; | ||
return copyRegExp(value, Constructor); | ||
} | ||
// maps | ||
if (objectClass === '[object Map]') { | ||
var clone = new Constructor(value.entries()); | ||
cache.set(value, clone); | ||
return clone; | ||
return copyMap(value, Constructor, copier, cache); | ||
} | ||
// sets | ||
if (objectClass === '[object Set]') { | ||
var clone = new Constructor(value.values()); | ||
cache.set(value, clone); | ||
return clone; | ||
return copySet(value, Constructor, copier, cache); | ||
} | ||
@@ -234,11 +262,7 @@ // blobs | ||
if (objectClass === '[object DataView]') { | ||
var clone = new Constructor(value.buffer.slice(0)); | ||
cache.set(value, clone); | ||
return clone; | ||
return new Constructor(value.buffer.slice(0)); | ||
} | ||
// array buffers | ||
if (ARRAY_BUFFER_OBJECT_CLASSES[objectClass]) { | ||
var clone = value.slice(0); | ||
cache.set(value, clone); | ||
return clone; | ||
return value.slice(0); | ||
} | ||
@@ -254,6 +278,8 @@ // if the value cannot / should not be copied deeply, return the reference | ||
// assume anything left is a custom constructor | ||
return getObjectClone(value, prototype, handleCopy, cache); | ||
} | ||
return handleCopy(value, createCache()); | ||
return copyObject(value, prototype, copier, cache); | ||
}; | ||
} | ||
var copierStrict = createCopier(true); | ||
var copierLoose = createCopier(false); | ||
/** | ||
@@ -263,3 +289,3 @@ * Copy an value deeply as much as possible. | ||
function copy(value) { | ||
return performCopy(value, getObjectCloneLoose, getArrayCloneLoose); | ||
return copierLoose(value, createCache()); | ||
} | ||
@@ -272,7 +298,7 @@ /** | ||
function copyStrict(value) { | ||
return performCopy(value, getObjectCloneStrict, getObjectCloneStrict); | ||
return copierStrict(value, createCache()); | ||
} | ||
exports.copy = copy; | ||
exports.copyStrict = copyStrict; | ||
exports["default"] = copy; | ||
@@ -279,0 +305,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
/** | ||
* Copy an value deeply as much as possible. | ||
*/ | ||
export declare function copy<Value>(value: Value): Value; | ||
export default function copy<Value>(value: Value): Value; | ||
/** | ||
@@ -6,0 +6,0 @@ * Copy an value deeply as much as possible, where strict recreation of object properties |
@@ -1,2 +0,1 @@ | ||
declare type InternalCopier = <Value = any>(value: Value, cache: Cache) => Value; | ||
export interface Cache { | ||
@@ -14,17 +13,6 @@ _keys?: any[]; | ||
export declare const createCache: typeof createCacheModern; | ||
export declare function getArrayCloneLoose(array: any[], prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get an empty version of the object with the same prototype it has. | ||
*/ | ||
export declare function getCleanClone(object: any, prototype: any): any; | ||
/** | ||
* Get a copy of the object based on loose rules, meaning all enumerable keys | ||
* and symbols are copied, but property descriptors are not considered. | ||
*/ | ||
export declare function getObjectCloneLoose(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
/** | ||
* Get a copy of the object based on strict rules, meaning all keys and symbols | ||
* are copied based on the original property descriptors. | ||
*/ | ||
export declare function getObjectCloneStrict(object: any, prototype: any, handleCopy: InternalCopier, cache: Cache): any; | ||
export declare function getCleanClone(prototype: any): any; | ||
declare function getRegExpFlagsModern(regExp: RegExp): string; | ||
@@ -31,0 +19,0 @@ /** |
@@ -1,2 +0,4 @@ | ||
export const copy: <Value>(value: Value) => Value; | ||
declare const copy: <Value>(value: Value) => Value; | ||
export const copyStrict: <Value>(value: Value) => Value; | ||
export default copy; |
@@ -100,3 +100,3 @@ { | ||
"types": "index.d.ts", | ||
"version": "3.0.0-beta.3" | ||
"version": "3.0.0-beta.4" | ||
} |
@@ -56,3 +56,3 @@ # fast-copy | ||
```ts | ||
import { copy } from 'fast-copy'; | ||
import copy from 'fast-copy'; | ||
@@ -59,0 +59,0 @@ const copied = copy({ foo: 'bar' }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136948
33
1142