Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
copy-anything
Advanced tools
The 'copy-anything' npm package is a utility for deep cloning JavaScript objects, arrays, and other data types. It ensures that the copied data is a completely new instance, preventing unintended mutations in the original data.
Deep Cloning Objects
This feature allows you to create a deep clone of an object, ensuring that nested objects are also cloned and not just referenced.
const copy = require('copy-anything');
const original = { a: 1, b: { c: 2 } };
const cloned = copy(original);
console.log(cloned); // { a: 1, b: { c: 2 } }
Deep Cloning Arrays
This feature allows you to create a deep clone of an array, ensuring that nested arrays and objects within the array are also cloned.
const copy = require('copy-anything');
const originalArray = [1, [2, 3], { a: 4 }];
const clonedArray = copy(originalArray);
console.log(clonedArray); // [1, [2, 3], { a: 4 }]
Handling Special Data Types
This feature ensures that special data types like Date objects are properly cloned, creating new instances with the same values.
const copy = require('copy-anything');
const original = new Date();
const cloned = copy(original);
console.log(cloned); // A new Date instance with the same value as original
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a deep cloning function (`_.cloneDeep`) that offers similar functionality to 'copy-anything'. However, Lodash is a much larger library with many additional features beyond cloning.
The 'clone-deep' package is specifically designed for deep cloning objects and arrays. It offers similar functionality to 'copy-anything' but focuses solely on deep cloning without additional utilities.
The 'rfdc' (Really Fast Deep Clone) package is a lightweight and fast deep cloning library. It provides similar deep cloning capabilities as 'copy-anything' but is optimized for performance, making it a good choice for performance-critical applications.
An optimised way to copy'ing an object. A small and simple integration.
I created this package because I tried a lot of similar packages that do copy'ing/cloning of objects. But all had its quirks, and all of them break things they are not supposed to break... 😞
I was looking for:
JSON.parse(JSON.stringify(object))
This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to copy them inproperly. So we gotta be careful!
copy-anything will copy objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️
import copy from 'copy-anything'
const target = {name: 'Ditto', type: {water: true}}
const copy = copy(target)
// now if we change a nested prop like the type:
copy.type.water = false
copy.type.fire = true
// then the original object will still be the same:
// target.type.water === true
// target.type.fire === undefined
The source code is literally just these lines. Most of the magic comes from the isPlainObject function from the is-what library.
import { isPlainObject } from 'is-what'
/**
* Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object.
*
* @export
* @param {*} target Target can be anything
* @returns {*} the target with replaced values
*/
export default function copy (target: any): any {
if (!isPlainObject(target)) return target
return Object.keys(target)
.reduce((carry, key) => {
const val = target[key]
carry[key] = copy(val)
return carry
}, {})
}
FAQs
An optimised way to copy'ing an object. A small and simple integration
We found that copy-anything demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.