Skip to content

Commit 607c4ab

Browse files
committed
introduced 'excluded' to ignore keys in hash
1 parent be55639 commit 607c4ab

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/json-diff/diff.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@ def self.diff(before, after, opts = {})
55
include_moves = opts[:moves].nil? ? true : opts[:moves]
66
include_was = opts[:include_was].nil? ? false : opts[:include_was]
77
original_indices = opts[:original_indices].nil? ? false : opts[:original_indices]
8+
excluded_keys = opts[:excluded] || []
89
changes = []
910

1011
if before.is_a?(Hash)
1112
if after.is_a?(Hash)
1213
lost = before.keys - after.keys
1314
lost.each do |key|
15+
next if excluded_keys.include?(key)
16+
1417
inner_path = extend_json_pointer(path, key)
1518
changes << remove(inner_path, include_was ? before[key] : nil)
1619
end
1720

1821
if include_addition
1922
gained = after.keys - before.keys
2023
gained.each do |key|
24+
next if excluded_keys.include?(key)
25+
2126
inner_path = extend_json_pointer(path, key)
2227
changes << add(inner_path, after[key])
2328
end
2429
end
2530

2631
kept = before.keys & after.keys
2732
kept.each do |key|
33+
next if excluded_keys.include?(key)
34+
2835
inner_path = extend_json_pointer(path, key)
2936
changes += diff(before[key], after[key], opts.merge(path: inner_path))
3037
end

0 commit comments

Comments
 (0)