Quillon.Transform.MarkOrder (Quillon v0.1.0)

Copy Markdown View Source

Mark ordering for reliable comparison.

Provides consistent sorting of marks so that equality checks work regardless of the order marks were applied.

Summary

Types

A mark - either a simple atom or attributed tuple

Functions

Compare two marks lists for equality using sorted comparison.

Sort marks into a consistent order for comparison.

Types

mark()

@type mark() :: atom() | {atom(), map()}

A mark - either a simple atom or attributed tuple

Functions

marks_equal?(marks1, marks2)

@spec marks_equal?([mark()] | nil, [mark()] | nil) :: boolean()

Compare two marks lists for equality using sorted comparison.

More reliable than set-based comparison as it handles attributed marks with the same type but different attrs correctly.

Examples

iex> Quillon.Transform.MarkOrder.marks_equal?([:bold, :italic], [:italic, :bold])
true

iex> Quillon.Transform.MarkOrder.marks_equal?([:bold], [:italic])
false

iex> Quillon.Transform.MarkOrder.marks_equal?([], [])
true

iex> Quillon.Transform.MarkOrder.marks_equal?(nil, [])
true

iex> Quillon.Transform.MarkOrder.marks_equal?(
...>   [{:link, %{href: "/"}}],
...>   [{:link, %{href: "/"}}]
...> )
true

iex> Quillon.Transform.MarkOrder.marks_equal?(
...>   [{:link, %{href: "/"}}],
...>   [{:link, %{href: "/other"}}]
...> )
false

sort_marks(marks)

@spec sort_marks([mark()]) :: [mark()]

Sort marks into a consistent order for comparison.

Simple marks are sorted by priority, then attributed marks. Unknown marks sort last, alphabetically by name.

Examples

iex> Quillon.Transform.MarkOrder.sort_marks([:italic, :bold])
[:bold, :italic]

iex> Quillon.Transform.MarkOrder.sort_marks([:italic, {:link, %{href: "/"}}, :bold])
[:bold, :italic, {:link, %{href: "/"}}]

iex> Quillon.Transform.MarkOrder.sort_marks([])
[]