Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danilamihailov/differ
Small library for creating diffs and applying them
https://github.com/danilamihailov/differ
diff diffing elixir elixir-library
Last synced: about 1 month ago
JSON representation
Small library for creating diffs and applying them
- Host: GitHub
- URL: https://github.com/danilamihailov/differ
- Owner: DanilaMihailov
- License: apache-2.0
- Created: 2020-01-19T11:39:48.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T04:55:51.000Z (over 4 years ago)
- Last Synced: 2024-10-12T09:42:39.270Z (about 1 month ago)
- Topics: diff, diffing, elixir, elixir-library
- Language: Elixir
- Homepage:
- Size: 165 KB
- Stars: 21
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Differ [![Hex Version](https://img.shields.io/hexpm/v/differ.svg)](https://hex.pm/packages/differ) [![docs](https://img.shields.io/badge/docs-hexpm-blue.svg)](https://hexdocs.pm/differ/) [![Coverage Status](https://coveralls.io/repos/github/DanilaMihailov/differ/badge.svg?branch=master)](https://coveralls.io/github/DanilaMihailov/differ?branch=master)
Small library for creating diffs and applying them
```elixir
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`:```elixir
def deps do
[
{:differ, "~> 0.1.1"}
]
end
```## Documentation
Documentation can be found at [https://hexdocs.pm/differ](https://hexdocs.pm/differ).
## Usage
Simple diff of two maps
```elixir
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
```elixir
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:
```elixir
defmodule User do
@derive [Differ.Diffable, Differ.Patchable]
defstruct name: "", age: 21
end
```
And now you can call `Differ.diff/2` with your structs:
```elixir
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.
```elixir
iex> diff = Differ.diff("Hello!", "Hey!")
iex> Differ.patch("Hello!", diff)
{:ok, "Hey!"}
```
or revert diff
```elixir
iex> Differ.revert("Hey!", diff)
{:ok, "Hello!"}
```For more examples look at the [docs](https://hexdocs.pm/differ/Differ.html).