Skip to content

Commit

Permalink
Add original field to remove operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuan Yao committed Apr 14, 2021
1 parent 04bf11e commit fe43e6f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
8 changes: 5 additions & 3 deletions diff.ts
Expand Up @@ -25,7 +25,7 @@ differentiates between.
*/

export interface AddOperation { op: 'add', path: string, value: any }
export interface RemoveOperation { op: 'remove', path: string }
export interface RemoveOperation { op: 'remove', path: string, original?: any }
export interface ReplaceOperation { op: 'replace', path: string, value: any, original?: any }
export interface MoveOperation { op: 'move', from: string, path: string }
export interface CopyOperation { op: 'copy', from: string, path: string }
Expand Down Expand Up @@ -113,7 +113,7 @@ export function intersection(objects: ArrayLike<{[index: string]: any}>): string
}

interface ArrayAdd { op: 'add', index: number, value: any }
interface ArrayRemove { op: 'remove', index: number }
interface ArrayRemove { op: 'remove', index: number, original?: any }
interface ArrayReplace { op: 'replace', index: number, original: any, value: any }
/** These are not proper Operation objects, but will be converted into
Operation objects eventually. {index} indicates the actual target position,
Expand Down Expand Up @@ -203,6 +203,7 @@ export function diffArrays<T>(input: T[], output: T[], ptr: Pointer, diff: Diff
const remove_operation: ArrayRemove = {
op: 'remove',
index: i - 1,
original: input[i - 1],
}
alternatives.push(appendArrayOperation(remove_base, remove_operation))
}
Expand Down Expand Up @@ -264,6 +265,7 @@ export function diffArrays<T>(input: T[], output: T[], ptr: Pointer, diff: Diff
const operation = {
op: array_operation.op,
path: ptr.add(String(array_operation.index + padding)).toString(),
original: array_operation.original
}
// padding--
return [operations.concat(operation), padding - 1]
Expand All @@ -281,7 +283,7 @@ export function diffObjects(input: any, output: any, ptr: Pointer, diff: Diff =
// if a key is in input but not output -> remove it
const operations: Operation[] = []
subtract(input, output).forEach(key => {
operations.push({op: 'remove', path: ptr.add(key).toString()})
operations.push({op: 'remove', path: ptr.add(key).toString(), original: input[key]})
})
// if a key is in output but not input -> add it
subtract(output, input).forEach(key => {
Expand Down
4 changes: 3 additions & 1 deletion dist/rfc6902.js
Expand Up @@ -299,6 +299,7 @@
const remove_operation = {
op: 'remove',
index: i - 1,
original: input[i - 1],
};
alternatives.push(appendArrayOperation(remove_base, remove_operation));
}
Expand Down Expand Up @@ -359,6 +360,7 @@
const operation = {
op: array_operation.op,
path: ptr.add(String(array_operation.index + padding)).toString(),
original: array_operation.original
};
// padding--
return [operations.concat(operation), padding - 1];
Expand All @@ -375,7 +377,7 @@
// if a key is in input but not output -> remove it
const operations = [];
subtract(input, output).forEach(key => {
operations.push({ op: 'remove', path: ptr.add(key).toString() });
operations.push({ op: 'remove', path: ptr.add(key).toString(), original: input[key] });
});
// if a key is in output but not input -> add it
subtract(output, input).forEach(key => {
Expand Down
18 changes: 9 additions & 9 deletions dist/rfc6902.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions test/issues.ts
Expand Up @@ -25,8 +25,8 @@ test('issues/3', t => {
const input = {arr: ['1', '2', '2']}
const output = {arr: ['1']}
const expected_patch: Operation[] = [
{op: 'remove', path: '/arr/1'},
{op: 'remove', path: '/arr/1'},
{op: 'remove', path: '/arr/1', original: '2'},
{op: 'remove', path: '/arr/1', original: '2'},
]
checkRoundtrip(t, input, output, expected_patch)
})
Expand All @@ -36,7 +36,7 @@ test('issues/4', t => {
const output = ['B', 'A']
const expected_patch: Operation[] = [
{op: 'add', path: '/0', value: 'B'},
{op: 'remove', path: '/2'},
{op: 'remove', path: '/2', original: 'B'},
]
checkRoundtrip(t, input, output, expected_patch)
})
Expand Down Expand Up @@ -121,7 +121,7 @@ test('issues/29', t => {
stop_recursing: ['a'],
}
const expected_patch: Operation[] = [
{op: 'remove', path: '/normal/1'},
{op: 'remove', path: '/normal/1', original: 'b'},
{op: 'replace', path: '/stop_recursing', value: ['a']},
]
const actual_patch = createPatch(input, output, customDiff)
Expand All @@ -130,7 +130,7 @@ test('issues/29', t => {
const nested_input = {root: input}
const nested_output = {root: output}
const nested_expected_patch: Operation[] = [
{op: 'remove', path: '/root/normal/1'},
{op: 'remove', path: '/root/normal/1', 'original': 'b'},
{op: 'replace', path: '/root/stop_recursing', value: ['a']},
]
const nested_actual_patch = createPatch(nested_input, nested_output, customDiff)
Expand Down
4 changes: 2 additions & 2 deletions test/spec.yaml
Expand Up @@ -23,7 +23,7 @@
input:
{"baz": "qux", "foo": "bar"}
patch:
- {op: "remove", path: "/baz"}
- {op: "remove", path: "/baz", original: "qux"}
output:
{"foo": "bar"}
results: [null]
Expand All @@ -33,7 +33,7 @@
input:
{"foo": [ "bar", "qux", "baz" ]}
patch:
- {op: "remove", path: "/foo/1"}
- {op: "remove", path: "/foo/1", original: "qux"}
output:
{"foo": [ "bar", "baz" ]}
results: [null]
Expand Down

0 comments on commit fe43e6f

Please sign in to comment.