Comparing version 2.3.0 to 2.4.0
@@ -55,2 +55,4 @@ import { Pointer } from './pointer'; | ||
export declare function isDestructive({ op }: Operation): boolean; | ||
export declare type Diff = (input: any, output: any, ptr: Pointer) => Operation[]; | ||
export declare type VoidableDiff = (input: any, output: any, ptr: Pointer) => Operation[] | void; | ||
/** | ||
@@ -89,5 +91,5 @@ subtract(a, b) returns the keys in `a` that are not in `b`. | ||
*/ | ||
export declare function diffArrays<T>(input: T[], output: T[], ptr: Pointer): Operation[]; | ||
export declare function diffObjects(input: any, output: any, ptr: Pointer): Operation[]; | ||
export declare function diffArrays<T>(input: T[], output: T[], ptr: Pointer, diff?: Diff): Operation[]; | ||
export declare function diffObjects(input: any, output: any, ptr: Pointer, diff?: Diff): Operation[]; | ||
export declare function diffValues(input: any, output: any, ptr: Pointer): Operation[]; | ||
export declare function diffAny(input: any, output: any, ptr: Pointer): Operation[]; | ||
export declare function diffAny(input: any, output: any, ptr: Pointer, diff?: Diff): Operation[]; |
17
diff.js
@@ -90,3 +90,4 @@ "use strict"; | ||
*/ | ||
function diffArrays(input, output, ptr) { | ||
function diffArrays(input, output, ptr, diff) { | ||
if (diff === void 0) { diff = diffAny; } | ||
// set up cost matrix (very simple initialization: just a map) | ||
@@ -197,3 +198,3 @@ var memo = { | ||
var replace_ptr = ptr.add(String(array_operation.index + padding)); | ||
var replace_operations = diffAny(array_operation.original, array_operation.value, replace_ptr); | ||
var replace_operations = diff(array_operation.original, array_operation.value, replace_ptr); | ||
return [operations.concat.apply(operations, replace_operations), padding]; | ||
@@ -205,3 +206,4 @@ } | ||
exports.diffArrays = diffArrays; | ||
function diffObjects(input, output, ptr) { | ||
function diffObjects(input, output, ptr, diff) { | ||
if (diff === void 0) { diff = diffAny; } | ||
// if a key is in input but not output -> remove it | ||
@@ -218,3 +220,3 @@ var operations = []; | ||
intersection([input, output]).forEach(function (key) { | ||
operations.push.apply(operations, diffAny(input[key], output[key], ptr.add(key))); | ||
operations.push.apply(operations, diff(input[key], output[key], ptr.add(key))); | ||
}); | ||
@@ -231,10 +233,11 @@ return operations; | ||
exports.diffValues = diffValues; | ||
function diffAny(input, output, ptr) { | ||
function diffAny(input, output, ptr, diff) { | ||
if (diff === void 0) { diff = diffAny; } | ||
var input_type = objectType(input); | ||
var output_type = objectType(output); | ||
if (input_type == 'array' && output_type == 'array') { | ||
return diffArrays(input, output, ptr); | ||
return diffArrays(input, output, ptr, diff); | ||
} | ||
if (input_type == 'object' && output_type == 'object') { | ||
return diffObjects(input, output, ptr); | ||
return diffObjects(input, output, ptr, diff); | ||
} | ||
@@ -241,0 +244,0 @@ // only pairs of arrays and objects can go down a path to produce a smaller |
@@ -1,2 +0,2 @@ | ||
import { Operation, TestOperation } from './diff'; | ||
import { Operation, TestOperation, VoidableDiff } from './diff'; | ||
export { Operation, TestOperation }; | ||
@@ -29,5 +29,9 @@ export declare type Patch = Operation[]; | ||
`diff` is called on each pair of comparable non-primitive nodes in the | ||
`input`/`output` object trees, producing nested patches. Return `undefined` | ||
to fall back to default behaviour. | ||
Returns list of operations to perform on `input` to produce `output`. | ||
*/ | ||
export declare function createPatch(input: any, output: any): Operation[]; | ||
export declare function createPatch(input: any, output: any, diff?: VoidableDiff): Operation[]; | ||
/** | ||
@@ -34,0 +38,0 @@ Produce an 'application/json-patch+json'-type list of tests, to verify that |
16
index.js
@@ -35,2 +35,10 @@ "use strict"; | ||
exports.applyPatch = applyPatch; | ||
function wrapVoidableDiff(diff) { | ||
function wrappedDiff(input, output, ptr) { | ||
var custom_patch = diff(input, output, ptr); | ||
// ensure an array is always returned | ||
return Array.isArray(custom_patch) ? custom_patch : diff_1.diffAny(input, output, ptr, wrappedDiff); | ||
} | ||
return wrappedDiff; | ||
} | ||
/** | ||
@@ -43,8 +51,12 @@ Produce a 'application/json-patch+json'-type patch to get from one object to | ||
`diff` is called on each pair of comparable non-primitive nodes in the | ||
`input`/`output` object trees, producing nested patches. Return `undefined` | ||
to fall back to default behaviour. | ||
Returns list of operations to perform on `input` to produce `output`. | ||
*/ | ||
function createPatch(input, output) { | ||
function createPatch(input, output, diff) { | ||
var ptr = new pointer_1.Pointer(); | ||
// a new Pointer gets a default path of [''] if not specified | ||
return diff_1.diffAny(input, output, ptr); | ||
return (diff ? wrapVoidableDiff(diff) : diff_1.diffAny)(input, output, ptr); | ||
} | ||
@@ -51,0 +63,0 @@ exports.createPatch = createPatch; |
{ | ||
"name": "rfc6902", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Complete implementation of RFC6902 (patch and diff)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47751
956