README
npm('clone.js')
A NPM package wrapper around a lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects.
Original repository, clone.js.
✨Since version 1.1.0
clone
is a hybrid module that supports both CommonJS (legacy) and ES modules, thanks to Modern Module.
Usage
Install
npm install --save node-clone-js
Simple array copy
const clone = require('node-clone-js')
let firstArray = [1, 2, 3]
let 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)
const clone = require('node-clone-js')
let firstArray = [
{
id: '103',
name: 'Peter',
},
{
id: '214',
name: 'Eve',
},
]
let secondArray = clone(firstArray)
secondArray[0].name = 'John'
console.log('firstArray => ', firstArray)
console.log('secondArray => ', secondArray)
Simple object copy
const clone = require('node-clone-js')
let firstStudent = {
id: 103,
name: 'Ben',
classes: ['Maths', 'Science', 'English language'],
}
let secondStudent = clone(firstStudent)
secondStudent.classes[0] = 'Psychology'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)
Nested object copy
const clone = require('node-clone-js')
let firstStudent = {
id: 103,
name: 'Ben',
subjects: {
groupDke: {
science: 'B',
maths: 'C',
},
groupOpe: {
foo: 'bar',
},
},
}
let secondStudent = clone(firstStudent)
secondStudent.subjects.groupDke.maths = 'B'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)
Development
git clone https://github.com/igorskyflyer/npm-clone-js.git
cd npm-clone-js
npm install
Test
npm test
Benchmark
npm run benchmark