@jalik/deep-extend
Advanced tools
Comparing version 1.1.5 to 1.1.6
# Changelog | ||
## v1.1.6 | ||
- Fixes merging of arrays by cloning them in the extended object | ||
- Updates dependencies | ||
## v1.1.5 | ||
@@ -4,0 +8,0 @@ - Updates dependencies |
@@ -35,5 +35,40 @@ "use strict"; | ||
/** | ||
* Merges two arrays and returns the new one. | ||
* @param {[]} a | ||
* @param {[]} b | ||
* @param {Function} fn | ||
* @return {[]} | ||
*/ | ||
function mergeArrays(a, b, fn) { | ||
var result = []; | ||
for (var i = 0; i < a.length; i += 1) { | ||
if (typeof a !== 'undefined') { | ||
if (a[i] !== null && _typeof(a[i]) === 'object') { | ||
result[i] = fn({}, a[i]); | ||
} else { | ||
result[i] = a[i]; | ||
} | ||
} | ||
} | ||
for (var _i = 0; _i < b.length; _i += 1) { | ||
if (typeof b[_i] !== 'undefined') { | ||
if (b[_i] !== null && _typeof(b[_i]) === 'object') { | ||
result[_i] = fn(a[_i], b[_i]); | ||
} else { | ||
result[_i] = b[_i]; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* Merge deep objects | ||
* @param {*} args | ||
* @return {*} | ||
*/ | ||
function deepExtend() { | ||
@@ -54,11 +89,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
if (a instanceof Array && b instanceof Array) { | ||
for (var index = 0; index < b.length; index += 1) { | ||
if (typeof b[index] !== 'undefined') { | ||
if (b[index] !== null && _typeof(b[index]) === 'object') { | ||
a[index] = deepExtend(a[index], b[index]); | ||
} else { | ||
a[index] = b[index]; | ||
} | ||
} | ||
} | ||
a = mergeArrays(a, b, deepExtend); | ||
} else { | ||
@@ -79,3 +106,7 @@ var keys = Object.keys(b); | ||
} else if (b !== null && typeof b !== 'undefined') { | ||
a = b; | ||
if (b instanceof Array) { | ||
a = mergeArrays([], b, deepExtend); | ||
} else { | ||
a = b; | ||
} | ||
} | ||
@@ -82,0 +113,0 @@ } |
{ | ||
"name": "@jalik/deep-extend", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "A utility to merge deep objects.", | ||
@@ -34,21 +34,19 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@babel/core": "^7.8.4", | ||
"@babel/preset-env": "^7.8.4", | ||
"@babel/core": "^7.9.6", | ||
"@babel/preset-env": "^7.9.6", | ||
"acorn": "^7.1.0", | ||
"ajv": "^6.11.0", | ||
"ajv": "^6.12.2", | ||
"babel-eslint": "^10.0.3", | ||
"del": "^5.1.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb": "^18.0.1", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-jest": "^23.7.0", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-react": "^7.18.3", | ||
"eslint-plugin-react-hooks": "^1.7.0", | ||
"gulp": "^4.0.2", | ||
"gulp-babel": "^8.0.0", | ||
"gulp-eslint": "^6.0.0", | ||
"jest": "^25.1.0", | ||
"jest": "^25.5.2", | ||
"typescript": "^3.7.5" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
# @jalik/deep-extend | ||
![GitHub package.json version](https://img.shields.io/github/package-json/v/jalik/js-deep-extend.svg) | ||
@@ -23,19 +24,19 @@ [![Build Status](https://travis-ci.com/jalik/js-deep-extend.svg?branch=master)](https://travis-ci.com/jalik/js-deep-extend) | ||
const defaultColors = { | ||
cold: { | ||
blue: "#0000FF", | ||
green: "#00FF00" | ||
}, | ||
hot: { | ||
red: "#FF0000", | ||
yellow: "#FFFF00" | ||
} | ||
cold: { | ||
blue: '#0000FF', | ||
green: '#00FF00', | ||
}, | ||
hot: { | ||
red: '#FF0000', | ||
yellow: '#FFFF00', | ||
}, | ||
}; | ||
const customColors = { | ||
cold: { | ||
blue: "#48C2ED" | ||
}, | ||
hot: { | ||
yellow: "#E6CB5F" | ||
} | ||
cold: { | ||
blue: '#48C2ED', | ||
}, | ||
hot: { | ||
yellow: '#E6CB5F', | ||
}, | ||
}; | ||
@@ -46,5 +47,20 @@ | ||
// but the other colors will be the default ones. | ||
const finalColors = deepExtend({}, defaultColors, customColors); | ||
const result = deepExtend({}, defaultColors, customColors); | ||
``` | ||
The result: | ||
```json | ||
{ | ||
"cold": { | ||
"blue": "#48C2ED", | ||
"green": "#00FF00" | ||
}, | ||
"hot": { | ||
"red": "#FF0000", | ||
"yellow": "#E6CB5F" | ||
} | ||
} | ||
``` | ||
## Merging arrays | ||
@@ -59,7 +75,20 @@ | ||
const b = [undefined, [4, [undefined, 5], 6], 7]; | ||
const c = deepExtend([], a, b); | ||
// c would result to [1, [4, [3, 5], 6], 7] | ||
const result = deepExtend([], a, b); | ||
``` | ||
The result: | ||
```json | ||
[ | ||
1, | ||
[ | ||
4, | ||
[3, 5], | ||
6 | ||
], | ||
7 | ||
] | ||
``` | ||
## Changelog | ||
@@ -66,0 +95,0 @@ |
@@ -26,3 +26,36 @@ /* | ||
/** | ||
* Merges two arrays and returns the new one. | ||
* @param {[]} a | ||
* @param {[]} b | ||
* @param {Function} fn | ||
* @return {[]} | ||
*/ | ||
function mergeArrays(a, b, fn) { | ||
const result = []; | ||
for (let i = 0; i < a.length; i += 1) { | ||
if (typeof a !== 'undefined') { | ||
if (a[i] !== null && typeof a[i] === 'object') { | ||
result[i] = fn({}, a[i]); | ||
} else { | ||
result[i] = a[i]; | ||
} | ||
} | ||
} | ||
for (let i = 0; i < b.length; i += 1) { | ||
if (typeof b[i] !== 'undefined') { | ||
if (b[i] !== null && typeof b[i] === 'object') { | ||
result[i] = fn(a[i], b[i]); | ||
} else { | ||
result[i] = b[i]; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* Merge deep objects | ||
* @param {*} args | ||
* @return {*} | ||
@@ -41,11 +74,3 @@ */ | ||
if (a instanceof Array && b instanceof Array) { | ||
for (let index = 0; index < b.length; index += 1) { | ||
if (typeof b[index] !== 'undefined') { | ||
if (b[index] !== null && typeof b[index] === 'object') { | ||
a[index] = deepExtend(a[index], b[index]); | ||
} else { | ||
a[index] = b[index]; | ||
} | ||
} | ||
} | ||
a = mergeArrays(a, b, deepExtend); | ||
} else { | ||
@@ -66,3 +91,7 @@ const keys = Object.keys(b); | ||
} else if (b !== null && typeof b !== 'undefined') { | ||
a = b; | ||
if (b instanceof Array) { | ||
a = mergeArrays([], b, deepExtend); | ||
} else { | ||
a = b; | ||
} | ||
} | ||
@@ -69,0 +98,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11254
15
190
98
0