miniBill/elm-diff/1.1.1

NOTE: the latest version is 1.1.2

Diff

Compares two list and returns how they have changed. Each function internally uses Wu's O(NP) algorithm.

Types

type Change similar a
= Added a
| Removed a
| Similar a a similar
| NoChange a

This describes how each line has changed and also contains its value.

Diffing

diff : List a -> List a -> List (Change Never a)

Compares general lists.

diff [ 1, 3 ] [ 2, 3 ]
--> [ Removed 1, Added 2, NoChange 3 ]

This will never produce Similar, so you can use never in that branch.

diff a b
    |> List.map
        (\change ->
            case change of
                Diff.Added _ ->
                    1


                Diff.Removed _ ->
                    -1


                Diff.NoChange _ ->
                    0


                Diff.Similar _ _ ever ->
                    never ever
        )
diffLines : String -> String -> List (Change () String)

Compares two text. Considers two lines similar if they are equivalent ignoring spaces.

Giving the following text

a =
    """aaa
bbb
ddd"""


b =
    """zzz
aaa
ccc
ddd"""

results in

[ Added "zzz"
, NoChange "aaa"
, Removed "bbb"
, Added "ccc"
, NoChange "ddd"
]

.

diffWith : (a -> a -> Maybe similar) -> List a -> List a -> List (Change similar a)

Compares general lists. Allows specifying when two elements are similar.

diff (\l r -> if abs (l - r) < 1 then Just () else Nothing) [ 1, 3 ] [ 2, 3 ]
--> [ Similar 1 2 (), NoChange 3 ]
-> String
-> String
-> List (Change (List (Change Never Char)) String)

Calculate the diff between two multiline strings.

Options for diffing multiline strings.

Default comparison options.

Ignore leading whitespace when comparing multiline strings.

diffLinesWith (defaultOptions |> ignoreLeadingWhitespace) "a" "  a"
--> [ NoChange "a" ]

Equivalent to equalIf (\a b -> String.trimLeft a == String.trimLeft b).

equalIf : (String -> String -> Bool) -> DiffOptions -> DiffOptions

Configure when to consider two lines equal.

As an example you can use \l r -> String.trim l == String.trim r to ignore whitespace around lines.

similarIf : (String -> String -> Bool) -> DiffOptions -> DiffOptions

Configure when to consider two lines similar.