Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Jul 1, 2019
1 parent 547f520 commit 3421de5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 16 additions & 15 deletions lib/App/jl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sub new {
my $self = bless {
_opt => $opt,
_json => JSON->new->utf8->pretty(!$opt->{no_pretty})->canonical(1),
__current_orig_line => undef,
}, $class;

$self->_lazyload_modules;
Expand All @@ -56,44 +57,45 @@ sub run {

my $out = !!$self->opt('stderr') ? *STDERR : *STDOUT;

while (my $orig_line = <STDIN>) {
if (my $line = $self->_run_line($orig_line)) {
while ($self->{__current_orig_line} = <STDIN>) {
if (my $line = $self->_run_line) {
print $out $line;
}
$self->{__current_orig_line} = undef;
}
}

sub _run_line {
my ($self, $orig_line) = @_;
my ($self) = @_;

if ($orig_line =~ m!^[\s\t\r\n]+$!) {
if ($self->{__current_orig_line} =~ m!^[\s\t\r\n]+$!) {
return;
}

if ($orig_line !~ m!^\s*[\[\{]!) {
return $self->opt('sweep') ? undef : $orig_line;
if ($self->{__current_orig_line} !~ m!^\s*[\[\{]!) {
return $self->opt('sweep') ? undef : $self->{__current_orig_line};
}

if (my $rs = $self->opt('grep')) {
if (!$self->_match_grep($rs, $orig_line)) {
if (!$self->_match_grep($rs)) {
return; # no match
}
}

if (my $rs = $self->opt('ignore')) {
if ($self->_match_grep($rs, $orig_line)) {
if ($self->_match_grep($rs)) {
return; # ignore if even one match
}
}

return $self->_process($orig_line);
return $self->_process;
}

sub _match_grep {
my ($self, $rs, $orig_line) = @_;
my ($self, $rs) = @_;

for my $r (@{$rs}) {
return 1 if $orig_line =~ m!$r!;
return 1 if $self->{__current_orig_line} =~ m!$r!;
}
}

Expand All @@ -113,18 +115,17 @@ sub _lazyload_modules {
}

sub _process {
my ($self, $orig_line) = @_;
my ($self) = @_;

my $decoded = eval {
$self->{_json}->decode($orig_line);
$self->{_json}->decode($self->{__current_orig_line});
};
if ($@) {
return $orig_line;
return $self->{__current_orig_line};
}
else {
$self->_recursive_process($decoded);
return $self->_encode($decoded);

}
}

Expand Down
8 changes: 5 additions & 3 deletions t/01_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use App::jl;
sub jl_test {
my ($name, $src_json, $opt, $test_ref, $do_note) = @_;

my $ouput = App::jl->new($opt ? @{$opt} : ())->_run_line($src_json);
my $jl = App::jl->new($opt ? @{$opt} : ());
$jl->{__current_orig_line} = $src_json;
my $output = $jl->_run_line;

note $name if $do_note;
note $ouput if $do_note;
note $output if $do_note;

if (ref $test_ref eq 'CODE') {
$test_ref->($ouput, $src_json);
$test_ref->($output, $src_json);
}
}

Expand Down

0 comments on commit 3421de5

Please sign in to comment.