diff --git a/libs/json-difference/src/core/generate-json-patch.spec.ts b/libs/json-difference/src/core/generate-json-patch.spec.ts new file mode 100644 index 0000000..fc5cd61 --- /dev/null +++ b/libs/json-difference/src/core/generate-json-patch.spec.ts @@ -0,0 +1,59 @@ +// Packages +import { getDiff } from '.' + +// Models +import { JsonPatch } from '../models/json-difference.model' +import { generateJsonPatch } from './generate-json-patch' + +describe('GenerateJsonPatch function', () => { + test('Should generate JSON Patch operations from delta', () => { + const struct1 = { '0': [{ '0': 1 }] } + const struct2 = { '0': { '0': [1] } } + const expectedResult: Array = [ + { 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 generate JSON Patch operations from delta', () => { + const struct1 = { + baz: 'qux', + foo: 'bar' + } + const struct2 = { + baz: 'boo', + hello: ['world'] + } + const expectedResult: Array = [ + { + 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) + }) +}) diff --git a/libs/json-difference/src/core/generate-json-patch.ts b/libs/json-difference/src/core/generate-json-patch.ts new file mode 100644 index 0000000..3412aa8 --- /dev/null +++ b/libs/json-difference/src/core/generate-json-patch.ts @@ -0,0 +1,31 @@ +// Models +import { Delta, JsonPatch } from '../models/json-difference.model' + +export const generateJsonPatch = (delta: Delta): Array => { + const operations: Array = [] + + 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 +} diff --git a/libs/json-difference/src/core/get-diff.spec.ts b/libs/json-difference/src/core/get-diff.spec.ts index 6d542e3..272bcfc 100644 --- a/libs/json-difference/src/core/get-diff.spec.ts +++ b/libs/json-difference/src/core/get-diff.spec.ts @@ -2,7 +2,7 @@ import { getDiff } from '.' // Models -import { Delta } from '../models/jsondiffer.model' +import { Delta } from '../models/json-difference.model' describe('GetDiff function', () => { test('Should return the difference between two basic structures', () => { diff --git a/libs/json-difference/src/core/get-diff.ts b/libs/json-difference/src/core/get-diff.ts index 9b223d7..8c7a542 100644 --- a/libs/json-difference/src/core/get-diff.ts +++ b/libs/json-difference/src/core/get-diff.ts @@ -4,8 +4,8 @@ import { getPathsDiff } from './get-paths-diff' import { getStructPaths } from './get-struct-paths' // Models -import { Delta, JsonDiffOptions } from '../models/jsondiffer.model' import sanitizeDelta from '../helpers/sanitize-delta' +import { Delta, JsonDiffOptions } from '../models/json-difference.model' const defaultOptions: JsonDiffOptions = { isLodashLike: false diff --git a/libs/json-difference/src/core/get-edited-paths.spec.ts b/libs/json-difference/src/core/get-edited-paths.spec.ts index 33b8473..1c6556a 100644 --- a/libs/json-difference/src/core/get-edited-paths.spec.ts +++ b/libs/json-difference/src/core/get-edited-paths.spec.ts @@ -1,5 +1,5 @@ import { getEditedPaths } from '.' -import { EditedPath } from '../models/jsondiffer.model' +import { EditedPath } from '../models/json-difference.model' describe('GetEditedPaths function', () => { test('Should return empty when there is no edited value', () => { diff --git a/libs/json-difference/src/core/get-edited-paths.ts b/libs/json-difference/src/core/get-edited-paths.ts index 31d41dc..e9f3d33 100644 --- a/libs/json-difference/src/core/get-edited-paths.ts +++ b/libs/json-difference/src/core/get-edited-paths.ts @@ -1,5 +1,5 @@ // Models -import { EditedPath, StructPaths } from '../models/jsondiffer.model' +import { EditedPath, StructPaths } from '../models/json-difference.model' /** * This method returns all paths whose leaf value has changed diff --git a/libs/json-difference/src/core/get-paths-diff.spec.ts b/libs/json-difference/src/core/get-paths-diff.spec.ts index 821d8d9..63a7a12 100644 --- a/libs/json-difference/src/core/get-paths-diff.spec.ts +++ b/libs/json-difference/src/core/get-paths-diff.spec.ts @@ -1,6 +1,6 @@ // Packages import { getPathsDiff } from '.' -import { PathsDiff } from '../models/jsondiffer.model' +import { PathsDiff } from '../models/json-difference.model' describe('GetPathsDiff function', () => { test('Should return empty when there is no key difference', () => { diff --git a/libs/json-difference/src/core/get-paths-diff.ts b/libs/json-difference/src/core/get-paths-diff.ts index e75b95e..fb7482c 100644 --- a/libs/json-difference/src/core/get-paths-diff.ts +++ b/libs/json-difference/src/core/get-paths-diff.ts @@ -1,5 +1,5 @@ // Models -import { PathsDiff, StructPaths } from '../models/jsondiffer.model' +import { PathsDiff, StructPaths } from '../models/json-difference.model' /** * This method returns all paths whose leaf value has changed diff --git a/libs/json-difference/src/core/get-struct-paths.ts b/libs/json-difference/src/core/get-struct-paths.ts index d64786d..a896348 100644 --- a/libs/json-difference/src/core/get-struct-paths.ts +++ b/libs/json-difference/src/core/get-struct-paths.ts @@ -1,5 +1,5 @@ // Models -import { StructPaths } from '../models/jsondiffer.model' +import { StructPaths } from '../models/json-difference.model' const generatePath = (isArray: boolean, currentPath: string, newPath: string, lodashLike: boolean): string => { const prefix = lodashLike ? (isArray ? '[' : '.') : '/' diff --git a/libs/json-difference/src/helpers/sanitize-delta.spec.ts b/libs/json-difference/src/helpers/sanitize-delta.spec.ts index 630ddaf..a5cd9da 100644 --- a/libs/json-difference/src/helpers/sanitize-delta.spec.ts +++ b/libs/json-difference/src/helpers/sanitize-delta.spec.ts @@ -2,7 +2,7 @@ import sanitizeDelta from './sanitize-delta' // Models -import { Delta } from '../models/jsondiffer.model' +import { Delta } from '../models/json-difference.model' describe('sanitizeDelta helper', () => { test.only('Should remove unnecessary @{}', () => { diff --git a/libs/json-difference/src/models/index.ts b/libs/json-difference/src/models/index.ts index 62d3b9a..f8f1f66 100644 --- a/libs/json-difference/src/models/index.ts +++ b/libs/json-difference/src/models/index.ts @@ -1 +1 @@ -export * from './jsondiffer.model' +export * from './json-difference.model' diff --git a/libs/json-difference/src/models/jsondiffer.model.ts b/libs/json-difference/src/models/json-difference.model.ts similarity index 50% rename from libs/json-difference/src/models/jsondiffer.model.ts rename to libs/json-difference/src/models/json-difference.model.ts index 659fa7c..5254c98 100644 --- a/libs/json-difference/src/models/jsondiffer.model.ts +++ b/libs/json-difference/src/models/json-difference.model.ts @@ -13,3 +13,22 @@ export interface Delta { export interface JsonDiffOptions { isLodashLike?: boolean } + +export interface JsonPatchRemove { + op: 'remove' + path: string +} + +export interface JsonPatchReplace { + op: 'replace' + path: string + value: any +} + +export interface JsonPatchAdd { + op: 'add' + path: string + value: any +} + +export type JsonPatch = JsonPatchRemove | JsonPatchReplace | JsonPatchAdd