Skip to content

Generate the identity when running generate(a,a) #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,33 @@ module.exports = function generate(before, after) {
before = serialize(before);
after = serialize(after);

if (!(before instanceof Object) &&
!(after instanceof Object) &&
before === after) { // Return no op when values match
return {}
}

if (before === null || after === null ||
typeof before !== 'object' || typeof after !== 'object' ||
Array.isArray(before) !== Array.isArray(after)) {
return after;
Array.isArray(before) || Array.isArray(after)) {
return serialize(after);
}

if (Array.isArray(before)) {
if (!arrayEquals(before, after)) {
return after;
let patch = {};
for (let key of Object.keys(before)) {
let newVal = generate(before[key] ?? null, after[key] ?? null);
// Omit noops
if (equal(newVal, {})) {
continue;
}
return undefined;
patch[key] = serialize(newVal);
}

var patch = {};
var beforeKeys = Object.keys(before);
var afterKeys = Object.keys(after);

var key, i;

// new elements
var newKeys = {};
for (i = 0; i < afterKeys.length; i++) {
key = afterKeys[i];
if (beforeKeys.indexOf(key) === -1) {
newKeys[key] = true;
for (let key of Object.keys(after)) {
if (!(key in before)) {
patch[key] = serialize(after[key]);
}
}

// removed & modified elements
var removedKeys = {};
for (i = 0; i < beforeKeys.length; i++) {
key = beforeKeys[i];
if (afterKeys.indexOf(key) === -1) {
removedKeys[key] = true;
patch[key] = null;
} else {
if (before[key] !== null && typeof before[key] === 'object') {
var subPatch = generate(before[key], after[key]);
if (subPatch !== undefined) {
patch[key] = subPatch;
}
} else if (before[key] !== after[key]) {
patch[key] = serialize(after[key]);
}
}
}

return (Object.keys(patch).length > 0 ? patch : undefined);
return (Object.keys(patch).length > 0 ? patch : {});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-merge-patch",
"version": "1.0.2",
"version": "2.1.0",
"description": "Implementation of JSON Merge Patch (RFC 7396)",
"main": "index.js",
"directories": {
Expand Down
12 changes: 6 additions & 6 deletions test/lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,24 @@ describe('generate', function() {
);
});

it('should return undefined if the object hasnt changed', function() {
it('should return {} if the object hasnt changed', function() {
assert.deepEqual(
generate({a: 'a'}, {a: 'a'}),
undefined
{}
);
});

it('should return undefined if the object with sub attributes hasnt changed', function() {
it('should return {} if the object with sub attributes hasnt changed', function() {
assert.deepEqual(
generate({a: {b: 'c'}}, {a: {b: 'c'}}),
undefined
{}
);
});

it('should return undefined if the array hasnt changed', function() {
it('should return the target if the output is an array', function() {
assert.deepEqual(
generate([1, 2, 3], [1, 2, 3]),
undefined
[1,2,3]
);
});

Expand Down