Skip to content

Small library for creating diffs and applying them

License

Notifications You must be signed in to change notification settings

DanilaMihailov/differ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Differ Hex Version docs Coverage Status

Small library for creating diffs and applying them

iex> Differ.diff("Hello!", "Hey!")
[eq: "He", del: "llo", ins: "y", eq: "!"]

iex> Differ.patch("Hello!", [eq: "He", del: "llo", ins: "y", eq: "!"])
{:ok, "Hey!"}

Installation

The package can be installed by adding differ to your list of dependencies in mix.exs:

def deps do
  [
    {:differ, "~> 0.1.1"}
  ]
end

Documentation

Documentation can be found at https://hexdocs.pm/differ.

Usage

Simple diff of two maps

iex> user = %{name: "John"}
iex> updated_user = Map.put(user, :age, 25)
iex> Differ.diff(user, updated_user)
[{:name, :eq, "John"}, {:age, :ins, 25}]

For lists and strings using List.myers_difference/3 and String.myers_difference/2 respectevly. So diffs of lists and strings a excactly output of this functions

iex> Differ.diff("Hello!", "Hey!")
[eq: "He", del: "llo", ins: "y", eq: "!"]

It is possible to use Differ with structs, you need to derive default implementation for Differ.Diffable and Differ.Patchable protocols:

defmodule User do
    @derive [Differ.Diffable, Differ.Patchable]
    defstruct name: "", age: 21
end

And now you can call Differ.diff/2 with your structs:

iex> Differ.diff(%User{name: "John"}, %User{name: "John Smith"})
[{:name, :diff, [eq: "John", ins: " Smith"]}, {:age, :eq, 21}]

As well as creating diffs, you can apply diff to a term. There is Differ.patch/2 and Differ.revert/2 for this purposes.

iex> diff = Differ.diff("Hello!", "Hey!")
iex> Differ.patch("Hello!", diff)
{:ok, "Hey!"}

or revert diff

iex> Differ.revert("Hey!", diff)
{:ok, "Hello!"}

For more examples look at the docs.

About

Small library for creating diffs and applying them

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages