Skip to content

Commit

Permalink
1.33 - Added Added commands: csv.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timofey Potapov committed Nov 5, 2024
1 parent 16ceeea commit 9caa0af
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 14 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ my $builder = $class->new(
'Set::Scalar' => '0',
'Sub::Util' => '0',
'Term::Table' => '0',
'Text::CSV_XS' => '0',
'Tiny::Prof' => '0.03',
'YAML::XS' => '0',
},
Expand Down
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Revision history for e

=================
1.33 - 2024-11-05
=================

Added commands: csv.

=================
1.32 - 2024-10-30
=================
Expand Down
112 changes: 107 additions & 5 deletions lib/e.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ package e;
⠹⡽⣾⣿⠹⣿⣆⣾⢯⣿⣿ ⡞ ⠻⣿⣿⣿⠁ ⢠⣿⢏ ⡀ ⡟ ⢀⣴⣿⠃⢁⡼⠁ ⠈
⠈⠛ ⢻⣿⣧⢸⢟⠶⢾⡇ ⣸⡿⠁ ⢠⣾⡟⢼ ⣷ ⡇ ⣰⠋⠙⠁
⠈⣿⣻⣾⣦⣇⢸⣇⣀⣶⡿⠁⣀⣀⣾⢿⡇⢸ ⣟⡦⣧⣶⠏ unleashed
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.32
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.33
⢻⡄ ⠐⠚⠋⣠⡾⣧⣿⠁⠙⢳⣽⡟
⠈⠳⢦⣤⣤⣀⣤⡶⠛ ⠈⢿⡆ ⢿⡇
⠈ ⠈⠓ ⠈
Expand All @@ -45,7 +45,7 @@ use 5.006;
use strict;
use warnings;

our $VERSION = '1.32';
our $VERSION = '1.33';

=head1 SYNOPSIS
Expand Down Expand Up @@ -268,6 +268,67 @@ Benchmark and compare different pieces of code.

=head2 Format Conversions
=head3 csv
CSV parser.
Syntax:
csv ( ARRAYREF_OF_ARRAYREFS )
csv ( ARRAYREF, [ARRAYREF] )
csv ( STRING )
csv ( )
Convert Perl object to CSV string:
$ perl -Me -e 'say csv [ "A1", "B1", "C1" ], [ "A2", "B2", "C2" ]'
A1,B1,C1
A2,B2,C2
Convert CSV string to Perl object:
# Single row:
perl -Me -e 'p csv "A1,B1,C1"'
[
[0] [
[0] "A1",
[1] "B1",
[2] "C1",
],
]
# Multiple rows at once:
$ perl -Me -e 'p csv "A1,B1,C1\nA2,B2,C2"'
[
[0] [
[0] "A1",
[1] "B1",
[2] "C1",
],
[1] [
[0] "A2",
[1] "B2",
[2] "C2",
],
]
# Can use default variable:
$ perl -Me -e 'p csv for "A1,B1,C1", "A2,B2,C2"'
[
[0] [
[0] "A1",
[1] "B1",
[2] "C1",
],
]
[
[0] [
[0] "A2",
[1] "B2",
[2] "C2",
],
]
=head3 j
JSON Parser.
Expand Down Expand Up @@ -858,28 +919,69 @@ sub import {
# Format Conversions
######################################

# CSV.
csv => sub {
if ( !$imported{$caller}{"Text::CSV_XS"}++ ) {
require Text::CSV_XS;

# Avoid rebuilding this object.
$e::_csv = Text::CSV_XS->new(
{
binary => 1,
auto_diag => 1,
}
);
}

my @args = @_ ? @_ : ( $_ );
my ( $thing ) = @args;
return if !defined $thing;

# String to reference.
if ( !ref $thing ) {
open my $io, "<", \$thing;
return $e::_csv->getline_all( $io );
}

# Reference to string.
if ( ref( $thing ) ne "ARRAY" ) {
die "csv arguement is not an array reference!\n";
}

if ( ref( $thing->[0] ) ne "ARRAY" ) {
$thing = [@args];
}

join "\n",
map { $e::_csv->combine( @$_ ) && $e::_csv->string; } @$thing;

},

# Json.
j => sub {
if ( !$imported{$caller}{"Mojo::JSON"}++ ) {
require Mojo::JSON;
}
Mojo::JSON::j( @_ );
my @args = @_ ? @_ : ( $_ );
Mojo::JSON::j( @args );
},

# XML/HTML.
x => sub {
if ( !$imported{$caller}{"Mojo::DOM"}++ ) {
require Mojo::DOM;
}
Mojo::DOM->new( @_ );
my @args = @_ ? @_ : ( $_ );
Mojo::DOM->new( @args );
},

# YAML.
yml => sub {
if ( !$imported{$caller}{"YAML::XS"}++ ) {
require YAML::XS;
}
my ( $thing ) = @_;
my @args = @_ ? @_ : ( $_ );
my ( $thing ) = @args;
ref $thing
? YAML::XS::Dump( $thing )
: YAML::XS::Load( $thing );
Expand Down
80 changes: 71 additions & 9 deletions t/01-simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,74 @@ like run(
# Format Conversions
######################################

# CSV - ref to string.
is
csv( [ "A1", "B1", "C1" ] ),
qq(A1,B1,C1),
"csv - ref to csv string (single)";

is
csv( [ [ "A1", "B1", "C1" ] ] ),
qq(A1,B1,C1),
"csv - ref to csv string (single, aoa)";

is
csv( [ "A1", "B1", "C1" ], [ "A2", "B2", "C2" ] ),
qq(A1,B1,C1\nA2,B2,C2),
"csv - ref to csv string (multiple)";

is
csv( [ [ "A1", "B1", "C1" ], [ "A2", "B2", "C2" ] ] ),
qq(A1,B1,C1\nA2,B2,C2),
"csv - ref to csv string (multiple, aoa)";

is
csv( [ "A1", "B1", "C1" ], [ "A2", "B2", "C\n2" ] ),
qq(A1,B1,C1\nA2,B2,"C\n2"),
"csv - ref to csv string (newline)";

is
csv( [ "A1", "B1", "C1" ], [ "A2", "B,2", "C2" ] ),
qq(A1,B1,C1\nA2,"B,2",C2),
"csv - ref to csv string (separator)";

{
local $_ = [ "A1", "B1", "C1" ];
is
csv,
qq(A1,B1,C1),
"csv - ref to csv string (default var)";
}

# CSV - string to ref.
is_deeply
csv( qq(A1,B1,C1) ),
[ [ "A1", "B1", "C1" ] ],
"csv - csv string to ref (single)";

is_deeply
csv( qq(A1,B1,"C\n1") ),
[ [ "A1", "B1", "C\n1" ] ],
"csv - csv string to ref (single,newline)";

is_deeply
csv( qq(A1,B1,"C,1") ),
[ [ "A1", "B1", "C,1" ] ],
"csv - csv string to ref (single,separator)";

is_deeply
csv( qq(A1,B1,C1\nA2,B2,C2) ),
[ [ "A1", "B1", "C1" ], [ "A2", "B2", "C2" ] ],
"csv - csv string to ref (multiple)";

{
local $_ = "A1,B1,C1";
is_deeply
csv,
[ [ "A1", "B1", "C1" ] ],
"csv - csv string to ref (default var)";
}

# Json.
is
j( { a => 1 } ),
Expand Down Expand Up @@ -257,17 +325,11 @@ is
# List Support
######################################

is
max( 10, 20, 9, 15 ), 20,
"max - sanity check";
is max( 10, 20, 9, 15 ), 20, "max - sanity check";

is
min( 10, 20, 9, 15 ), 9,
"min - sanity check";
is min( 10, 20, 9, 15 ), 9, "min - sanity check";

is
sum( 1 .. 10 ), 55,
"sum - sanity check";
is sum( 1 .. 10 ), 55, "sum - sanity check";

is_deeply
[ uniq 2, 4, 4, 6 ],
Expand Down

0 comments on commit 9caa0af

Please sign in to comment.