
Research
NPM targeted by malware campaign mimicking familiar library names
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.
deep-clone-map
Advanced tools
Install | API | Usage | Tests | TypeScript
Deep Clone Map maps any object or array and transforms its primitive values, always returning a new instance, it can map deeply nested values in complex objects and arrays. Think of it as Array.prototype.map() on steriods, capable to map objects and deeply nested structure.
Most existing libraries do not map values in arrays, and in nested complex structures combining both objects and arrays. Typescript support is also one of the lacking features of most existing libraries.
Deep Clone Map size is really tiny only 242 bytes minified and gzipped.
Deep Clone Map has a performance on par with other popular alternatives, but it offers more: TypeScript support and mapping complex structures with nested arrays.
Some benchmarks running on MacOS Catalina and Node v12.13.0 using benchmark library:
deep-clone-map | 1,433,245 ops/sec ±0.53% (92 runs sampled) |
deep-map | 1,131,833 ops/sec ±0.66% (88 runs sampled) |
map-obj | 1,344,719 ops/sec ±1.25% (87 runs sampled) |
npm:
npm install --save deep-clone-map
yarn:
yarn add deep-clone-map
deepCloneMap(any, mapFn?)
Argument | Type | Description |
---|---|---|
arg0 | any | Any object, array or primitive whose values should be resolved. |
arg1? | (arg0: any, key: string) => any |
Callback used to transform primitive values, this parameter is optional if skipped, the object/array will be just deeply cloned instead.
Arguments:
|
Returns a new deeply cloned version of the input argument value, it will maintain the exact same structure as the original object or array. In case if a primitive is provided as the first argument it will map its value to a new one, based on the callback function.
import deepCloneMap from 'deep-clone-map'
import deepCloneMap from 'deep-clone-map/es5'
const obj = {
a: 1,
b: 2,
c: {
a: 1,
b: 2,
c: [1, 2, 3],
},
}
const newObj = deepCloneMap(obj)
// newObj !== obj && newObj.c !== obj.c && newObj.c.c !== obj.c.c
const arr = [
[1, 2, 3],
[
{
a: 1,
b: 2,
c: [1, 2, 3],
},
],
]
const newArr = deepCloneMap(arr)
// newArr !== arr && newArr[1] !== arr[1] && newArr[1].c !== arr[1].c
const obj = {
a: 1,
b: 2,
c: {
a: 1,
b: 2,
c: [1, 2, 3],
},
}
const newObj = deepCloneMap(obj, val => val + 1)
/*
newObj => {
a: 2,
b: 3,
c: {
a: 2,
b: 3,
c: [2, 4, 4]
}
}
*/
const obj = {
a: 1,
b: 2,
c: {
a: 1,
b: 2,
c: [1, 2, 3],
},
}
const newObj = deepCloneMap(obj, (val, key) => {
switch (key) {
case 'a':
return 10
case 'c.b':
return 20
case 'c.c[1]':
return 20
default:
return val
}
})
/*
newObj => {
a: 10,
b: 2,
c: {
a: 1,
b: 20,
c: [1, 20, 3]
}
}
*/
const arr = [
[1, 2, 3],
[
{
a: 1,
b: [1, 2, 3],
c: [
{
a: 1,
b: [1, 2, 3],
},
],
},
],
]
const newArr = deepCloneMap(arr, val => val + 1)
/*
newArr => [
[2, 3, 4],
[
{
a: 2,
b: [2, 3, 4],
c: [
{
a: 2,
b: [2, 3, 4]
}
]
}
]
]
*/
In order to run the provided unit tests:
# yarn
yarn test
# npm
npm test
The packages comes with typescript declarations included in the package, you only need to import the module normally.
By default the types are infered from the input argument:
const obj = {
a: 1,
b: 2,
}
const newObj = deepCloneMap(obj)
/*
newObj => {
a: number
b: number
}
*/
In some cases you will need to provide a different type to the deepCloneMap
function, for example in instances when you map the primitive values to a different type:
const obj = {
a: 1,
b: 2
}
const newObj = deepCloneMap<{ a: string; b: string }>(obj, val => String(val))
FAQs
Deep clone and map complex nested objects
We found that deep-clone-map demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Research
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.
Research
Socket's research uncovers three dangerous Go modules that contain obfuscated disk-wiping malware, threatening complete data loss.
Research
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.