-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c72f29
commit c7be77c
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Packages | ||
import { getDiff } from '.' | ||
|
||
// Models | ||
import { JsonPatch } from '../models/jsondiffer.model' | ||
import { generateJsonPatch } from './generate-json-patch' | ||
|
||
describe('GenerateJsonPatch function', () => { | ||
test('Should return the difference between two basic structures', () => { | ||
const struct1 = { '0': [{ '0': 1 }] } | ||
const struct2 = { '0': { '0': [1] } } | ||
const expectedResult: Array<JsonPatch> = [ | ||
{ op: 'remove', path: '0/0' }, | ||
{ op: 'remove', path: '0/0/0[]' }, | ||
{ op: 'replace', path: '0', value: [] }, | ||
{ op: 'add', path: '0/0[]', value: {} }, | ||
{ op: 'add', path: '0/0[]/0', value: 1 } | ||
] | ||
const delta = getDiff(struct1, struct2) | ||
const result = generateJsonPatch(delta) | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
|
||
test('Should return the difference between two basic structures', () => { | ||
const struct1 = { | ||
baz: 'qux', | ||
foo: 'bar' | ||
} | ||
const struct2 = { | ||
baz: 'boo', | ||
hello: ['world'] | ||
} | ||
const expectedResult: Array<JsonPatch> = [ | ||
{ | ||
op: 'remove', | ||
path: 'hello' | ||
}, | ||
{ | ||
op: 'remove', | ||
path: 'hello/0[]' | ||
}, | ||
{ | ||
op: 'replace', | ||
path: 'baz', | ||
value: 'qux' | ||
}, | ||
{ | ||
op: 'add', | ||
path: 'foo', | ||
value: 'bar' | ||
} | ||
] | ||
const delta = getDiff(struct1, struct2) | ||
const result = generateJsonPatch(delta) | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Models | ||
import { Delta, JsonPatch } from '../models/jsondiffer.model' | ||
|
||
export const generateJsonPatch = (delta: Delta): Array<JsonPatch> => { | ||
const operations: Array<JsonPatch> = [] | ||
|
||
delta.added.forEach((path) => { | ||
operations.push({ | ||
op: 'remove', | ||
path: path[0] | ||
}) | ||
}) | ||
|
||
delta.edited.forEach((path) => { | ||
operations.push({ | ||
op: 'replace', | ||
path: path[0], | ||
value: path[1] | ||
}) | ||
}) | ||
|
||
delta.removed.forEach((path) => { | ||
operations.push({ | ||
op: 'add', | ||
path: path[0], | ||
value: path[1] | ||
}) | ||
}) | ||
|
||
return operations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters