Skip to content

Commit

Permalink
Merge pull request #4 from 7NXT/feature-03-get-a-key-from-an-array
Browse files Browse the repository at this point in the history
fixing `undefined method `has_key?'` when an array is as element given
  • Loading branch information
brice-morin authored Aug 17, 2018
2 parents b7ff881 + 204ec09 commit 3c86c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rodash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def self.isKey(value, object)
return true if value.is_a? Numeric
return !value.is_a?(Array) &&
(@@reIsPlainProp =~ value || !@@reIsDeepProp =~ value ||
(!object.nil? && object.has_key?(value)))
(!object.nil? && !object.is_a?(Array) && object.has_key?(value)))
end

def self.baseToPath(value)
Expand Down
7 changes: 7 additions & 0 deletions test/test_rodash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ class GetTest < Test::Unit::TestCase
end
end

should "get a key from an array" do
object = {'a' => 1}, {'a' => 2}

assert_equal Rodash.get(object, '[0].a'), 1
assert_equal Rodash.get(object, '[1].a'), 2
end

should "no coerce array paths to strings" do
object = {'a,b,c' => 3, 'a' => { 'b' => { 'c' => 4 } } }
assert_equal Rodash.get(object, ['a', 'b', 'c']), 4
Expand Down

0 comments on commit 3c86c04

Please sign in to comment.