Skip to content

Commit

Permalink
improve JSDoc for copiers
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Oct 1, 2022
1 parent a4a8d67 commit f594106
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/copier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ function getStrictPropertiesModern(object: any): Array<string | symbol> {
);
}

/**
* Get the properites used when copying objects strictly. This includes both keys and symbols.
*/
const getStrictProperties = SUPPORTS_SYMBOL
? getStrictPropertiesModern
: getOwnPropertyNames;

/**
* Striclty copy all properties contained on the object.
*/
function copyOwnPropertiesStrict<Value extends any>(
value: Value,
clone: Value,
Expand Down Expand Up @@ -74,6 +80,9 @@ function copyOwnPropertiesStrict<Value extends any>(
return clone;
}

/**
* Deeply copy the indexed values in the array.
*/
export function copyArrayLoose(array: any[], state: State) {
const clone = new state.Constructor();

Expand All @@ -87,6 +96,9 @@ export function copyArrayLoose(array: any[], state: State) {
return clone;
}

/**
* Deeply copy the indexed values in the array, as well as any custom properties.
*/
export function copyArrayStrict<Value extends any[]>(
array: Value,
state: State
Expand All @@ -99,31 +111,46 @@ export function copyArrayStrict<Value extends any[]>(
return copyOwnPropertiesStrict(array, clone, state);
}

/**
* Copy the contents of the ArrayBuffer.
*/
export function copyArrayBuffer<Value extends ArrayBuffer>(
arrayBuffer: Value,
_state: State
): Value {
return arrayBuffer.slice(0) as Value;
}

/**
* Create a new Blob with the contents of the original.
*/
export function copyBlob<Value extends Blob>(
blob: Value,
_state: State
): Value {
return blob.slice(0, blob.size, blob.type) as Value;
}

/**
* Create a new DataView with the contents of the original.
*/
export function copyDataView<Value extends DataView>(
dataView: Value,
state: State
): Value {
return new state.Constructor(copyArrayBuffer(dataView.buffer, state));
}

/**
* Create a new Date based on the time of the original.
*/
export function copyDate<Value extends Date>(date: Value, state: State): Value {
return new state.Constructor(date.getTime());
}

/**
* Deeply copy the keys and values of the original.
*/
export function copyMapLoose<Value extends Map<any, any>>(
map: Value,
state: State
Expand All @@ -140,6 +167,9 @@ export function copyMapLoose<Value extends Map<any, any>>(
return clone;
}

/**
* Deeply copy the keys and values of the original, as well as any custom properties.
*/
export function copyMapStrict<Value extends Map<any, any>>(
map: Value,
state: State
Expand Down Expand Up @@ -198,16 +228,15 @@ function copyObjectLooseModern<Value extends {}>(
}

/**
* Get a copy of the object based on loose rules, meaning all enumerable keys
* and symbols are copied, but property descriptors are not considered.
* Deeply copy the properties (keys and symbols) and values of the original.
*/
export const 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.
* Deeply copy the properties (keys and symbols) and values of the original, as well
* as any hidden or non-enumerable properties.
*/
export function copyObjectStrict<Value extends {}>(
object: Value,
Expand All @@ -221,6 +250,9 @@ export function copyObjectStrict<Value extends {}>(
return copyOwnPropertiesStrict(object, clone, state);
}

/**
* Create a new RegExp based on the value and flags of the original.
*/
export function copyRegExp<Value extends RegExp>(
regExp: Value,
state: State
Expand All @@ -235,10 +267,19 @@ export function copyRegExp<Value extends RegExp>(
return clone;
}

/**
* Return the original value (an identity function).
*
* @note
* THis is used for objects that cannot be copied, such as WeakMap.
*/
export function copySelf<Value>(value: Value, _state: State): Value {
return value;
}

/**
* Deeply copy the values of the original.
*/
export function copySetLoose<Value extends Set<any>>(
set: Value,
state: State
Expand All @@ -255,6 +296,9 @@ export function copySetLoose<Value extends Set<any>>(
return clone;
}

/**
* Deeply copy the values of the original, as well as any custom properties.
*/
export function copySetStrict<Value extends Set<any>>(
set: Value,
state: State
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const DEFAULT_STRICT_OPTIONS: Required<CreateCopierOptions> = assign(
}
);

/**
* Get the copiers used for each specific object tag.
*/
function getTagSpecificCopiers(
options: Required<CreateCopierOptions>
): Record<string, InternalCopier<any>> {
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const { toString: toStringFunction } = Function.prototype;
const { create } = Object;
const { toString: toStringObject } = Object.prototype;

/**
* @classdesc Fallback cache for when WeakMap is not natively supported
*/
class LegacyCache {
private _keys: any[] = [];
private _values: any[] = [];
Expand Down

0 comments on commit f594106

Please sign in to comment.