A high-performance, type-safe deep cloning utility for TypeScript and JavaScript applications.
- π High Performance Optimized for speed and minimal memory footprint
- π Circular References Handles circular references in objects by default
- π Comprehensive Support Clones objects, arrays, dates, RegExp, Maps, Sets, and more
- π‘οΈ Type Safety Full TypeScript support with accurate type preservation
- π§ Configurable Control clone depth and prototype handling
- π ES6+ Support Works with modern JavaScript features like Map, Set, Symbol, and Promise
# Using npm npm install ts-clone # Using yarn yarn add ts-clone # Using pnpm pnpm add ts-clone # Using bun bun add ts-clone
import { clone } from 'ts-clone' // Simple cloning const original = { name: 'John', roles: ['admin', 'user'] } const cloned = clone(original) // Handles circular references const circular = { prop: 'value' } circular.self = circular const clonedCircular = clone(circular) // Works without infinite loops // Clone with specified depth const nested = { level1: { level2: { level3: 'deep' } } } const shallow = clone(nested, true, 1) // Only clones the first level // Clone with options object const obj = { hidden: 'property' } Object.defineProperty(obj, 'hidden', { enumerable: false }) const withNonEnumerable = clone(obj, { includeNonEnumerable: true })
// Clone RegExp objects with their flags and lastIndex const regex = /pattern/gi regex.lastIndex = 5 const clonedRegex = clone(regex) console.log(clonedRegex.source) // 'pattern' console.log(clonedRegex.flags) // 'gi' console.log(clonedRegex.lastIndex) // 5 // Clone ES6+ objects const map = new Map([['key', 'value']]) const set = new Set(['item1', 'item2']) const promise = Promise.resolve('data') const clonedMap = clone(map) const clonedSet = clone(set) const clonedPromise = clone(promise) // Clone with prototype handling const proto = { shared: 'value' } const instance = Object.create(proto) instance.own = 'property' // Clone with original prototype const clonedWithProto = clone(instance) console.log(clonedWithProto.shared) // 'value' // Clone with custom prototype const clonedCustomProto = clone(instance, true, Infinity, {}) console.log(clonedCustomProto.shared) // undefined
// Clone just the prototype const proto = { method() { return 'result' } } const protoClone = clone.clonePrototype(proto) // Type checking utilities clone.**isArray([1, 2, 3]) // true clone.**isDate(new Date()) // true clone.**isRegExp(/pattern/) // true // RegExp flags extraction clone.**getRegExpFlags(/pattern/gi) // 'gim'
- Deep Copying Objects: Create true copies without reference sharing
- API Response Processing: Safely modify cloned API responses without side effects
- State Management: Immutable state updates in frameworks like React/Redux
- Object Serialization: Pre-process objects before serialization
- Defensive Programming: Protect internal data structures from external modifications
Please see the Contributing Guide for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
βSoftware that is free, but hopes for a postcard.β We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States π
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
- Original
clonemodule for the initial implementation inspiration - Chris Breuer
- All Contributors
The MIT License (MIT). Please see LICENSE for more information.
Made with π
