@igorskyflyer/clone
v1.0.1
Published
🧬 A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects. 🪁
Downloads
4
Maintainers
Readme
📃 Table of Contents
🤖 Features
- 🧬 Deep‑clones objects and arrays with full fidelity
- 🔁 Recursively copies nested structures without mutation
- 🛡️ Preserves original data while enabling safe edits
- 🧠 Smart handling of null and non‑object values
- 🧩 Works with mixed arrays, primitives, and objects
- ⚙️ Zero dependencies - drop‑in and lightweight
- 🚀 Ideal for state snapshots, undo systems, or sandboxing
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/cloneyarn add @igorskyflyer/clonenpm i @igorskyflyer/clone🤹🏼 API
Simple array copy
import { clone } from '@igorskyflyer/clone'
const firstArray = [1, 2, 3]
const secondArray = clone(firstArray)
secondArray[0] = 5
console.log('firstArray => ', firstArray) // [1, 2, 3]
console.log('secondArray => ', secondArray) // [5, 2, 3]Complex array (array of objects)
import { clone } from '@igorskyflyer/clone'
const firstArray = [
{
id: '103',
name: 'Peter',
},
{
id: '214',
name: 'Eve',
},
]
const secondArray = clone(firstArray)
secondArray[0].name = 'John'
console.log('firstArray => ', firstArray)
console.log('secondArray => ', secondArray)Simple object copy
import { clone } from '@igorskyflyer/clone'
const firstStudent = {
id: 103,
name: 'Ben',
classes: ['Maths', 'Science', 'English language'],
}
const secondStudent = clone(firstStudent)
secondStudent.classes[0] = 'Psychology'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)Nested object copy
import { clone } from '@igorskyflyer/clone'
const firstStudent = {
id: 103,
name: 'Ben',
subjects: {
groupDke: {
science: 'B',
maths: 'C',
},
groupOpe: {
foo: 'bar',
},
},
}
const secondStudent = clone(firstStudent)
secondStudent.subjects.groupDke.maths = 'B'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)📝 Changelog
📑 The changelog is available here, CHANGELOG.md.
🪪 License
Licensed under the MIT license which is available here, MIT license.
💖 Support
🧬 Related
🧲 Provides ways of properly checking if a path exists inside a given array of files/directories both on Windows and UNIX-like operating systems. 🗺
🧠 Zep is a zero-dependency, efficient debounce module. ⏰
🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜
🌟 An npm module that strongly types file paths! 🥊
@igorskyflyer/recursive-readdir
📖 Provides recursive readdir() and readdirSync() functions. 📁
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
