File tree 2 files changed +27
-2
lines changed
2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
function key ($ value , $ coll , $ default = null )
17
17
{
18
- return array_search ($ value , $ coll ) ? : $ default ;
18
+ $ key = array_search ($ value , $ coll );
19
+
20
+ return false !== $ key ? $ key : $ default ;
19
21
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Lambdish \Phunctional \Tests ;
4
4
5
- use ArrayIterator ;
6
5
use PHPUnit_Framework_TestCase ;
7
6
use function Lambdish \Phunctional \key ;
8
7
@@ -31,4 +30,28 @@ public function it_should_return_the_default_value_provided_if_the_key_does_not_
31
30
32
31
$ this ->assertSame ('three ' , key (3 , $ actual , 'three ' ));
33
32
}
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
+ }
34
57
}
You can’t perform that action at this time.
0 commit comments