Gathering detailed insights and metrics for copy-anything
Gathering detailed insights and metrics for copy-anything
Gathering detailed insights and metrics for copy-anything
Gathering detailed insights and metrics for copy-anything
An optimised way to copy'ing (cloning) an Object or Array. A small and simple integration
npm install copy-anything
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
44 Stars
74 Commits
8 Forks
1 Watching
8 Branches
2 Contributors
Updated on 17 Nov 2024
TypeScript (97.45%)
JavaScript (2.55%)
Cumulative downloads
Total Downloads
Last day
-3.9%
1,248,200
Compared to previous day
Last week
2.1%
6,848,696
Compared to previous week
Last month
7.7%
28,838,690
Compared to previous month
Last year
36.7%
288,050,960
Compared to previous year
1
5
npm i copy-anything
An optimised way to copy'ing (cloning) an object or array. A small and simple integration.
I created this package because I tried a lot of similar packages that do copy'ing/cloning. But all had its quirks, and all of them break things they are not supposed to break... 😞
I was looking for:
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". ♻️
1import { copy } from 'copy-anything' 2 3const original = { name: 'Ditto', type: { water: true } } 4const copy = copy(original) 5 6// now if we change a nested prop like the type 7copy.type.water = false 8// or add a new nested prop 9copy.type.fire = true 10 11// then the original object will still be the same: 12(original.type.water === true) // true 13(original.type.fire === undefined) // true
Please note, by default copy-anything does not copy non-enumerable props. If you need to copy those, see the instructions further down below.
It will also clone arrays, as well as objects inside arrays! 😉
1const original = [{ name: 'Squirtle' }] 2const copy = copy(original) 3 4// now if we change a prop in the array 5copy[0].name = 'Wartortle' 6// or add a new item to the array 7copy.push({ name: 'Charmander' }) 8 9// then the original array will still be the same: 10(original[0].name === 'Squirtle') // true 11(original[1] === undefined) // true
By default, copy-anything only copies enumerable properties. If you also want to copy non-enumerable properties you can do so by passing that as an option.
1const original = { name: 'Bulbasaur' } 2// bulbasaur's ID is non-enumerable 3Object.defineProperty(original, 'id', { 4 value: '001', 5 writable: true, 6 enumerable: false, 7 configurable: true, 8}) 9const copy1 = copy(original) 10(copy1.id === undefined) // true 11 12const copy2 = copy(original, { nonenumerable: true }) 13(copy2.id === '001') // true
You can limit to specific props.
1const original = { name: 'Flareon', type: ['fire'], id: '136' } 2const copy = copy(original, { props: ['name'] }) 3 4(copy) // will look like: `{ name: 'Flareon' }`
Please note, if the props you have specified are non-enumerable, you will also need to pass
{nonenumerable: true}
.
The source code is literally just these lines. Most of the magic comes from the isPlainObject function from the is-what library.
1import { isPlainObject } from 'is-what' 2 3export function copy (target) { 4 if (isArray(target)) return target.map(i => copy(i)) 5 if (!isPlainObject(target)) return target 6 return Object.keys(target) 7 .reduce((carry, key) => { 8 const val = target[key] 9 carry[key] = copy(val) 10 return carry 11 }, {}) 12}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More