text_delta v1.4.0 TextDelta.Difference View Source

Document diffing.

Given valid document states A and B, generate a delta that when applied to A will result in B.

Link to this section Summary

Types

Reason for an error

Result of getting a diff

Functions

Calculates a difference between two documents in form of new delta

Calculates a difference between two documents in form of new delta

Link to this section Types

Link to this type error_reason() View Source
error_reason() :: :bad_document

Reason for an error.

Link to this type result() View Source
result() :: {:ok, TextDelta.t()} | {:error, error_reason()}

Result of getting a diff.

An ok/error tuple. Represents either a successful diffing in form of {:ok, delta} or an error in form of {:error, reason}.

Link to this section Functions

Calculates a difference between two documents in form of new delta.

Examples

successful application:

iex> doc_a =
iex>  TextDelta.new()
iex>  |> TextDelta.insert("hello")
iex> doc_b =
iex>  TextDelta.new()
iex>  |> TextDelta.insert("goodbye")
iex> TextDelta.diff(doc_a, doc_b)
{:ok, %TextDelta{ops: [
  %{insert: "g"},
  %{delete: 4},
  %{retain: 1},
  %{insert: "odbye"}]}}

error handling:

iex> doc = TextDelta.retain(TextDelta.new(), 3)
iex> TextDelta.diff(doc, doc)
{:error, :bad_document}

Calculates a difference between two documents in form of new delta.

Equivalent to &TextDelta.Difference.diff/2, but instead of returning ok/error tuples raises a RuntimeError.