Skip to content

Commit

Permalink
Merge pull request #5 from Geta/develop
Browse files Browse the repository at this point in the history
Fix issue with concatting arrays inside objects.
  • Loading branch information
eirhor authored Dec 21, 2017
2 parents d0b20a8 + 3e144cf commit db154e0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 14 deletions.
64 changes: 63 additions & 1 deletion lib/nestedObjectAssign.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "nested-object-assign",
"version": "1.0.1",
"version": "1.0.2",
"description": "Package to support nested merging of objects & properties, using Object.Assign",
"main": "./index.js",
"scripts": {
"start": "npm run-script build-dev",
"prepublish": "npm run build-all",
"preversion": "npm run build-all && npm run unit",
"version": "git add .",
Expand Down
3 changes: 3 additions & 0 deletions src/isArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isArray(item){
return (item && Array.isArray(item));
}
15 changes: 11 additions & 4 deletions src/nestedObjectAssign.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isObject} from './isObject';
import { isObject } from './isObject';
import { isArray } from './isArray';

export default function nestedObjectAssign(target, ...sources){
if (!sources.length)
Expand All @@ -9,12 +10,18 @@ export default function nestedObjectAssign(target, ...sources){
if (isObject(target) && isObject(source)){
for (const key in source){
if (isObject(source[key])){
if (!target[key])
if (!target[key]) {
Object.assign(target, {[key]: {}});
}

nestedObjectAssign(target[key], source[key]);
}
else {
} else if (isArray(source[key])) {
if (!target[key]) {
Object.assign(target, {[key]: []});
}

target[key] = target[key].concat(source[key]);
} else {
Object.assign(target, {[key]: source[key]});
}
}
Expand Down
45 changes: 37 additions & 8 deletions test/nestedObjectAssign.spec.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@
import { expect } from 'chai';
import nestedObjectAssign from '../index.js';

var mockData = {
const mockData = {
default: {
heading: 'title',
body: {
paragraph: 'p',
heading: 'h1'
}
},
products: [
{
paragraph: 'p',
heading: 'h1'
}
]
},
first: {
body: {
span: 'span',
header: 'header'
}
},
products: [
{
span: 'span',
header: 'header'
}
]
},
second: {
body: {
heading2: 'h2'
}
},
products: [
{
heading2: 'h2'
}
]
}

};

var expectedData = {
const expectedData = {
heading: 'title',
body: {
paragraph: 'p',
heading: 'h1',
span: 'span',
header: 'header',
heading2: 'h2'
}
},
products: [
{
paragraph: 'p',
heading: 'h1'
},
{
span: 'span',
header: 'header'
},
{
heading2: 'h2'
}
]
};

describe('Given an instance of nestedObjectAssign', function() {
describe('when i merge the mockData', function() {
describe('when I merge the mockData', function() {
it('it should be equal to expectedData', () => {
expect(JSON.stringify(nestedObjectAssign({}, mockData.default, mockData.first, mockData.second))).to.be.equal(JSON.stringify(expectedData));
});
Expand Down

0 comments on commit db154e0

Please sign in to comment.