diff options
author | Urban Müller | 2017-09-27 16:02:05 +0200 |
---|---|---|
committer | Urban Müller | 2017-09-27 16:03:09 +0200 |
commit | 5d52cf51cc8cae4f29dc2c34e1a7e1993a68622d (patch) | |
tree | 2203644a1224823cfffd9329234483f3d2f004f1 | |
parent | 4bf376c1a603caa7bd093a1c36817f8ea21180b1 (diff) | |
download | itools-5d52cf51cc8cae4f29dc2c34e1a7e1993a68622d.tar.gz itools-5d52cf51cc8cae4f29dc2c34e1a7e1993a68622d.tar.bz2 itools-5d52cf51cc8cae4f29dc2c34e1a7e1993a68622d.zip |
allow 3-argument form of explode, it::match etc
-rw-r--r-- | it_pipe.class | 5 | ||||
-rwxr-xr-x | tests/it_pipe.t | 5 |
2 files changed, 8 insertions, 2 deletions
diff --git a/it_pipe.class b/it_pipe.class index 5ab7e8a..8c7f92f 100644 --- a/it_pipe.class +++ b/it_pipe.class @@ -2,7 +2,7 @@ class it_pipe implements Iterator { - static $lastargfunc = array('explode' => 1, 'preg_match' => 1, 'preg_split' => 1, 'it__match' => 1, 'it__replace' => 1); + static $parampositions = array('explode' => 1, 'preg_match' => 1, 'preg_split' => 1, 'it__match' => 1, 'it__replace' => 1); var $lines; # all lines currently in pipe var $_valid; @@ -29,7 +29,8 @@ function __construct($p = array()) */ function __call($name, $params) { - list($parampos) = self::$lastargfunc[$name] ? array(count($params)) : array(0, array_unshift($params, "")); + $parampos = self::$parampositions[$name] ?: 0; + array_splice($params, $parampos, 0, [null]); # create room for inserted line arg $func = ($t = it::match('(\w+)__(\w+)', $name)) ? array($t[0], $t[1]) : (method_exists($this, $name) ? array($this, $name) : $name); foreach($this->lines as $i => $params[$parampos]) diff --git a/tests/it_pipe.t b/tests/it_pipe.t index 93957b5..16e2c29 100755 --- a/tests/it_pipe.t +++ b/tests/it_pipe.t @@ -10,4 +10,9 @@ is(json_encode((new it_pipe(['data' => ["a\tb", "1\t2"]]))->csv("c,d")), '[{"c": is(json_encode((new it_pipe(['data' => ["a\tb", "1\t2"]]))->csv(['forceschema' => "c,d"])), '[{"c":"1","d":"2"}]'); is(json_encode((new it_pipe(['data' => ["a b\tb", "1\t2"]]))->csv(['fixcolnames' => true])), '[{"a_b":"1","b":"2"}]'); +# askey() is(json_encode((new it_pipe(['data' => "a\nb\n"]))->askey()), '{"a":true,"b":true}'); + +# __call() generic funcs +is((new it_pipe(['data' => [" a a "]]))->trim()->lines[0], "a a"); +is((new it_pipe(['data' => [" a a "]]))->it__match('\w', ['all' => true])->lines[0], ["a", "a"], "test argument in second pos"); |