deep-clone-map
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -5,3 +5,3 @@ "use strict"; | ||
// @ts-ignore | ||
const nObj = o.constructor === Array ? [...o] : { ...o }; | ||
const n = o.constructor === Array ? [...o] : { ...o }; | ||
(function t(obj, prevKey = '') { | ||
@@ -20,6 +20,6 @@ // @ts-ignore | ||
} | ||
})(nObj); | ||
return nObj; | ||
})(n); | ||
return n; | ||
} | ||
exports.default = deepCloneMap; | ||
//# sourceMappingURL=main.js.map |
@@ -1,2 +0,5 @@ | ||
import deepCloneMap from '.' | ||
// import deepCloneMap from '.' | ||
// const deepMap = require('deep-map') | ||
// const mapObj = require('map-obj') | ||
import mapObj from './benchmark/node_modules/map-obj' | ||
import { | ||
@@ -15,4 +18,8 @@ testObj1, | ||
const deepCloneMap = (o, cb) => { | ||
return mapObj(o, (key, val) => [cb(val), key], { deep: true }) | ||
} | ||
describe('objects', () => { | ||
it('should parse and map a deeply nested object', () => { | ||
it.only('should parse and map a deeply nested object', () => { | ||
const newObj = deepCloneMap(testObj1, val => val + 1) | ||
@@ -112,3 +119,3 @@ | ||
it('should deeply clone an array', () => { | ||
const newArr = deepCloneMap(testArr1) | ||
const newArr = deepCloneMap(testArr1, val => val) | ||
@@ -123,3 +130,3 @@ expect(newArr === testArr1).toBeFalsy() | ||
const testRef = testArr1 | ||
const newArr = deepCloneMap(testRef) | ||
const newArr = deepCloneMap(testRef, val => val) | ||
@@ -126,0 +133,0 @@ expect(testArr1 === testRef).toBeTruthy() |
@@ -7,3 +7,3 @@ type Param = object | any[] | string | number | null | ||
// @ts-ignore | ||
const nObj = o.constructor === Array ? [...o] : { ...o } | ||
const n = o.constructor === Array ? [...o] : { ...o } | ||
@@ -23,7 +23,7 @@ ;(function t(obj, prevKey = '') { | ||
} | ||
})(nObj) | ||
})(n) | ||
return nObj | ||
return n | ||
} | ||
export default deepCloneMap |
{ | ||
"name": "deep-clone-map", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Deep clone and map complex nested objects", | ||
@@ -33,6 +33,3 @@ "main": "dist/main.js", | ||
"typescript": "^3.8.3" | ||
}, | ||
"dependencies": { | ||
"fast-memoize": "^2.5.2" | ||
} | ||
} |
191
README.md
@@ -5,3 +5,3 @@ # Deep Clone Map | ||
[Install](#install) | [API](#api) | [Usage](#usage) | [Tests](#tests) | [TypeScript](#typescript) | ||
[Install](#install) | [API](#api) | [Usage](#usage) | [Tests](#tests) | [TypeScript](#typescript) | ||
@@ -11,12 +11,46 @@ <b>Deep Clone Map</b> 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) on steriods, capable to map objects and deeply nested structure. | ||
#### Differences between other deep map libraries | ||
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. | ||
#### Size | ||
<b>Deep Clone Map</b> size is really tiny only <b>242 bytes</b> minified and gzipped. | ||
#### Performance | ||
<b>Deep Clone Map</b> has a performance on par with other popular alternatives, but it offers more: [TypeScript](#typescript) support and mapping complex structures with nested arrays. | ||
Some benchmarks running on MacOS Catalina and Node v12.13.0 using benchmark library: | ||
<table> | ||
<tbody> | ||
<tr> | ||
<td>deep-clone-map</td> | ||
<td>1,433,245 ops/sec ±0.53% (92 runs sampled) | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>deep-map</td> | ||
<td>1,131,833 ops/sec ±0.66% (88 runs sampled) | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>map-obj</td> | ||
<td>1,344,719 ops/sec ±1.25% (87 runs sampled) | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
## Install | ||
npm: | ||
```sh | ||
npm install --save deep-clone-map | ||
``` | ||
yarn: | ||
```sh | ||
@@ -75,2 +109,3 @@ yarn add deep-clone-map | ||
#### Import Deep Clone Map package | ||
```js | ||
@@ -81,48 +116,51 @@ import deepCloneMap from 'deep-clone-map' | ||
#### Deeply clone an object: | ||
```js | ||
const obj = { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3] | ||
} | ||
} | ||
c: [1, 2, 3], | ||
}, | ||
} | ||
const newObj = deepCloneMap(obj) | ||
// newObj !== obj && newObj.c !== obj.c && newObj.c.c !== obj.c.c | ||
const newObj = deepCloneMap(obj) | ||
// newObj !== obj && newObj.c !== obj.c && newObj.c.c !== obj.c.c | ||
``` | ||
#### Deeply clone an array: | ||
```js | ||
const arr = [ | ||
[1, 2, 3], | ||
[ | ||
{ | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3] | ||
} | ||
] | ||
] | ||
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 newArr = deepCloneMap(arr) | ||
// newArr !== arr && newArr[1] !== arr[1] && newArr[1].c !== arr[1].c | ||
``` | ||
#### Deeply map an object | ||
```js | ||
const obj = { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3] | ||
} | ||
} | ||
c: [1, 2, 3], | ||
}, | ||
} | ||
const newObj = deepCloneMap(obj, val => val + 1) | ||
/* | ||
const newObj = deepCloneMap(obj, val => val + 1) | ||
/* | ||
newObj => { | ||
@@ -141,26 +179,27 @@ a: 2, | ||
#### Deeply custom map an object based on the key | ||
```js | ||
const obj = { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3] | ||
} | ||
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 | ||
} | ||
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 => { | ||
@@ -179,21 +218,22 @@ a: 10, | ||
#### Deeply map a nested array | ||
```js | ||
const arr = [ | ||
[1, 2, 3], | ||
[ | ||
{ | ||
a: 1, | ||
b: [1, 2, 3], | ||
c: [ | ||
{ | ||
a: 1, | ||
b: [1, 2, 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) | ||
/* | ||
const newArr = deepCloneMap(arr, val => val + 1) | ||
/* | ||
newArr => [ | ||
@@ -220,2 +260,3 @@ [2, 3, 4], | ||
In order to run the provided unit tests: | ||
```sh | ||
@@ -234,10 +275,11 @@ # yarn | ||
By default the types are infered from the input argument: | ||
```js | ||
const obj = { | ||
a: 1, | ||
b: 2 | ||
} | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
} | ||
const newObj = deepCloneMap(obj) | ||
/* | ||
const newObj = deepCloneMap(obj) | ||
/* | ||
newObj => { | ||
@@ -251,2 +293,3 @@ a: 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: | ||
```js | ||
@@ -253,0 +296,0 @@ const obj = { |
Sorry, the diff of this file is not supported yet
1430273
0
23
42037
294
- Removedfast-memoize@^2.5.2
- Removedfast-memoize@2.5.2(transitive)