Skip to content

Commit

Permalink
Merge pull request #64 from JordiCabezudo/master
Browse files Browse the repository at this point in the history
[key] fix possible errors with some key values
  • Loading branch information
eloipoch authored Feb 9, 2018
2 parents 91af1d3 + e42f96b commit 07b84d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/key.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
*/
function key($value, $coll, $default = null)
{
return array_search($value, $coll) ? : $default;
$key = array_search($value, $coll);

return false !== $key ? $key : $default;
}
25 changes: 24 additions & 1 deletion tests/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lambdish\Phunctional\Tests;

use ArrayIterator;
use PHPUnit_Framework_TestCase;
use function Lambdish\Phunctional\key;

Expand Down Expand Up @@ -31,4 +30,28 @@ public function it_should_return_the_default_value_provided_if_the_key_does_not_

$this->assertSame('three', key(3, $actual, 'three'));
}

/** @test */
public function it_should_return_empty_string_if_the_key_does_is_empty()
{
$actual = ['one' => 1, 'two' => 2, '' => 3];

$this->assertSame('', key(3, $actual, 'default'));
}

/** @test */
public function it_should_return_the_value_of_the_item_of_an_existent_boolean_key()
{
$actual = ['one' => 1, 'two' => false];

$this->assertSame('two', key(false, $actual, 'default'));
}

/** @test */
public function it_should_return_first_occurrence_of_the_item_of_an_existent_key()
{
$actual = [false => 1];

$this->assertSame(0, key(1, $actual, 'default'));
}
}

0 comments on commit 07b84d7

Please sign in to comment.