From 89329b66264560864f5a79e90260cea11423ef5b Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Sun, 28 Aug 2022 23:13:06 +0400 Subject: [PATCH] Handle multi dimentional arrays in path decoder --- lib/hashdiff/util.rb | 3 ++- spec/hashdiff/util_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/hashdiff/util.rb b/lib/hashdiff/util.rb index 09ed84b..455f472 100644 --- a/lib/hashdiff/util.rb +++ b/lib/hashdiff/util.rb @@ -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 diff --git a/spec/hashdiff/util_spec.rb b/spec/hashdiff/util_spec.rb index b9db735..b3ef736 100644 --- a/spec/hashdiff/util_spec.rb +++ b/spec/hashdiff/util_spec.rb @@ -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