1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/www/server/bin/php
<?php
# it::cat frontend
is(it::cat($argv[0])->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"]);
foreach(it::pipe(['a','b','a']) as $i) $x1 .= $i;
is($x1, "aba");
foreach(it::pipe(['a','b','a'])->filter(fn($x) => $x == 'a') as $k => $v) $x2 .= "$k$v";
is($x2, "0a1a");
foreach(it::pipe(['a','b','a'])->grep('a') as $k => $v) $x3 .= "$k$v";
is($x3, "0a1a");
# csv()
is(json_encode((new it_pipe(['data' => ["a\tb", "1\t2"]]))->csv()), '[{"a":"1","b":"2"}]');
is(json_encode((new it_pipe(['data' => ["a;b", "1;2"]]))->csv()), '[{"a":"1","b":"2"}]');
is(json_encode((new it_pipe(['data' => ["a,b", "1,2"]]))->csv()), '[{"a":"1","b":"2"}]');
is(json_encode((new it_pipe(['data' => ["\t", "1\t2"]]))->csv()), '[{"field0":"1","field1":"2"}]');
is(json_encode((new it_pipe(['data' => ["a\tb", "1\t2"]]))->csv("c,d")), '[{"c":"1","d":"2"}]');
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"}]');
is(iterator_to_array((new it_pipe(['data' => ["a b\tb", "1\t2"]]))->cols('c')), [(object)['c' => 'a b'], (object)['c' => '1']], "cols() ignoring some columns");
# askey()
is(json_encode((new it_pipe(['data' => "a\nb\n"]))->askey()), '{"a":true,"b":true}');
# keyval()
is(json_encode((new it_pipe(['data' => "a\tA\nb\tB\n"]))->keyval()), '{"a":"A","b":"B"}');
is(json_encode((new it_pipe(['data' => "a,A\nb,B\n"]))->keyval(",")), '{"a":"A","b":"B"}');
# swap()
is(json_encode((new it_pipe(['data' => "a\tA\nb\tB\n"]))->swap()->lines), '["A\ta","B\tb"]');
is(json_encode((new it_pipe(['data' => "a,A\nb,B\n"]))->swap(',')->lines), '["A,a","B,b"]');
# cut()
is(json_encode((new it_pipe(['data' => "a\tA\t1\nb\tB\t2\n"]))->cut('0,2')->lines), '["a\t1","b\t2"]');
is(json_encode((new it_pipe(['data' => "a\tA\t1\nb\tB\t2\n"]))->cut('2,0')->lines), '["1\ta","2\tb"]');
is(json_encode((new it_pipe(['data' => "a\tA\t1\nb\tB\t2\n"]))->cut('1')->lines), '["A","B"]');
is(json_encode((new it_pipe(['data' => "a,A,1\nb,B,2\n"]))->cut('0,2', ',')->lines), '["a,1","b,2"]');
is(json_encode((new it_pipe(['data' => "a,A,1\nb,B,2\n"]))->cut('2,0', ',')->lines), '["1,a","2,b"]');
is(json_encode((new it_pipe(['data' => "a,A,1\nb,B,2\n"]))->cut('1', ',')->lines), '["A","B"]');
# __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");
|