Skip to content

Commit 16f9a04

Browse files
committed
Add Function.overArgs function
1 parent 79444f3 commit 16f9a04

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/Function/overArgs.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
use function _\internal\arrayMap;
15+
use function _\internal\baseFlatten;
16+
use function _\internal\baseRest;
17+
use function _\internal\baseUnary;
18+
19+
/**
20+
* Creates a function that invokes `func` with its arguments transformed.
21+
*
22+
* @static
23+
* @memberOf _
24+
* @category Function
25+
*
26+
* @param callable $func The function to wrap.
27+
* @param callable[] $transforms The argument transforms.
28+
*
29+
* @return callable the new function.
30+
*
31+
* @example
32+
* <code>
33+
* function doubled($n) {
34+
* return $n * 2;
35+
* }
36+
*
37+
* function square($n) {
38+
* return $n * $n;
39+
* }
40+
*
41+
* $func = overArgs(function($x, $y) {
42+
* return [$x, $y];
43+
* }, ['square', 'doubled']);
44+
*
45+
* $func(9, 3);
46+
* // => [81, 6]
47+
*
48+
* $func(10, 5);
49+
* // => [100, 10]
50+
* </code>
51+
*/
52+
function overArgs(callable $func, array $transforms): callable
53+
{
54+
return baseRest(function ($func, $transforms) {
55+
$transforms = (count($transforms) == 1 && \is_array($transforms[0]))
56+
? arrayMap($transforms[0], baseUnary('\_\internal\baseIteratee'))
57+
: arrayMap(baseFlatten($transforms, 1), baseUnary('\_\internal\baseIteratee'));
58+
59+
$funcsLength = \count($transforms);
60+
61+
return baseRest(function (...$args) use ($funcsLength, $transforms, $func) {
62+
$index = -1;
63+
$length = \min(\count($args), $funcsLength);
64+
65+
while (++$index < $length) {
66+
$args[$index] = $transforms[$index]($args[$index]);
67+
}
68+
69+
return $func(...$args);
70+
});
71+
})($func, $transforms);
72+
}

tests/Function/OverArgsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 PHPUnit\Framework\TestCase;
13+
use function _\overArgs;
14+
15+
class OverArgsTest extends TestCase
16+
{
17+
public function testOverArgs()
18+
{
19+
function doubled($n)
20+
{
21+
return $n * 2;
22+
}
23+
24+
function square($n)
25+
{
26+
return $n * $n;
27+
}
28+
29+
$func = overArgs(function ($x, $y) {
30+
return [$x, $y];
31+
}, ['square', 'doubled']);
32+
33+
$this->assertSame([81, 6], $func(9, 3));
34+
35+
$this->assertSame([100, 10], $func(10, 5));
36+
}
37+
}

0 commit comments

Comments
 (0)