Skip to content

Commit 07b84d7

Browse files
authored
Merge pull request #64 from JordiCabezudo/master
[key] fix possible errors with some key values
2 parents 91af1d3 + e42f96b commit 07b84d7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/key.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
*/
1616
function key($value, $coll, $default = null)
1717
{
18-
return array_search($value, $coll) ? : $default;
18+
$key = array_search($value, $coll);
19+
20+
return false !== $key ? $key : $default;
1921
}

tests/KeyTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Lambdish\Phunctional\Tests;
44

5-
use ArrayIterator;
65
use PHPUnit_Framework_TestCase;
76
use function Lambdish\Phunctional\key;
87

@@ -31,4 +30,28 @@ public function it_should_return_the_default_value_provided_if_the_key_does_not_
3130

3231
$this->assertSame('three', key(3, $actual, 'three'));
3332
}
33+
34+
/** @test */
35+
public function it_should_return_empty_string_if_the_key_does_is_empty()
36+
{
37+
$actual = ['one' => 1, 'two' => 2, '' => 3];
38+
39+
$this->assertSame('', key(3, $actual, 'default'));
40+
}
41+
42+
/** @test */
43+
public function it_should_return_the_value_of_the_item_of_an_existent_boolean_key()
44+
{
45+
$actual = ['one' => 1, 'two' => false];
46+
47+
$this->assertSame('two', key(false, $actual, 'default'));
48+
}
49+
50+
/** @test */
51+
public function it_should_return_first_occurrence_of_the_item_of_an_existent_key()
52+
{
53+
$actual = [false => 1];
54+
55+
$this->assertSame(0, key(1, $actual, 'default'));
56+
}
3457
}

0 commit comments

Comments
 (0)