diff options
author | Urban Müller | 2023-07-18 19:33:59 +0200 |
---|---|---|
committer | Urban Müller | 2023-07-18 19:33:59 +0200 |
commit | c3b2d0515f7631fa4ffd693ef17bbb061064b260 (patch) | |
tree | 224356ba5eca04bc57475801574a040c46cf1ebd | |
parent | 1feca783d60c0fdf0c60dd65801eed07b09cb4b7 (diff) | |
download | itools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.tar.gz itools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.tar.bz2 itools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.zip |
more concise arg merging
-rw-r--r-- | it.class | 6 | ||||
-rwxr-xr-x | test/it_pipe.t | 11 |
2 files changed, 8 insertions, 9 deletions
@@ -1228,11 +1228,9 @@ static function cat($p = null) */ static function pipe($cmd, ...$args) { - $values = []; - foreach ($args as $arg) - $values = array_merge($values, (array)$arg); + $args = array_reduce($args, fn($carry, $v) => array_merge($carry, (array)$v), []); # varargs to single arr - return new it_pipe(is_string($cmd) ? array('cmd' => $cmd, 'args' => $values) : array('data' => $cmd)); + return new it_pipe(is_string($cmd) ? array('cmd' => $cmd, 'args' => $args) : array('data' => $cmd)); } /** diff --git a/test/it_pipe.t b/test/it_pipe.t index f6b069a..79c27b2 100755 --- a/test/it_pipe.t +++ b/test/it_pipe.t @@ -2,11 +2,12 @@ <?php # it::cat frontend -is(it::cat("test/it_pipe.t")->lines[1], "<?php"); -is(it::cat(['data' => "foo"])->lines(), ["foo"]); -is(it::pipe('echo {x}', ['x' => "foo"])->lines(), ["foo"]); -is(it::pipe('echo {0}{1}', "f", "oo")->lines(), ["foo"]); -is(it::pipe(['foo'])->lines(), ["foo"]); +is(it::cat("test/it_pipe.t")->lines[1], "<?php"); +is(it::cat(['data' => "foo"])->lines(), ["foo"]); +is(it::pipe('echo {x}', ['x' => "foo"])->lines(), ["foo"]); +is(it::pipe('echo {0}{1}', "f", "oo")->lines(), ["foo"]); +is(it::pipe('echo {f}', ['f' => 1, 'f' => 2])->lines(), [2]); +is(it::pipe(['foo'])->lines(), ["foo"]); # csv() is(json_encode((new it_pipe(['data' => ["a\tb", "1\t2"]]))->csv()), '[{"a":"1","b":"2"}]'); |