Skip to content

Commit 700e766

Browse files
committed
Add Function.ary function
1 parent 6e75f93 commit 700e766

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Function/ary.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
namespace _;
13+
14+
/**
15+
* Creates a function that invokes `func`, with up to `n` arguments,
16+
* ignoring any additional arguments.
17+
*
18+
* @category Function
19+
*
20+
* @param callable $func The function to cap arguments for.
21+
* @param int $n The arity cap.
22+
*
23+
* @return Callable Returns the new capped function.
24+
*
25+
* @example
26+
* <code>
27+
* map(['6', '8', '10'], ary('intval', 1));
28+
* // => [6, 8, 10]
29+
* </code>
30+
*/
31+
function ary(callable $func, int $n): callable
32+
{
33+
return function (...$args) use ($func, $n) {
34+
\array_splice($args, $n);
35+
36+
return $func(...$args);
37+
};
38+
}

tests/Function/AryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
use function _\ary;
13+
use function _\map;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class AryTest extends TestCase
17+
{
18+
public function testAry()
19+
{
20+
$this->assertSame([6, 8, 10], map(['6', '8', '10'], ary('intval', 1)));
21+
}
22+
}

0 commit comments

Comments
 (0)