@se-oss/deep-clone
v1.0.0
Published
A utility for deep cloning objects.
Maintainers
Readme
@se-oss/deep-clone
@se-oss/deep-clone is a utility for deeply cloning objects.
📦 Installation
npm install @se-oss/deep-clonepnpm
pnpm install @se-oss/deep-cloneyarn
yarn add @se-oss/deep-clone📖 Usage
Basic Usage
import { deepClone } from '@se-oss/deep-clone';
const original = {
a: 1,
b: { c: 2, d: new Date() },
e: [3, { f: 4 }],
g: /pattern/g,
};
const cloned = deepClone(original);
console.log(cloned !== original); // true
console.log(cloned.b !== original.b); // true
console.log(cloned.b.d !== original.b.d); // true (Date cloned)Circular References
const obj: any = {};
obj.a = obj;
const cloned = deepClone(obj);
console.log(cloned !== obj); // true
console.log(cloned.a === cloned); // trueDepth Limiting
const nested = { a: { b: { c: 1 } } };
// Shallow clone (depth 0)
const shallow = deepClone(nested, { maxDepth: 0 });
console.log(shallow.a === nested.a); // true
// Clone 1 level deep
const oneLevel = deepClone(nested, { maxDepth: 1 });
console.log(oneLevel.a === nested.a); // false
console.log(oneLevel.a.b === nested.a.b); // true📚 Documentation
For all configuration options, please see the API docs.
🤝 Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
License
MIT © Shahrad Elahi and contributors.
