Skip to main content

core.diffarrays

Home > @medplum/core > diffArrays

diffArrays() function

Calculate the shortest sequence of operations to get from input to output, using a dynamic programming implementation of the Levenshtein distance algorithm.

To get from the input ABC to the output AZ we could just delete all the input and say "insert A, insert Z" and be done with it. That's what we do if the input is empty. But we can be smarter.

output A Z - - [0] 1 2 input A | 1 [0] 1 B | 2 [1] 1 C | 3 2 [2]

  1. start at 0,0 (+0) 2) keep A (+0) 3) remove B (+1) 4) replace C with Z (+1)

If the input (source) is empty, they'll all be in the top row, resulting in an array of 'add' operations. If the output (target) is empty, everything will be in the left column, resulting in an array of 'remove' operations.

Signature:

export declare function diffArrays<T>(input: T[], output: T[], ptr: Pointer, diff?: Diff): Operation[];

Parameters

Parameter

Type

Description

input

T[]

The original array.

output

T[]

The target array.

ptr

Pointer

JSON Pointer.

diff

Diff

(Optional) Diff function.

Returns:

Operation

A list of add/remove/replace operations.