Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iminside/diffing

Gem for calculating two strings difference
https://github.com/iminside/diffing

Last synced: 1 day ago
JSON representation

Gem for calculating two strings difference

Awesome Lists containing this project

README

        

# Diffing

[![Build Status](https://travis-ci.org/denis-churbanov/Diffing.svg?branch=master)](https://travis-ci.org/denis-churbanov/Diffing)

## Installation

```
sudo gem install diffing
```

## Usage

```ruby
require 'diffing'

from = "Hello! I am string for diffing test"
to = "Hi! I am two string for diffing"
```

#### By chars
```ruby
diff = Diffing.by_chars( from, to )

diff.as_ascii
# => H{`ello`>>`i`}! I am{+` two`} string for diffing{-` test`}
diff.as_html
# => Helloi! I am two string for diffing test
```

#### By words
```ruby
diff = Diffing.by_words( from, to )

diff.as_ascii
# => {`Hello!`>>`Hi!`} I am{+` two`} string for diffing{-` test`}
diff.as_html
# => Hello!Hi! I am two string for diffing test
```

#### By lines
```ruby
diff = Diffing.by_lines( from, to )

diff.as_ascii
# => {`Hello! I am string for diffing test`>>`Hi! I am two string for diffing`}
diff.as_html
# => Hello! I am string for diffing testHi! I am two string for diffing
```

## Custom format

```ruby
module CustomFormat
class << self

def source( value )
value
end

def insert( value )
"(++#{ value })"
end

def remove( value )
"(--#{ value })"
end

def change( from, to )
"(#{ from } => #{ to })"
end

end
end

Diffing.by_chars( from, to ).format( CustomFormat )
# => H(ello => i)! I am(++ two) string for diffing(-- test)
Diffing.by_words( from, to ).format( CustomFormat )
# => (Hello! => Hi!) I am(++ two) string for diffing(-- test)
Diffing.by_lines( from, to ).format( CustomFormat )
# => (Hello! I am string for diffing test => Hi! I am two string for diffing)

```

## Custom use separated parts

```ruby
Diffing.by_words( from, to ).parts.map { |part|

result = ''
result << "" if part.source?
result << "" if part.insert?
result << "" if part.remove?
result

}.join
# =>
```

## Custom pattern of parts

```ruby
Diffing::Diff.new( from, to, /.{,3}\s?/ ).as_ascii
# => {`Hello! `>>`Hi! `}I a{`m string `>>`m two string `}for diffin{`g test`>>`g`}
```

## Copyright

Copyright © 2015 Denis Churbanov