summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2023-07-18 19:33:59 +0200
committerUrban Müller2023-07-18 19:33:59 +0200
commitc3b2d0515f7631fa4ffd693ef17bbb061064b260 (patch)
tree224356ba5eca04bc57475801574a040c46cf1ebd
parent1feca783d60c0fdf0c60dd65801eed07b09cb4b7 (diff)
downloaditools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.tar.gz
itools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.tar.bz2
itools-c3b2d0515f7631fa4ffd693ef17bbb061064b260.zip
more concise arg merging
-rw-r--r--it.class6
-rwxr-xr-xtest/it_pipe.t11
2 files changed, 8 insertions, 9 deletions
diff --git a/it.class b/it.class
index 6e294c3..098e57d 100644
--- a/it.class
+++ b/it.class
@@ -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"}]');