View Source Delta (Delta v0.3.0)
Link to this section Summary
Functions
Compacts Delta to satisfy compactness requirement.
Returns a new Delta that is equivalent to applying the operations of one Delta, followed by another Delta
Returns a new Delta that is equivalent to applying Deltas one by one
Concatenates two Deltas.
Returns an inverted delta that has the opposite effect of against a base document delta.
Pushes an operation to a reversed Delta honouring semantics.
Returns the size of delta.
Attempts to take len
characters starting from index
.
Takes len
or fewer characters from index
position. Variable len
allows
to not cut things like emojis in half.
Splits delta at the given index.
Transforms given delta against another's operations.
Link to this section Types
@type t() :: [Delta.Op.t()]
Link to this section Functions
Compacts Delta to satisfy compactness requirement.
examples
Examples
iex> delta = [Op.insert("Hel"), Op.insert("lo"), Op.insert("World", %{"bold" => true})]
iex> Delta.compact(delta)
[%{"insert" => "Hello"}, %{"insert" => "World", "attributes" => %{"bold" => true}}]
Returns a new Delta that is equivalent to applying the operations of one Delta, followed by another Delta
examples
Examples
iex> a = [Op.insert("abc")]
iex> b = [Op.retain(1), Op.delete(1)]
iex> Delta.compose(a, b)
[%{"insert" => "ac"}]
Returns a new Delta that is equivalent to applying Deltas one by one
examples
Examples
iex> a = [Op.insert("ac")]
iex> b = [Op.retain(1), Op.insert("b")]
iex> c = [Op.delete(1)]
iex> Delta.compose_all([a, b, c])
[%{"insert" => "bc"}]
Concatenates two Deltas.
examples
Examples
iex> a = [Op.insert("Hel")]
iex> b = [Op.insert("lo")]
iex> Delta.concat(a, b)
[%{"insert" => "Hello"}]
Returns an inverted delta that has the opposite effect of against a base document delta.
That is base |> Delta.compose(change) |> Delta.compose(inverted) == base.
examples
Examples
iex> base = [Op.insert("Hello\nWorld")]
iex> change = [
...> Op.retain(6, %{"bold" => true}),
...> Op.delete(5),
...> Op.insert("!"),
...> ]
iex> inverted = Delta.invert(change, base)
[
%{"retain" => 6, "attributes" => %{"bold" => nil}},
%{"insert" => "World"},
%{"delete" => 1},
]
iex> base |> Delta.compose(change) |> Delta.compose(inverted) == base
true
@spec push(t(), false) :: t()
@spec push(t(), Delta.Op.t()) :: t()
Pushes an operation to a reversed Delta honouring semantics.
Note: that reversed delta does not represent a reversed text, but rather a list of operations that was naively reversed during programmatic manipulations. This function is normally only used by other functions which reverse the list back in the end.
examples
Examples
iex> delta = [Op.insert("World", %{"italic" => true}), Op.insert("Hello", %{"bold" => true})]
iex> op = Op.insert("!")
iex> Delta.push(delta, op)
[%{"insert" => "!"}, %{"insert" => "World", "attributes" => %{"italic" => true}}, %{"insert" => "Hello", "attributes" => %{"bold" => true}}]
iex> delta = [Op.insert("World"), Op.insert("Hello", %{"bold" => true})]
iex> op = Op.insert("!")
iex> Delta.push(delta, op)
[%{"insert" => "World!"}, %{"insert" => "Hello", "attributes" => %{"bold" => true}}]
@spec size(t()) :: non_neg_integer()
Returns the size of delta.
examples
Examples
iex> delta = [Op.insert("abc"), Op.retain(2), Op.delete(1)]
iex> Delta.size(delta)
6
@spec slice(t(), non_neg_integer(), non_neg_integer()) :: t()
Attempts to take len
characters starting from index
.
Note: note that due to the way it's implemented this operation can potentially raise if the resulting text isn't a valid UTF-8 encoded string
examples
Examples
iex> delta = [Op.insert("Hello World")]
iex> Delta.slice(delta, 6, 3)
[%{"insert" => "Wor"}]
iex> delta = [Op.insert("01🙋45")]
iex> Delta.slice(delta, 1, 2)
** (RuntimeError) Encoding failed in take_partial {"1🙋45", %{"insert" => "1🙋45"}, 2, {:incomplete, "1", <<216, 61>>}, {:error, "", <<222, 75, 0, 52, 0, 53>>}}
@spec slice_max(t(), non_neg_integer(), non_neg_integer()) :: t()
Takes len
or fewer characters from index
position. Variable len
allows
to not cut things like emojis in half.
examples
Examples
iex> delta = [Op.insert("Hello World")]
iex> Delta.slice_max(delta, 6, 3)
[%{"insert" => "Wor"}]
iex> delta = [Op.insert("01🙋45")]
iex> Delta.slice_max(delta, 1, 2)
[%{"insert" => "1"}]
Splits delta at the given index.
options
Options
:align
- whentrue
, allow moving index left if we're likely to split a grapheme otherwise.
examples
Examples
iex> delta = [Op.insert("Hello World")]
iex> Delta.split(delta, 5)
{[%{"insert" => "Hello"}], [%{"insert" => " World"}]}
iex> delta = [Op.insert("01🙋45")]
iex> Delta.split(delta, 3, align: true)
{[%{"insert" => "01"}], [%{"insert" => "🙋45"}]}
Transforms given delta against another's operations.
This accepts an optional priority argument (default: false), used to break ties. If true, the first delta takes priority over other, that is, its actions are considered to happen "first."
examples
Examples
iex> a = [Op.insert("a")]
iex> b = [Op.insert("b"), Op.retain(5), Op.insert("c")]
iex> Delta.transform(a, b, true)
[
%{"retain" => 1},
%{"insert" => "b"},
%{"retain" => 5},
%{"insert" => "c"},
]
iex> Delta.transform(a, b)
[
%{"insert" => "b"},
%{"retain" => 6},
%{"insert" => "c"},
]