Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multi dimentional arrays in path decoder #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/hashdiff/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def self.decode_property_path(path, delimiter = '.')
path.split(delimiter).inject([]) do |memo, part|
if part =~ /^(.*)\[(\d+)\]$/
if !Regexp.last_match(1).empty?
memo + [Regexp.last_match(1), Regexp.last_match(2).to_i]
inner_decode = self.decode_property_path(Regexp.last_match(1), delimiter)
memo + [*inner_decode, Regexp.last_match(2).to_i]
else
memo + [Regexp.last_match(2).to_i]
end
Expand Down
10 changes: 10 additions & 0 deletions spec/hashdiff/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@
expect(described_class.comparable?(1, {})).to be false
end
end

describe '.decode_property_path' do
it 'decodes according to doc' do
expect(described_class.decode_property_path('a.b[3].c')).to eq(['a', 'b', 3, 'c'])
end

it 'decodes nested array' do
expect(described_class.decode_property_path('a[1][2]')).to eq(['a', 1, 2])
end
end
end