rse/grunt-merge-json — pull request #2 by Iszak (2013-12-08 22:04:03+00:00)
Title: Recursive merge

Hello,

I have noticed a case where it does not merge but instead overwrites, I bring this down to how [extend](http://lodash.com/docs#assign) works, according to the documentation extend will overwrite the keys if they already exist, e.g.

var a = {
"bar": {
"title": "The title"
}
}

var b = {
"bar": {
"description": "The description"
}
}

var c = grunt.util._.extend(a, b);

In this case the key "bar" already exist so the second instance overwrites the first resulting in the following;

{
"bar": {
"description": "The description"
}
}

However if we use [merge](http://lodash.com/docs#merge), this will merge the two properties into the same object;

{
"bar": {
"title": "The title",
"description": "The description"
}
}

However, there is a edge case to this, that is if you have two arrays and they both occupy the same index e.g. 0

var a = {
"bar": [
{
"title": "The title 1"
}
]
};

var b = {
"bar": [
{
"title": "The title 2"
}
]
};

var c = grunt.util._.merge(a, b);

I would expect it to result in the following;

{
"bar": [
{
"title": "The title 1"
},
{
"title": "The title 2"
}
]
}

However it doesn't, instead the second object overwrites the first. Therefore we get the resulting change I propose which checks if it's an array and if it is, concats both arrays and if not, returns undefined to allow the merge function to handle it. As for the inclusion of lodash instead of using grunt's inbuilt reference is because it's an older version [~0.9.2](https://github.com/gruntjs/grunt/blob/master/package.json#L59) which doesn't support the callback.

Obviously this will be a breaking change and I understand you may not want to introduce it as it may go against the usage of grunt-merge-json. A solution to this would be to introduce a option which is sets whether it's a) recursive and b) concats arrays. Let me know what you think, thanks for the tool!