Skip to content

clone ​

Deep clones an array or object.

NOTE

Dates, maps, sets and regular expressions are cloned as themselves. Class instances are not - they are copied as plain objects, and circular references are not supported.

Usage ​

Arrays ​

ts
import { clone } from 'tsu'

const array = [1, 2, 3]
const clonedArray = clone(array)
// clonedArray: [1, 2, 3]

array === clonedArray
// false

Objects ​

ts
import { clone } from 'tsu'

const object = { frog: 'good' }
const clonedObject = clone(object)
// clonedObject: { frog: 'good' }

object === clonedObject
// false

Dates, maps, sets and regular expressions ​

ts
import { clone } from 'tsu'

const map = new Map([['frog', { sound: 'ribbit' }]])
const clonedMap = clone(map)

clonedMap.get('frog')
// { sound: 'ribbit' }

clonedMap.get('frog') === map.get('frog')
// false

Type Definitions ​

ts
/**
 * @param input - The array or object to clone.
 * @returns A deep clone of the input array or object.
 */
function clone<T>(input: T): T

Released under the MIT License.