Skip to content
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

feat: add json patch generator RFC 6902 support #43

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
59 changes: 59 additions & 0 deletions libs/json-difference/src/core/generate-json-patch.spec.ts
Original file line number Diff line number Diff line change
@@ -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<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 generate JSON Patch operations from delta', () => {
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)
})
})
31 changes: 31 additions & 0 deletions libs/json-difference/src/core/generate-json-patch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Models
import { Delta, JsonPatch } from '../models/json-difference.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
}
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-edited-paths.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-edited-paths.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-paths-diff.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-paths-diff.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/core/get-struct-paths.ts
Original file line number Diff line number Diff line change
@@ -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 ? '[' : '.') : '/'
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/helpers/sanitize-delta.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @{}', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/json-difference/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './jsondiffer.model'
export * from './json-difference.model'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading