json-diff 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2079e5281095664f9827f175517045a3164b675b
4
- data.tar.gz: 6a7d698b88209ed747f621ab609d3a735270fec0
3
+ metadata.gz: 7ff98bfabbd5790a20441a9efb6abbc3ff11214a
4
+ data.tar.gz: 36be41ebbaee8a09fedf8199a81e236692261d1a
5
5
  SHA512:
6
- metadata.gz: bb9cf0f504e1e0892697bee3d5b4beefa7f0eff93fadc57c9bbfccc852a7f4f5c3210a02a74d1d0bc0141887437224182ee5116c9d9e92c2a5506f634d77da0e
7
- data.tar.gz: a045d4d6507b8834ce9bc92f8be212777777b94cc15068a362649b608887af9dcb92e8172913bfb82a2230f9c279e366ba99eff972af2a04221cba60ae17d04f
6
+ metadata.gz: b140221da9b734eaaf0532e8391b50e0dca60d6ea39c3b4a0b8c0c0952392ac02e5f65864242a5f131934fd75e02a74de16a5f59c8e9182beb14b1c86201d0a7
7
+ data.tar.gz: dea0ec9287eb8e4bbe5f8948e3ec9a398408d26b0f1a5f8af9774e14a406686493d60420232412d82c168e5f60c7372eafc2ba89f3a05471ae37a3d7ca643cb7
data/README.md CHANGED
@@ -23,6 +23,7 @@ Outputs [RFC6902][]. Look at [hana][] for a JSON patch algorithm that can use th
23
23
  - `moves`\*: include move operations. Set it to false to remove clutter.
24
24
  - `additions`\*: include add operations. Se it to false to remove clutter.
25
25
  - `original_indices`\*: array indices are those from the source array (for `from` fields, or `path` fields on remove operations) or the target array (for other `path` fields). It eases manual checking of differences.
26
+ - `similarity`: procedure taking (before, after) objects. Returns a probability between 0 and 1 of how likely `after` is a modification of `before`, or nil if you wish to fall back to the default computation.
26
27
 
27
28
  \* Changing this option prevents the use of the output for JSON patching.
28
29
 
@@ -92,9 +93,10 @@ JsonDiff.generate(
92
93
 
93
94
  Roughly ordered by priority.
94
95
 
95
- - Support adding a custom procedure which computes similarities.
96
96
  - Support LCS as an option. (The default will remain what yields the best results, regardless of the time it takes.)
97
97
  - Support specifying a depth for similarity computation.
98
+ - Character-wise substring add and remove operations.
99
+ - SVG output.
98
100
 
99
101
  ---
100
102
 
data/json-diff.gemspec CHANGED
@@ -6,9 +6,9 @@ Gem::Specification.new do |s|
6
6
  s.license = 'MIT'
7
7
  s.version = JsonDiff::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ['Captain Train']
9
+ s.authors = ['Thaddée Tyl']
10
10
  s.email = ['ttyl@captaintrain.com']
11
- s.homepage = 'http://github.com/captaintrain/json-diff'
11
+ s.homepage = 'http://github.com/espadrine/json-diff'
12
12
  s.summary = %q{Compute the difference between two JSON-serializable Ruby objects.}
13
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
14
  s.files = `git ls-files`.split("\n")
@@ -50,7 +50,7 @@ module JsonDiff
50
50
  changes << remove(inner_path, include_was ? item : nil)
51
51
  end
52
52
  else
53
- pairing = array_pairing(before, after)
53
+ pairing = array_pairing(before, after, opts)
54
54
  # FIXME: detect replacements.
55
55
 
56
56
  # All detected moves that do not reach the similarity limit are deleted
@@ -111,11 +111,15 @@ module JsonDiff
111
111
  # {pairs: [[before index, after index, similarity]],
112
112
  # removed: [before index],
113
113
  # added: [after index]}
114
- def self.array_pairing(before, after)
114
+ #
115
+ # - options[:similarity]: procedure taking (before, after) objects.
116
+ # Returns a probability between 0 and 1 of how likely `after` is a
117
+ # modification of `before`, or nil if it cannot determine it.
118
+ def self.array_pairing(before, after, options)
115
119
  # Array containing the array of similarities from before to after.
116
120
  similarities = before.map do |before_item|
117
121
  after.map do |after_item|
118
- similarity(before_item, after_item)
122
+ similarity(before_item, after_item, options)
119
123
  end
120
124
  end
121
125
 
@@ -181,10 +185,18 @@ module JsonDiff
181
185
 
182
186
  # Compute an arbitrary notion of how probable it is that one object is the
183
187
  # result of modifying the other.
184
- def self.similarity(before, after)
188
+ #
189
+ # - options[:similarity]: procedure taking (before, after) objects.
190
+ # Returns a probability between 0 and 1 of how likely `after` is a
191
+ # modification of `before`, or nil if it cannot determine it.
192
+ def self.similarity(before, after, options)
185
193
  return 0.0 if before.class != after.class
186
194
 
187
- # FIXME: call custom similarity procedure.
195
+ # Use the custom similarity procedure if it isn't nil.
196
+ if options[:similarity] != nil
197
+ custom_result = options[:similarity].call(before, after)
198
+ return custom_result if custom_result != nil
199
+ end
188
200
 
189
201
  if before.is_a?(Hash)
190
202
  if before.size == 0
@@ -199,7 +211,7 @@ module JsonDiff
199
211
  # We don't consider key renames.
200
212
  similarities = []
201
213
  before.each do |before_key, before_item|
202
- similarities << similarity(before_item, after[before_key])
214
+ similarities << similarity(before_item, after[before_key], options)
203
215
  end
204
216
  # Also consider keys' names.
205
217
  before_keys = before.keys
@@ -216,7 +228,7 @@ module JsonDiff
216
228
  # similarity between each elements of the list.
217
229
  similarities = before.map do |before_item|
218
230
  after.map do |after_item|
219
- similarity(before_item, after_item)
231
+ similarity(before_item, after_item, options)
220
232
  end.max || 0.0
221
233
  end
222
234
 
@@ -1,3 +1,3 @@
1
1
  module JsonDiff
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -62,8 +62,8 @@ describe JsonDiff do
62
62
  expect(diff).to eql([
63
63
  {'op' => 'replace', 'path' => "/2/pillar", 'was' => 0, 'value' => 1},
64
64
  {'op' => 'remove', 'path' => "/0", 'was' => "laundry"},
65
- {'op' => 'move', 'from' => "/2", 'path' => "/0"},
66
- {'op' => 'move', 'from' => "/1", 'path' => "/2"},
65
+ {'op' => 'move', 'from' => "/0", 'path' => "/2"},
66
+ {'op' => 'move', 'from' => "/1", 'path' => "/0"},
67
67
  {'op' => 'add', 'path' => "/2", 'value' => 3},
68
68
  ])
69
69
  end
@@ -116,4 +116,35 @@ describe JsonDiff do
116
116
  ])
117
117
  end
118
118
 
119
+ it "should be able to diff two objects with a custom similarity" do
120
+ similarity = -> (before, after) do
121
+ if before.is_a?(Hash) && after.is_a?(Hash)
122
+ if before[:id] == after[:id]
123
+ 1.0
124
+ else
125
+ 0.0
126
+ end
127
+ end
128
+ end
129
+
130
+ diff = JsonDiff.diff([
131
+ {id: 1, we: "must", start: "somewhere"},
132
+ {id: 2, and: "this", will: "do"},
133
+ ], [
134
+ {id: 2, insert: "something", completely: "different"},
135
+ {id: 1, this: "too", is: "different"},
136
+ ], similarity: similarity)
137
+ expect(diff).to eql([
138
+ {'op' => 'remove', 'path' => '/0/we'},
139
+ {'op' => 'remove', 'path' => '/0/start'},
140
+ {'op' => 'add', 'path' => '/0/this', 'value' => 'too'},
141
+ {'op' => 'add', 'path' => '/0/is', 'value' => 'different'},
142
+ {'op' => 'remove', 'path' => '/1/and'},
143
+ {'op' => 'remove', 'path' => '/1/will'},
144
+ {'op' => 'add', 'path' => '/1/insert', 'value' => 'something'},
145
+ {'op' => 'add', 'path' => '/1/completely', 'value' => 'different'},
146
+ {'op' => 'move', 'from' => '/0', 'path' => '/1'},
147
+ ])
148
+ end
149
+
119
150
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Captain Train
7
+ - Thaddée Tyl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-13 00:00:00.000000000 Z
11
+ date: 2016-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Take two Ruby objects that can be serialized to JSON. Output an array
14
14
  of operations (additions, deletions, moves) that would convert the first one to
@@ -19,8 +19,8 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - .gitignore
23
- - .rspec
22
+ - ".gitignore"
23
+ - ".rspec"
24
24
  - Gemfile
25
25
  - LICENSE
26
26
  - Makefile
@@ -36,7 +36,7 @@ files:
36
36
  - spec/json-diff/index-map_spec.rb
37
37
  - spec/json-diff/operation_spec.rb
38
38
  - spec/spec_helper.rb
39
- homepage: http://github.com/captaintrain/json-diff
39
+ homepage: http://github.com/espadrine/json-diff
40
40
  licenses:
41
41
  - MIT
42
42
  metadata: {}
@@ -46,17 +46,17 @@ require_paths:
46
46
  - lib
47
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
- - - '>='
49
+ - - ">="
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - '>='
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.0.14
59
+ rubygems_version: 2.4.5.1
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Compute the difference between two JSON-serializable Ruby objects.