hashdiff 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -13
- data/.travis.yml +1 -1
- data/changelog.md +4 -0
- data/lib/hashdiff/diff.rb +3 -3
- data/lib/hashdiff/util.rb +1 -1
- data/lib/hashdiff/version.rb +1 -1
- data/spec/hashdiff/diff_spec.rb +20 -0
- data/spec/hashdiff/patch_spec.rb +25 -0
- data/spec/hashdiff/util_spec.rb +19 -19
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MzdlMDAxNTk0ZjA4ODNhOWIxNjdiYThhYjNlYjhkODJiYTlhYWJiMQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 47874e343c8689f4bef1893d4d855a9e606d96d8
|
4
|
+
data.tar.gz: a94c89c5a54b9bbef455ba24abcccefdc19bbae6
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ODdiNDdkYTRlMjZkNWFmNTE5NmEzMjRhYTVjZGY5YTEzZjEyMDA1YmZlY2E2
|
11
|
-
ZWRiNjFiOTdiNGU4YzRkYTZhZjk0YjMxNWNmN2Q3ODg5MGY5YTU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZWU1MDBjNTNkZTZjMTRiYzBhOTY2NGY1OGM4OWE0MjI1OGM4ZjM1YzFiMDVk
|
14
|
-
NGRkNzYxZjc1NzkzODEyMzk4OTgzOWQxMjg3Y2IxYjE3ZDE2ZTFjOGY3ZjJj
|
15
|
-
MGMxZDNiNDcyNTBmZjM3YWYxZjc4ZTMyNmE4MTAxYTk0NTJlYWQ=
|
6
|
+
metadata.gz: 6d24313cccba03484ba19fb169676451850786a3d4683631dba275b4da78731fa5251adfb39b8e3c03b7e1388685fad2d7341028888f8198318a5e7d5b4bcb5e
|
7
|
+
data.tar.gz: 62b2ab8565244c3f09f872f00447b837c413f1be2e8275e1f4153e5b3d3fcb1528768f36e0e205d2a24d9742348e70d331a37e40355e79d551f3aa51a411d8b3
|
data/.travis.yml
CHANGED
data/changelog.md
CHANGED
data/lib/hashdiff/diff.rb
CHANGED
@@ -127,7 +127,7 @@ module HashDiff
|
|
127
127
|
added_keys = obj2.keys - obj1.keys
|
128
128
|
|
129
129
|
# add deleted properties
|
130
|
-
deleted_keys.
|
130
|
+
deleted_keys.sort_by{|k,v| k.to_s }.each do |k|
|
131
131
|
custom_result = custom_compare(opts[:comparison], "#{prefix}#{k}", obj1[k], nil)
|
132
132
|
|
133
133
|
if custom_result
|
@@ -138,10 +138,10 @@ module HashDiff
|
|
138
138
|
end
|
139
139
|
|
140
140
|
# recursive comparison for common keys
|
141
|
-
common_keys.
|
141
|
+
common_keys.sort_by{|k,v| k.to_s }.each {|k| result.concat(diff(obj1[k], obj2[k], opts.merge(:prefix => "#{prefix}#{k}"))) }
|
142
142
|
|
143
143
|
# added properties
|
144
|
-
added_keys.
|
144
|
+
added_keys.sort_by{|k,v| k.to_s }.each do |k|
|
145
145
|
unless obj1.key?(k)
|
146
146
|
custom_result = custom_compare(opts[:comparison], "#{prefix}#{k}", nil, obj2[k])
|
147
147
|
|
data/lib/hashdiff/util.rb
CHANGED
@@ -55,7 +55,7 @@ module HashDiff
|
|
55
55
|
# e.g. "a.b[3].c" => ['a', 'b', 3, 'c']
|
56
56
|
def self.decode_property_path(path, delimiter='.')
|
57
57
|
parts = path.split(delimiter).collect do |part|
|
58
|
-
if part =~ /^(
|
58
|
+
if part =~ /^(.*)\[(\d+)\]$/
|
59
59
|
if $1.size > 0
|
60
60
|
[$1, $2.to_i]
|
61
61
|
else
|
data/lib/hashdiff/version.rb
CHANGED
data/spec/hashdiff/diff_spec.rb
CHANGED
@@ -22,6 +22,26 @@ describe HashDiff do
|
|
22
22
|
diff.should == []
|
23
23
|
end
|
24
24
|
|
25
|
+
it "should be able to diff two equal hashes with mixed key types" do
|
26
|
+
a = { 'a' => 1, :b => 1 }
|
27
|
+
diff = HashDiff.diff(a, a)
|
28
|
+
diff.should == []
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be able to diff if mixed key types are removed" do
|
32
|
+
a = { 'a' => 1, :b => 1 }
|
33
|
+
b = {}
|
34
|
+
diff = HashDiff.diff(a, b)
|
35
|
+
diff.should == [["-", "a", 1], ["-", "b", 1]]
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be able to diff if mixed key types are added" do
|
39
|
+
a = { 'a' => 1, :b => 1 }
|
40
|
+
b = {}
|
41
|
+
diff = HashDiff.diff(b, a)
|
42
|
+
diff.should == [["+", "a", 1], ["+", "b", 1]]
|
43
|
+
end
|
44
|
+
|
25
45
|
it "should be able to diff two hashes with equivalent numerics, when strict is false" do
|
26
46
|
diff = HashDiff.diff({ 'a' => 2.0, 'b' => 2 }, { 'a' => 2, 'b' => 2.0 }, :strict => false)
|
27
47
|
diff.should == []
|
data/spec/hashdiff/patch_spec.rb
CHANGED
@@ -61,6 +61,18 @@ describe HashDiff do
|
|
61
61
|
HashDiff.unpatch!(b, diff).should == a
|
62
62
|
end
|
63
63
|
|
64
|
+
it "should be able to patch array under hash key with non-word characters" do
|
65
|
+
a = {"a" => 1, "b-b" => [1, 2]}
|
66
|
+
b = {"a" => 1, "b-b" => [2, 1]}
|
67
|
+
diff = HashDiff.diff(a, b)
|
68
|
+
|
69
|
+
HashDiff.patch!(a, diff).should == b
|
70
|
+
|
71
|
+
a = {"a" => 1, "b-b" => [1, 2]}
|
72
|
+
b = {"a" => 1, "b-b" => [2, 1]}
|
73
|
+
HashDiff.unpatch!(b, diff).should == a
|
74
|
+
end
|
75
|
+
|
64
76
|
it "should be able to patch hash value removal" do
|
65
77
|
a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
|
66
78
|
b = {"a" => 1}
|
@@ -133,4 +145,17 @@ describe HashDiff do
|
|
133
145
|
HashDiff.unpatch!(b, diff).should == a
|
134
146
|
end
|
135
147
|
|
148
|
+
it "should be able to patch hash value removal with custom delimiter" do
|
149
|
+
a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
|
150
|
+
b = {"a" => 1, "b" => {"b1" => 3} }
|
151
|
+
diff = HashDiff.diff(a, b, :delimiter => "\n")
|
152
|
+
|
153
|
+
HashDiff.patch!(a, diff, :delimiter => "\n").should == b
|
154
|
+
|
155
|
+
a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
|
156
|
+
b = {"a" => 1, "b" => {"b1" => 3} }
|
157
|
+
HashDiff.unpatch!(b, diff, :delimiter => "\n").should == a
|
158
|
+
end
|
159
|
+
|
160
|
+
|
136
161
|
end
|
data/spec/hashdiff/util_spec.rb
CHANGED
@@ -14,64 +14,64 @@ describe HashDiff do
|
|
14
14
|
it "should be able to tell similiar hash" do
|
15
15
|
a = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}
|
16
16
|
b = {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}
|
17
|
-
HashDiff.similar?(a, b).should
|
18
|
-
HashDiff.similar?(a, b, :similarity => 1).should
|
17
|
+
HashDiff.similar?(a, b).should be true
|
18
|
+
HashDiff.similar?(a, b, :similarity => 1).should be false
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should be able to tell similiar hash with values within tolerance" do
|
22
22
|
a = {'a' => 1.5, 'b' => 2.25, 'c' => 3, 'd' => 4, 'e' => 5}
|
23
23
|
b = {'a' => 1.503, 'b' => 2.22, 'c' => 3, 'e' => 5}
|
24
|
-
HashDiff.similar?(a, b, :numeric_tolerance => 0.05).should
|
25
|
-
HashDiff.similar?(a, b).should
|
24
|
+
HashDiff.similar?(a, b, :numeric_tolerance => 0.05).should be true
|
25
|
+
HashDiff.similar?(a, b).should be false
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should be able to tell numbers and strings" do
|
29
|
-
HashDiff.similar?(1, 2).should
|
30
|
-
HashDiff.similar?("a", "b").should
|
31
|
-
HashDiff.similar?("a", [1, 2, 3]).should
|
32
|
-
HashDiff.similar?(1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}).should
|
29
|
+
HashDiff.similar?(1, 2).should be false
|
30
|
+
HashDiff.similar?("a", "b").should be false
|
31
|
+
HashDiff.similar?("a", [1, 2, 3]).should be false
|
32
|
+
HashDiff.similar?(1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}).should be false
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should be able to tell true when similarity == 0.5" do
|
36
36
|
a = {"value" => "New1", "onclick" => "CreateNewDoc()"}
|
37
37
|
b = {"value" => "New", "onclick" => "CreateNewDoc()"}
|
38
38
|
|
39
|
-
HashDiff.similar?(a, b, :similarity => 0.5).should
|
39
|
+
HashDiff.similar?(a, b, :similarity => 0.5).should be true
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should be able to tell false when similarity == 0.5" do
|
43
43
|
a = {"value" => "New1", "onclick" => "open()"}
|
44
44
|
b = {"value" => "New", "onclick" => "CreateNewDoc()"}
|
45
45
|
|
46
|
-
HashDiff.similar?(a, b, :similarity => 0.5).should
|
46
|
+
HashDiff.similar?(a, b, :similarity => 0.5).should be false
|
47
47
|
end
|
48
48
|
|
49
49
|
describe '.compare_values' do
|
50
50
|
it "should compare numeric values exactly when no tolerance" do
|
51
|
-
expect(HashDiff.compare_values(10.004, 10.003)).to
|
51
|
+
expect(HashDiff.compare_values(10.004, 10.003)).to be false
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should allow tolerance with numeric values" do
|
55
|
-
expect(HashDiff.compare_values(10.004, 10.003, :numeric_tolerance => 0.01)).to
|
55
|
+
expect(HashDiff.compare_values(10.004, 10.003, :numeric_tolerance => 0.01)).to be true
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should compare other objects with or without tolerance" do
|
59
|
-
expect(HashDiff.compare_values('hats', 'ninjas')).to
|
60
|
-
expect(HashDiff.compare_values('hats', 'ninjas', :numeric_tolerance => 0.01)).to
|
61
|
-
expect(HashDiff.compare_values('horse', 'horse')).to
|
59
|
+
expect(HashDiff.compare_values('hats', 'ninjas')).to be false
|
60
|
+
expect(HashDiff.compare_values('hats', 'ninjas', :numeric_tolerance => 0.01)).to be false
|
61
|
+
expect(HashDiff.compare_values('horse', 'horse')).to be true
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'should compare strings exactly by default' do
|
65
|
-
expect(HashDiff.compare_values(' horse', 'horse')).to
|
66
|
-
expect(HashDiff.compare_values('horse', 'Horse')).to
|
65
|
+
expect(HashDiff.compare_values(' horse', 'horse')).to be false
|
66
|
+
expect(HashDiff.compare_values('horse', 'Horse')).to be false
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'should strip strings before comparing when requested' do
|
70
|
-
expect(HashDiff.compare_values(' horse', 'horse', :strip => true)).to
|
70
|
+
expect(HashDiff.compare_values(' horse', 'horse', :strip => true)).to be true
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should ignore string case when requested" do
|
74
|
-
expect(HashDiff.compare_values('horse', 'Horse', :case_insensitive => true)).to
|
74
|
+
expect(HashDiff.compare_values('horse', 'Horse', :case_insensitive => true)).to be true
|
75
75
|
end
|
76
76
|
|
77
77
|
end
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashdiff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Liu Fengyun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: yard
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bluecloth
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
56
|
-
|
55
|
+
description: " HashDiff is a diff lib to compute the smallest difference between two
|
56
|
+
hashes. "
|
57
57
|
email:
|
58
58
|
- liufengyunchina@gmail.com
|
59
59
|
executables: []
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
- .gitignore
|
64
|
-
- .rspec
|
65
|
-
- .travis.yml
|
66
|
-
- .yardopts
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- ".yardopts"
|
67
67
|
- Gemfile
|
68
68
|
- LICENSE
|
69
69
|
- README.md
|
@@ -93,17 +93,17 @@ require_paths:
|
|
93
93
|
- lib
|
94
94
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
95
|
requirements:
|
96
|
-
- -
|
96
|
+
- - ">="
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: 1.8.7
|
99
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.5.1
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: HashDiff is a diff lib to compute the smallest difference between two hashes.
|