Giter Site home page Giter Site logo

diff's Introduction

Diff

A simple diff library in Elixir

Diff.diff

Compares 2 terms that have an implementation of the Diff.Diffable protocol and returns a list of changes from the first given binary with the second

The diff function can take the following options:

  • :keep_unchanged - keeps unchanged binary parts in the returned patches
  • :ignore - a regex used to ignore element. Ignored parts are kept in returned in patches

Usage:

iex(1)> Diff.diff("test", "taste")
[%Diff.Modified{index: 1, length: 1, old_element: ["e"], element: ["a"]},
%Diff.Insert{index: 4, length: 1, element: ["e"]}]

Diff.patch

Applies the given patches to the term. Takes an optional third parameter to turn the patched list created from applying the patches back into the type needed.

Usage:

iex(1)> patches = Diff.diff([1, 2, 3, 4], [1, 5, 6, 4, 8])
[%Diff.Modified{element: [5, 6], index: 1, length: 2, old_element: [2, 3]},
 %Diff.Insert{element: '\b', index: 4, length: 1}]

iex(2)> Diff.patch([1, 2, 3, 4], patches)
[1, 5, 6, 4, 8]
iex(1)> patches = Diff.diff("test", "taste")
[%Diff.Modified{index: 1, length: 1, old_element: ["e"], element: ["a"]},
%Diff.Insert{index: 4, length: 1, element: ["e"]}]

iex(2)> Diff.patch("test", patches, &Enum.join/1)
"taste"

Diff.annotated_patch

Diff.annotated_patch takes a a data structure of annotations, and uses them when applying the patch.

Usage:

iex(1)>  annotations =   [
...(1)>     %{delete:   %{before: "<span class='deleted'>",
...(1)>                   after:  "</span>"}},
...(1)>     %{insert:   %{before: "<span class='inserted'>",
...(1)>                   after:  "</span>"}},
...(1)>     %{modified: %{before: "<span class='modified'>",
...(1)>                   after:  "</span>"}}
...(1)> ]
[%{delete: %{after: "</span>", before: "<span class='deleted'>"}},
 %{insert: %{after: "</span>", before: "<span class='inserted'>"}},
 %{modified: %{after: "</span>", before: "<span class='modified'>"}}]
iex(2)> patches = Diff.diff("test", "tast")
[%Diff.Modified{element: ["a"], index: 1, length: 1, old_element: ["e"]}]
iex(3)> Diff.annotated_patch("test", patches, annotations)
iex(3)> Diff.annotated_patch("test", patches, annotations)
["t", "<span class='modified'>", "a", "</span>", "s", "t"]

It takes the same optional join function as Diff.patch as you would expect

Diff.diff, Diff.patch and Diff.annotated_patch all take as a first parameter a term that has an implementation of the Diff.Diffable protocol. By default one exist for BitString and List

diff's People

Contributors

bryanjos avatar type1fool avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

diff's Issues

`Enum.OutOfBoundsError` when comparing against empty string or list on Elixir 1.2.2 and diff 1.0.0

Examples to reproduce the issue:

iex> Diff.diff [1,2,3,4,5], []
** (Enum.OutOfBoundsError) out of bounds error
    (elixir) lib/enum.ex:722: Enum.fetch!/2
             lib/diff.ex:94: anonymous fn/5 in Diff.longest_common_subsequence/4
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:84: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:80: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
iex> Diff.diff [], [1,2,3,4,5]
** (Enum.OutOfBoundsError) out of bounds error
    (elixir) lib/enum.ex:722: Enum.fetch!/2
             lib/diff.ex:94: anonymous fn/5 in Diff.longest_common_subsequence/4
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:80: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:84: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
iex> Diff.diff "", "asdf"
** (Enum.OutOfBoundsError) out of bounds error
    (elixir) lib/enum.ex:722: Enum.fetch!/2
             lib/diff.ex:94: anonymous fn/5 in Diff.longest_common_subsequence/4
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:80: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:84: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
iex> Diff.diff "asdf", ""
** (Enum.OutOfBoundsError) out of bounds error
    (elixir) lib/enum.ex:722: Enum.fetch!/2
             lib/diff.ex:94: anonymous fn/5 in Diff.longest_common_subsequence/4
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:84: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3
    (elixir) lib/enum.ex:1478: anonymous fn/3 in Enum.reduce/3
    (elixir) lib/range.ex:80: Enumerable.Range.reduce/5
    (elixir) lib/enum.ex:1477: Enum.reduce/3

For now I can work around it by capturing empty comparison lists before calling Diff.diff, but it's still inconvenient.

Extend diff function to work with other types

Current implementation allows comparisons of strings only:

iex(10)> Diff.diff([1,2,3], [1,2,3])
** (FunctionClauseError) no function clause matching in String.Graphemes.graphemes/1

It would be great if this function could be applied for other collections, e.g. lists.
Probably, good solution will be to expose protocol Diffable (or so) with set of operations that are needed to perform a diff. This way, any user will be able to adapt library to his needs.

Let me know what you think about this.
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.