Deeply clone the given object or array. The only nested objects that get cloned by default are: plain objects, non-native class instances, arrays, Map instances, and Set instances.
The default behavior aims to support the most popular use cases. See “Customized cloning” below if you need more control.
By default, non-enumerable properties and computed properties are copied lossless-ly. Note that you can opt out of this behavior if you need better performance (see “Faster cloning” below).
import*as
import _
_from'radashi'
const
constobj: {
a:number;
b: {
c:number;
};
}
obj= {
a: number
a: 1,
b: {
c: number;
}
b: {
c: number
c: 2 } }
const
constclone: {
a:number;
b: {
c:number;
};
}
clone=
import _
_.
cloneDeep<{
a:number;
b: {
c:number;
};
}>(root: {
a:number;
b: {
c:number;
};
}, customStrategy?:Partial<_.CloningStrategy>): {
a:number;
b: {
c:number;
};
}
export cloneDeep
Clone the given object and possibly other objects nested inside.
By default, the only objects that get cloned are plain objects,
non-native class instances, arrays, Set instances, and Map
instances. If an object is not cloned, any objects nested inside
are also not cloned.
You may define a custom cloning strategy by passing a partial
implementation of the CloningStrategy interface to the
cloneDeep function. Any undefined methods will fall back to the
default cloning logic. Your own methods may return null to indicate
that the default cloning logic should be used. They may also return
the input object to indicate that cloning should be skipped.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
You can pass the FastCloningStrategy for better performance, but bear in mind the following tradeoff.
All plain objects and class instances are cloned with {...obj}. This means that the original prototype, computed properties, and non-enumerable properties are not preserved.
Also note that built-in, complex objects like RegExp and Date are still not cloned with this cloning strategy. You can override the cloneOther function if you need to clone these object types.
Customized cloning
“Cloning strategies” control how certain object types are handled by cloneDeep. You can pass in a custom strategy, which may even be a partial strategy. Any undefined methods in your strategy will inherit the default logic. Your custom methods can return null to use the default logic, or they can return the received object to skip cloning.
import*as
import _
_from'radashi'
import _
_.
cloneDeep<any>(root: any, customStrategy?: Partial<_.CloningStrategy>): any
export cloneDeep
Clone the given object and possibly other objects nested inside.
By default, the only objects that get cloned are plain objects,
non-native class instances, arrays, Set instances, and Map
instances. If an object is not cloned, any objects nested inside
are also not cloned.
You may define a custom cloning strategy by passing a partial
implementation of the CloningStrategy interface to the
cloneDeep function. Any undefined methods will fall back to the
default cloning logic. Your own methods may return null to indicate
that the default cloning logic should be used. They may also return
the input object to indicate that cloning should be skipped.
// Clone arrays with default logic if they are not frozen.
cloneArray:
array: readonly T[]
array=> (
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.
ObjectConstructor.isFrozen(o: any): boolean
Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.
@param ― o Object to test.
isFrozen(
array: readonly T[]
array) ?
array: readonly T[]
array:null),
Error ts(2322) ― Type '<T>(array: readonly T[]) => readonly T[] | null' is not assignable to type '<T>(parent: readonly T[], track: (newParent: T[]) => T[], clone: <T>(value: T) => T) => T[] | null'.
Type 'readonly T[] | null' is not assignable to type 'T[] | null'.
The type 'readonly T[]' is 'readonly' and cannot be assigned to the mutable type 'T[]'.
})
If you clone the object in your custom method, make sure to pass the clone into the track function before cloning the nested objects. Here’s an example with cloneOther that handles a custom class instance.
import*as _ from'radashi'
_.cloneDeep(obj, {
cloneOther: (obj, track, clone) => {
if (obj instanceofMyClass) {
// 1. Create a new instance and track it.
constclone=track(newMyClass())
// 2. Copy over the properties of the original instance.