From c3b2d0515f7631fa4ffd693ef17bbb061064b260 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Tue, 18 Jul 2023 19:33:59 +0200 Subject: more concise arg merging --- it.class | 6 ++---- test/it_pipe.t | 11 ++++++----- 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 @@ lines[1], " "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], " "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"}]'); -- cgit v1.2.3