forked from lstrojny/functional-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
entries
and from_entries
, inspired by JavaScript's `Object.en…
…tries` (and Python's `enumerate`) and `Object.from_entries`, respectively (lstrojny#243)
- Loading branch information
1 parent
6012d7b
commit 5f5ddb6
Showing
8 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* @package Functional-php | ||
* @author Hugo Sales <[email protected]> | ||
* @copyright 2021 Lars Strojny | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/lstrojny/functional-php | ||
*/ | ||
|
||
namespace Functional; | ||
|
||
use Functional\Exceptions\InvalidArgumentException; | ||
use Traversable; | ||
|
||
/** | ||
* Inspired by JavaScript’s `Object.entries`, and Python’s `enumerate`, | ||
* convert a key-value map into an array of key-value pairs | ||
* | ||
* @see Functional\from_entries | ||
* @param Traversable|array $collection | ||
* @param int $start | ||
* @return array | ||
* @no-named-arguments | ||
*/ | ||
function entries($collection, int $start = 0) | ||
{ | ||
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); | ||
|
||
$aggregation = []; | ||
foreach ($collection as $key => $value) { | ||
$aggregation[$start++] = [$key, $value]; | ||
} | ||
|
||
return $aggregation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* @package Functional-php | ||
* @author Hugo Sales <[email protected]> | ||
* @copyright 2021 Lars Strojny | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/lstrojny/functional-php | ||
*/ | ||
|
||
namespace Functional; | ||
|
||
use Functional\Exceptions\InvalidArgumentException; | ||
use Traversable; | ||
|
||
/** | ||
* Inspired by JavaScript’s `Object.fromEntries`, | ||
* convert an array of key-value pairs into a key-value map | ||
* | ||
* @see Functional\entries | ||
* @param Traversable|array $collection | ||
* @return array | ||
* @no-named-arguments | ||
*/ | ||
function from_entries($collection) | ||
{ | ||
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); | ||
|
||
$aggregation = []; | ||
foreach ($collection as $entry) { | ||
InvalidArgumentException::assertPair($entry, __FUNCTION__, 1); | ||
[$key, $value] = $entry; | ||
InvalidArgumentException::assertValidArrayKey($key, __FUNCTION__, 1); | ||
$aggregation[$key] = $value; | ||
} | ||
|
||
return $aggregation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/** | ||
* @package Functional-php | ||
* @author Hugo Sales <[email protected]> | ||
* @copyright 2021 Lars Strojny | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/lstrojny/functional-php | ||
*/ | ||
|
||
namespace Functional\Tests; | ||
|
||
use ArrayIterator; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
use function Functional\entries; | ||
use function Functional\from_entries; | ||
|
||
class EntriesFromEntriesTest extends AbstractTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->list = ['value0', 'value1', 'value2', 'value3']; | ||
$this->listIterator = new ArrayIterator($this->list); | ||
$this->hash = ['k0' => 'value0', 'k1' => 'value1', 'k2' => 'value2', 'k3' => 'value3']; | ||
$this->hashIterator = new ArrayIterator($this->hash); | ||
} | ||
|
||
public function testArray(): void | ||
{ | ||
$res = entries($this->list); | ||
self::assertSame(\array_keys($res), \range(0, \count($this->list) - 1)); | ||
self::assertSame(from_entries($res), $this->list); | ||
} | ||
|
||
public function testIterator(): void | ||
{ | ||
$res = entries($this->listIterator); | ||
self::assertSame(\array_keys($res), \range(0, \count($this->listIterator) - 1)); | ||
self::assertSame(from_entries($res), $this->listIterator->getArrayCopy()); | ||
} | ||
|
||
public function testHash(): void | ||
{ | ||
$res = entries($this->hash); | ||
self::assertSame(\array_keys($res), \range(0, \count($this->hash) - 1)); | ||
self::assertSame(from_entries($res), $this->hash); | ||
} | ||
|
||
public function testHashIterator(): void | ||
{ | ||
$res = entries($this->hashIterator); | ||
self::assertSame(\array_keys($res), \range(0, \count($this->hashIterator) - 1)); | ||
self::assertSame(from_entries($res), $this->hashIterator->getArrayCopy()); | ||
} | ||
|
||
public function testHashWithStart(): void | ||
{ | ||
$res = entries($this->hash, 42); | ||
self::assertSame(\array_keys($res), \range(42, 42 + \count($this->hash) - 1)); | ||
self::assertSame(from_entries($res), $this->hash); | ||
} | ||
|
||
public function testPassNoCollection(): void | ||
{ | ||
$this->expectArgumentError('Functional\entries() expects parameter 1 to be array or instance of Traversable'); | ||
entries('invalidCollection'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters