Comparing version 3.0.0-beta.6 to 3.0.0-beta.7
@@ -10,2 +10,3 @@ import type { Cache } from './utils'; | ||
export declare function copyArrayLoose(array: any[], state: State): any; | ||
export declare function copyArrayStrict<Value extends any[]>(array: Value, state: State): Value; | ||
export declare function copyArrayBuffer<Value extends ArrayBuffer>(arrayBuffer: Value, _state: State): Value; | ||
@@ -15,3 +16,4 @@ export declare function copyBlob<Value extends Blob>(blob: Value, _state: State): Value; | ||
export declare function copyDate<Value extends Date>(date: Value, state: State): Value; | ||
export declare function copyMap<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapLoose<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapStrict<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
declare function copyObjectLooseModern<Value extends {}>(object: Value, state: State): Value; | ||
@@ -30,3 +32,4 @@ /** | ||
export declare function copySelf<Value>(value: Value, _state: State): Value; | ||
export declare function copySet<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetLoose<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetStrict<Value extends Set<any>>(set: Value, state: State): Value; | ||
export {}; |
@@ -20,2 +20,7 @@ import type { InternalCopier } from './copier'; | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
export declare function createStrictCopier(options: CreateCopierOptions): <Value>(value: Value) => Value; | ||
/** | ||
* Copy an value deeply as much as possible, where strict recreation of object properties | ||
@@ -22,0 +27,0 @@ * are maintained. All properties (including non-enumerable ones) are copied with their |
@@ -22,7 +22,7 @@ export interface Cache { | ||
export declare const getRegExpFlags: typeof getRegExpFlagsModern; | ||
declare function getTagLegacy(value: any): string; | ||
declare function getTagModern(value: any): string; | ||
/** | ||
* Get the tag of the value passed, so that the correct copier can be used. | ||
*/ | ||
export declare const getTag: typeof getTagLegacy; | ||
export declare const getTag: typeof getTagModern; | ||
export {}; |
@@ -10,2 +10,3 @@ import type { Cache } from './utils'; | ||
export declare function copyArrayLoose(array: any[], state: State): any; | ||
export declare function copyArrayStrict<Value extends any[]>(array: Value, state: State): Value; | ||
export declare function copyArrayBuffer<Value extends ArrayBuffer>(arrayBuffer: Value, _state: State): Value; | ||
@@ -15,3 +16,4 @@ export declare function copyBlob<Value extends Blob>(blob: Value, _state: State): Value; | ||
export declare function copyDate<Value extends Date>(date: Value, state: State): Value; | ||
export declare function copyMap<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapLoose<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapStrict<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
declare function copyObjectLooseModern<Value extends {}>(object: Value, state: State): Value; | ||
@@ -30,3 +32,4 @@ /** | ||
export declare function copySelf<Value>(value: Value, _state: State): Value; | ||
export declare function copySet<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetLoose<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetStrict<Value extends Set<any>>(set: Value, state: State): Value; | ||
export {}; |
@@ -20,2 +20,7 @@ import type { InternalCopier } from './copier'; | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
export declare function createStrictCopier(options: CreateCopierOptions): <Value>(value: Value) => Value; | ||
/** | ||
* Copy an value deeply as much as possible, where strict recreation of object properties | ||
@@ -22,0 +27,0 @@ * are maintained. All properties (including non-enumerable ones) are copied with their |
@@ -22,7 +22,7 @@ export interface Cache { | ||
export declare const getRegExpFlags: typeof getRegExpFlagsModern; | ||
declare function getTagLegacy(value: any): string; | ||
declare function getTagModern(value: any): string; | ||
/** | ||
* Get the tag of the value passed, so that the correct copier can be used. | ||
*/ | ||
export declare const getTag: typeof getTagLegacy; | ||
export declare const getTag: typeof getTagModern; | ||
export {}; |
@@ -10,2 +10,3 @@ import type { Cache } from './utils'; | ||
export declare function copyArrayLoose(array: any[], state: State): any; | ||
export declare function copyArrayStrict<Value extends any[]>(array: Value, state: State): Value; | ||
export declare function copyArrayBuffer<Value extends ArrayBuffer>(arrayBuffer: Value, _state: State): Value; | ||
@@ -15,3 +16,4 @@ export declare function copyBlob<Value extends Blob>(blob: Value, _state: State): Value; | ||
export declare function copyDate<Value extends Date>(date: Value, state: State): Value; | ||
export declare function copyMap<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapLoose<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapStrict<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
declare function copyObjectLooseModern<Value extends {}>(object: Value, state: State): Value; | ||
@@ -30,3 +32,4 @@ /** | ||
export declare function copySelf<Value>(value: Value, _state: State): Value; | ||
export declare function copySet<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetLoose<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetStrict<Value extends Set<any>>(set: Value, state: State): Value; | ||
export {}; |
@@ -20,2 +20,7 @@ import type { InternalCopier } from './copier'; | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
export declare function createStrictCopier(options: CreateCopierOptions): <Value>(value: Value) => Value; | ||
/** | ||
* Copy an value deeply as much as possible, where strict recreation of object properties | ||
@@ -22,0 +27,0 @@ * are maintained. All properties (including non-enumerable ones) are copied with their |
@@ -22,7 +22,7 @@ export interface Cache { | ||
export declare const getRegExpFlags: typeof getRegExpFlagsModern; | ||
declare function getTagLegacy(value: any): string; | ||
declare function getTagModern(value: any): string; | ||
/** | ||
* Get the tag of the value passed, so that the correct copier can be used. | ||
*/ | ||
export declare const getTag: typeof getTagLegacy; | ||
export declare const getTag: typeof getTagModern; | ||
export {}; |
@@ -103,2 +103,31 @@ (function (global, factory) { | ||
: getOwnPropertyNames; | ||
function copyOwnPropertiesStrict(value, clone, state) { | ||
var properties = getStrictProperties(value); | ||
var copier = state.copier; | ||
for (var index = 0, length_1 = properties.length, property = void 0, descriptor = void 0; index < length_1; ++index) { | ||
property = properties[index]; | ||
if (property === 'callee' || property === 'caller') { | ||
continue; | ||
} | ||
descriptor = getOwnPropertyDescriptor(value, property); | ||
if (!descriptor) { | ||
// In extra edge cases where the property descriptor cannot be retrived, fall back to | ||
// the loose assignment. | ||
clone[property] = copier(value[property], state); | ||
continue; | ||
} | ||
// Only clone the value if actually a value, not a getter / setter. | ||
if (!descriptor.get && !descriptor.set) { | ||
descriptor.value = copier(descriptor.value, state); | ||
} | ||
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 copyArrayLoose(array, state) { | ||
@@ -108,3 +137,3 @@ var clone = new state.Constructor(); | ||
cache.set(array, clone); | ||
for (var index = 0, length_1 = array.length; index < length_1; ++index) { | ||
for (var index = 0, length_2 = array.length; index < length_2; ++index) { | ||
clone[index] = copier(array[index], state); | ||
@@ -114,2 +143,7 @@ } | ||
} | ||
function copyArrayStrict(array, state) { | ||
var clone = new state.Constructor(); | ||
state.cache.set(array, clone); | ||
return copyOwnPropertiesStrict(array, clone, state); | ||
} | ||
function copyArrayBuffer(arrayBuffer, _state) { | ||
@@ -127,3 +161,3 @@ return arrayBuffer.slice(0); | ||
} | ||
function copyMap(map, state) { | ||
function copyMapLoose(map, state) { | ||
var clone = new state.Constructor(); | ||
@@ -135,4 +169,7 @@ var cache = state.cache, copier = state.copier; | ||
}); | ||
return map; | ||
return clone; | ||
} | ||
function copyMapStrict(map, state) { | ||
return copyOwnPropertiesStrict(map, copyMapLoose(map, state), state); | ||
} | ||
function copyObjectLooseLegacy(object, state) { | ||
@@ -161,3 +198,3 @@ var clone = getCleanClone(state.prototype); | ||
var symbols = getOwnPropertySymbols(object); | ||
for (var index = 0, length_2 = symbols.length, symbol = void 0; index < length_2; ++index) { | ||
for (var index = 0, length_3 = symbols.length, symbol = void 0; index < length_3; ++index) { | ||
symbol = symbols[index]; | ||
@@ -183,31 +220,5 @@ if (propertyIsEnumerable.call(object, symbol)) { | ||
var clone = getCleanClone(state.prototype); | ||
var cache = state.cache, copier = state.copier; | ||
// 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], state); | ||
continue; | ||
} | ||
// Only clone the value if actually a value, not a getter / setter. | ||
if (!descriptor.get && !descriptor.set) { | ||
descriptor.value = copier(descriptor.value, state); | ||
} | ||
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; | ||
state.cache.set(object, clone); | ||
return copyOwnPropertiesStrict(object, clone, state); | ||
} | ||
@@ -222,3 +233,3 @@ function copyRegExp(regExp, state) { | ||
} | ||
function copySet(set, state) { | ||
function copySetLoose(set, state) { | ||
var clone = new state.Constructor(); | ||
@@ -230,4 +241,7 @@ var cache = state.cache, copier = state.copier; | ||
}); | ||
return set; | ||
return clone; | ||
} | ||
function copySetStrict(set, state) { | ||
return copyOwnPropertiesStrict(set, copySetLoose(set, state), state); | ||
} | ||
@@ -240,4 +254,5 @@ var isArray = Array.isArray; | ||
function createCopier(options) { | ||
var _a = options.array, array = _a === void 0 ? copyArrayLoose : _a, _b = options.arrayBuffer, arrayBuffer = _b === void 0 ? copyArrayBuffer : _b, _c = options.blob, blob = _c === void 0 ? copyBlob : _c, _d = options.dataView, dataView = _d === void 0 ? copyDataView : _d, _e = options.date, date = _e === void 0 ? copyDate : _e, _f = options.error, error = _f === void 0 ? copySelf : _f, _g = options.map, map = _g === void 0 ? copyMap : _g, _h = options.object, object = _h === void 0 ? copyObjectLoose : _h, _j = options.regExp, regExp = _j === void 0 ? copyRegExp : _j, _k = options.set, set = _k === void 0 ? copySet : _k; | ||
var _a = options.array, array = _a === void 0 ? copyArrayLoose : _a, _b = options.arrayBuffer, arrayBuffer = _b === void 0 ? copyArrayBuffer : _b, _c = options.blob, blob = _c === void 0 ? copyBlob : _c, _d = options.dataView, dataView = _d === void 0 ? copyDataView : _d, _e = options.date, date = _e === void 0 ? copyDate : _e, _f = options.error, error = _f === void 0 ? copySelf : _f, _g = options.map, map = _g === void 0 ? copyMapLoose : _g, _h = options.object, object = _h === void 0 ? copyObjectLoose : _h, _j = options.regExp, regExp = _j === void 0 ? copyRegExp : _j, _k = options.set, set = _k === void 0 ? copySetLoose : _k; | ||
var tagSpecificCopiers = { | ||
Array: array, | ||
ArrayBuffer: arrayBuffer, | ||
@@ -300,2 +315,14 @@ Blob: blob, | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
function createStrictCopier(options) { | ||
return createCopier(Object.assign({ | ||
array: copyArrayStrict, | ||
map: copyMapStrict, | ||
object: copyObjectStrict, | ||
set: copySetStrict, | ||
}, options)); | ||
} | ||
/** | ||
* Copy an value deeply as much as possible, where strict recreation of object properties | ||
@@ -305,6 +332,3 @@ * are maintained. All properties (including non-enumerable ones) are copied with their | ||
*/ | ||
var copyStrict = createCopier({ | ||
array: copyObjectStrict, | ||
object: copyObjectStrict, | ||
}); | ||
var copyStrict = createStrictCopier({}); | ||
/** | ||
@@ -317,2 +341,3 @@ * Copy an value deeply as much as possible. | ||
exports.createCopier = createCopier; | ||
exports.createStrictCopier = createStrictCopier; | ||
exports["default"] = index; | ||
@@ -319,0 +344,0 @@ |
@@ -10,2 +10,3 @@ import type { Cache } from './utils'; | ||
export declare function copyArrayLoose(array: any[], state: State): any; | ||
export declare function copyArrayStrict<Value extends any[]>(array: Value, state: State): Value; | ||
export declare function copyArrayBuffer<Value extends ArrayBuffer>(arrayBuffer: Value, _state: State): Value; | ||
@@ -15,3 +16,4 @@ export declare function copyBlob<Value extends Blob>(blob: Value, _state: State): Value; | ||
export declare function copyDate<Value extends Date>(date: Value, state: State): Value; | ||
export declare function copyMap<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapLoose<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
export declare function copyMapStrict<Value extends Map<any, any>>(map: Value, state: State): Value; | ||
declare function copyObjectLooseModern<Value extends {}>(object: Value, state: State): Value; | ||
@@ -30,3 +32,4 @@ /** | ||
export declare function copySelf<Value>(value: Value, _state: State): Value; | ||
export declare function copySet<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetLoose<Value extends Set<any>>(set: Value, state: State): Value; | ||
export declare function copySetStrict<Value extends Set<any>>(set: Value, state: State): Value; | ||
export {}; |
@@ -20,2 +20,7 @@ import type { InternalCopier } from './copier'; | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
export declare function createStrictCopier(options: CreateCopierOptions): <Value>(value: Value) => Value; | ||
/** | ||
* Copy an value deeply as much as possible, where strict recreation of object properties | ||
@@ -22,0 +27,0 @@ * are maintained. All properties (including non-enumerable ones) are copied with their |
@@ -22,7 +22,7 @@ export interface Cache { | ||
export declare const getRegExpFlags: typeof getRegExpFlagsModern; | ||
declare function getTagLegacy(value: any): string; | ||
declare function getTagModern(value: any): string; | ||
/** | ||
* Get the tag of the value passed, so that the correct copier can be used. | ||
*/ | ||
export declare const getTag: typeof getTagLegacy; | ||
export declare const getTag: typeof getTagModern; | ||
export {}; |
@@ -48,1 +48,9 @@ interface Cache { | ||
): <Value>(value: Value) => Value; | ||
/** | ||
* Create a custom copier based on the object-specific copy methods passed, defaulting to the | ||
* same internals as `copyStrict`. | ||
*/ | ||
export function createStrictCopier( | ||
options: CreateCopierOptions | ||
): <Value>(value: Value) => Value; |
@@ -102,3 +102,3 @@ { | ||
"types": "index.d.ts", | ||
"version": "3.0.0-beta.6" | ||
"version": "3.0.0-beta.7" | ||
} |
@@ -18,2 +18,3 @@ # fast-copy | ||
- [`createCopier`](#createcopier) | ||
- [`createStrictCopier`](#createstrictcopier) | ||
- [Copier methods](#copier-methods) | ||
@@ -88,3 +89,3 @@ - [Types supported](#types-supported) | ||
Create a custom copier based on the object-specific methods passed. This is useful if you want to squeeze out maximum performance. | ||
Create a custom copier based on the type-specific methods passed. This is useful if you want to squeeze out maximum performance, or perform something other than a standard deep copy. | ||
@@ -94,15 +95,41 @@ ```ts | ||
const shallowCloneArray = (array) => [...array]; | ||
const shallowCloneMap = (map) => new Map(map.entries()); | ||
const shallowCloneObject = (object) => { ...object }; | ||
const shallowCloneSet = (set) => new Set(set.values()); | ||
const copyShallow = createCopier({ | ||
array: shallowCloneArray, | ||
map: shallowCloneMap, | ||
object: shallowCloneObject, | ||
set: shallowCloneSet, | ||
array: (array) => [...array], | ||
map: (map) => new Map(map.entries()), | ||
object: (object) => ({ ...object }), | ||
set: (set) => new Set(set.values()), | ||
}); | ||
``` | ||
### `createStrictCopier` | ||
Create a custom copier based on the type-specific methods passed, but defaulting to the same functions normally used for `copyStrict`. This is useful if you want to squeeze out better performance while maintaining strict requirements, or perform something other than a strict deep copy. | ||
```ts | ||
const createStrictClone = (value, clone) => | ||
Object.getOwnPropertyNames(value).reduce( | ||
(clone, property) => | ||
Object.defineProperty( | ||
clone, | ||
property, | ||
Object.getOwnPropertyDescriptor(value, property) || { | ||
configurable: true, | ||
enumerable: true, | ||
value: clone[property], | ||
writable: true, | ||
} | ||
), | ||
clone | ||
); | ||
const copyStrictShallow = createStrictCopier({ | ||
array: (array) => createStrictClone(array, []), | ||
map: (map) => createStrictClone(map, new Map(map.entries())), | ||
object: (object) => createStrictClone(object, {}), | ||
set: (set) => createStrictClone(set, new Set(set.values())), | ||
}); | ||
``` | ||
**NOTE**: This method is significantly slower than [`copy`](#copy), so it is recommended to only use this when you have specific use-cases that require it. | ||
#### Copier methods | ||
@@ -109,0 +136,0 @@ |
@@ -9,3 +9,3 @@ import baseConfig from './config.base'; | ||
...baseConfig.output, | ||
file: pkg.browser.replace('umd', 'min').replace('.js', '.min.js'), | ||
file: pkg.browser.replace('umd', 'min'), | ||
format: 'umd', | ||
@@ -12,0 +12,0 @@ }, |
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
173515
1568
294