Fix dig bug for Array
This commit is contained in:
parent
2f740ebbce
commit
03cd78d734
|
@ -29,6 +29,7 @@ scheme are considered to be bugs.
|
|||
### Fixed
|
||||
|
||||
* [#369](https://github.com/intridea/hashie/pull/369): If a translation for a property exists when using IndifferentAccess and IgnoreUndeclared, use the translation to find the property - [@whitethunder](https://github.com/whitethunder).
|
||||
* [#376](https://github.com/intridea/hashie/pull/376): Leave string index unchanged if it can't be converted to integer for Array#dig - [@sazor](https://github.com/sazor).
|
||||
|
||||
### Security
|
||||
|
||||
|
|
|
@ -7,7 +7,14 @@ module Hashie
|
|||
include Hashie::Extensions::RubyVersionCheck
|
||||
with_minimum_ruby('2.3.0') do
|
||||
def dig(*indexes)
|
||||
super(*indexes.map { |idx| Integer(idx) })
|
||||
converted_indexes = indexes.map do |idx|
|
||||
begin
|
||||
Integer(idx)
|
||||
rescue ArgumentError
|
||||
idx
|
||||
end
|
||||
end
|
||||
super(*converted_indexes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,6 +12,18 @@ describe Array do
|
|||
it 'works with a numeric index' do
|
||||
expect(array.dig(1)).to eq(:b)
|
||||
end
|
||||
|
||||
context 'when array is empty' do
|
||||
let(:array) { Hashie::Array.new([]) }
|
||||
|
||||
it 'works with a first numeric and next string index' do
|
||||
expect(array.dig(0, 'hello')).to eq(nil)
|
||||
end
|
||||
|
||||
it 'throws an error with first string and next numeric index' do
|
||||
expect { array.dig('hello', 0) }.to raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue