https://github.com/iminside/diffing
Gem for calculating two strings difference
https://github.com/iminside/diffing
Last synced: about 1 year ago
JSON representation
Gem for calculating two strings difference
- Host: GitHub
- URL: https://github.com/iminside/diffing
- Owner: iminside
- License: mit
- Created: 2015-08-03T20:24:07.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2020-09-18T12:10:09.000Z (almost 6 years ago)
- Last Synced: 2025-05-29T04:42:51.036Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 25.4 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Diffing
[](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