Skip to content

Commit 2435ed3

Browse files
committed
Diff + tests
0 parents  commit 2435ed3

File tree

14 files changed

+572
-0
lines changed

14 files changed

+572
-0
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "http://rubygems.org"
2+
gemspec
3+
4+
group :test do
5+
gem 'rake'
6+
end

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) 2015 Captain Train
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
install:
2+
gem build json-diff.gemspec && sudo gem install ./json-diff-*.gem
3+
4+
.PHONY: install

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# `json-diff`
2+
3+
*Take two Ruby objects that can be serialized to JSON. Output an array of operations (additions, deletions, moves) that would convert the first one to the second one.*
4+
5+
```bash
6+
gem install json-diff # Or `gem 'json-diff'` in your Gemfile.
7+
```
8+
9+
```ruby
10+
require 'json-diff'
11+
JsonDiff.diff(1, 2)
12+
#> [{:op => :replace, :path => "/", :value => 2}]
13+
```
14+
15+
Outputs [RFC6902][]. Look at [hana][] for a JSON patch algorithm that can use this output.
16+
17+
[RFC6902]: http://www.rfc-editor.org/rfc/rfc6902.txt
18+
[hana]: https://github.com/tenderlove/hana
19+
20+
# Heart
21+
22+
- Recursive similarity computation between any two Ruby values.
23+
- For arrays, match elements above a certain level of similarity pairwise, and treat them as a move.
24+
- Matching happens highest-similarity first.
25+
- The creation of move operations is generated by detecting rings in the list of moved elements (eg, A → B → C → A).
26+
27+
Pros:
28+
29+
- For lists which are not necessarily ordered, this approach yields far better results than LCS.
30+
- Move operations require no custom code to match elements.
31+
32+
Cons:
33+
34+
- This approach's quality is heavily reliant on how good the similarity algorithm is. Empirically, it yields sensible output. It can be improved by a user-defined procedure.
35+
- There is a computational overhead to the default similarity computation that scales with the total number of entities in the structure.
36+
37+
# Plans & Bugs
38+
39+
Roughly ordered by priority.
40+
41+
- Support adding a custom procedure which computes similarities.
42+
- Support LCS as an option. (The default will remain what yields the best results, regardless of the time it takes.)
43+
- Support specifying a depth for similarity computation.
44+
45+
---
46+
47+
See the LICENSE file for licensing information.

Rakefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$:.push File.expand_path("../lib", __FILE__)
2+
3+
require 'bundler'
4+
Bundler::GemHelper.install_tasks
5+
6+
require 'rspec/core/rake_task'
7+
RSpec::Core::RakeTask.new(:spec)
8+
9+
task default: :spec

json-diff.gemspec

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
2+
require 'json-diff/version'
3+
4+
Gem::Specification.new do |s|
5+
s.name = 'json-diff'
6+
s.license = 'MIT'
7+
s.version = JsonDiff::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ['Captain Train']
10+
s.email = ['[email protected]']
11+
s.homepage = 'http://github.com/captaintrain/json-diff'
12+
s.summary = %q{Compute the difference between two JSON-serializable Ruby objects.}
13+
s.description = %q{Take two Ruby objects that can be serialized to JSON. Output an array of operations (additions, deletions, moves) that would convert the first one to the second one.}
14+
s.files = `git ls-files`.split("\n")
15+
end

lib/json-diff.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'json-diff/diff'
2+
require 'json-diff/index-map'
3+
require 'json-diff/operation'
4+
require 'json-diff/version'

0 commit comments

Comments
 (0)