Data/Sah/Compiler/js/TH/str.pm

filter: Statementstmt 19.1% 19 / 99 Branchbran 0.0% 0 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 9 totaltotal 12.9% 24 / 186 CC 33 SCAR 49.0
CC
33
Cov
12%
superclause_has_elems
306.0
superclause_sortable
90.0
clause_match
20.0
CRAP 134.5
« Data/Sah/Compiler/js/TH/array.pm ↑ Summary Data/Sah/Compiler/js/TH/bool.pm »
1 package Data::Sah::Compiler::js::TH::str;
2  
3 3 our $DATE = '2016-09-14'; # DATE
4 3 our $VERSION = '0.87'; # VERSION
5  
6 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Log::Any '$log';
10  
11 3 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 extends 'Data::Sah::Compiler::js::TH';
15 3 with 'Data::Sah::Type::str';
16  
17 0 ▶sub handle_type {
Subroutine handle_type: not called CC=1 SCAR=6.9
Pod undocumented
18 0 my ($self, $cd) = @_;
19 0 my $c = $self->compiler;
20  
21 0 my $dt = $cd->{data_term};
22 0 $cd->{_ccl_check_type} = "typeof($dt)=='string' || typeof($dt)=='number'";
23 }
24  
25 0 ▶sub before_all_clauses {
Subroutine before_all_clauses: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my ($self, $cd) = @_;
27 0 my $c = $self->compiler;
28 0 my $dt = $cd->{data_term};
29  
30 # XXX only do this when there are clauses
31  
32 # convert number to string
33 0 $self->set_tmp_data_term($cd, "typeof($dt)=='number' ? ''+$dt : $dt");
34 }
35  
36 0 ▶sub after_all_clauses {
Subroutine after_all_clauses: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($self, $cd) = @_;
38 0 my $c = $self->compiler;
39 0 my $dt = $cd->{data_term};
40  
41 0 $self->restore_data_term($cd);
42 }
43  
44 0 ▶sub superclause_comparable {
Subroutine superclause_comparable: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my ($self, $which, $cd) = @_;
46 0 my $c = $self->compiler;
47 0 my $ct = $cd->{cl_term};
48 0 my $dt = $cd->{data_term};
49  
50 0 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 0 0 0
elsif ($which eq 'in') { } 0 0 0
51 0 $c->add_ccl($cd, "$dt == $ct");
52 } elsif ($which eq 'in') {
53 0 $c->add_ccl($cd, "($ct).indexOf($dt) > -1");
54 }
55 }
56  
57 0 ▶sub superclause_sortable {
Subroutine superclause_sortable: not called CC=9 SCAR=45.0
Pod undocumented
58 0 my ($self, $which, $cd) = @_;
59 0 my $c = $self->compiler;
60 0 my $cv = $cd->{cl_value};
61 0 my $ct = $cd->{cl_term};
62 0 my $dt = $cd->{data_term};
63  
64 0 ▶ if ($which eq 'min') {
Branch: 6 branches
BranchTrueFalseTotal
if ($which eq 'min') { } 0 0 0
elsif ($which eq 'xmin') { } 0 0 0
elsif ($which eq 'max') { } 0 0 0
elsif ($which eq 'xmax') { } 0 0 0
elsif ($which eq 'between') { } 0 0 0
elsif ($which eq 'xbetween') { } 0 0 0
65 0 $c->add_ccl($cd, "$dt >= $ct");
66 } elsif ($which eq 'xmin') {
67 0 $c->add_ccl($cd, "$dt > $ct");
68 } elsif ($which eq 'max') {
69 0 $c->add_ccl($cd, "$dt <= $ct");
70 } elsif ($which eq 'xmax') {
71 0 $c->add_ccl($cd, "$dt < $ct");
72 } elsif ($which eq 'between') {
73 0 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 0 0
74 0 $c->add_ccl($cd, "$dt >= ($ct)[0] && $dt <= ($ct)[1]");
75 } else {
76 # simplify code
77 0 $c->add_ccl($cd, "$dt >= ".$c->literal($cv->[0]).
78 " && $dt <= ".$c->literal($cv->[1]));
79 }
80 } elsif ($which eq 'xbetween') {
81 0 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 0 0
82 0 $c->add_ccl($cd, "$dt > ($ct)[0] && $dt < ($ct)[1]");
83 } else {
84 # simplify code
85 0 $c->add_ccl($cd, "$dt > ".$c->literal($cv->[0]).
86 " && $dt < ".$c->literal($cv->[1]));
87 }
88 }
89 }
90  
91 0 ▶sub superclause_has_elems {
Subroutine superclause_has_elems: not called CC=17 SCAR=57.2
Pod undocumented
92 0 my ($self_th, $which, $cd) = @_;
93 0 my $c = $self_th->compiler;
94 0 my $cv = $cd->{cl_value};
95 0 my $ct = $cd->{cl_term};
96 0 my $dt = $cd->{data_term};
97  
98 0 ▶ if ($which eq 'len') {
Branch: 11 branches
BranchTrueFalseTotal
if ($which eq 'len') { } 0 0 0
elsif ($which eq 'min_len') { } 0 0 0
elsif ($which eq 'max_len') { } 0 0 0
elsif ($which eq 'len_between') { } 0 0 0
elsif ($which eq 'has') { } 0 0 0
elsif ($which eq 'each_index') { } 0 0 0
elsif ($which eq 'each_elem') { } 0 0 0
elsif ($which eq 'check_each_index') { } 0 0 0
elsif ($which eq 'check_each_elem') { } 0 0 0
elsif ($which eq 'uniq') { } 0 0 0
elsif ($which eq 'exists') { } 0 0 0
99 0 $c->add_ccl($cd, "($dt).length == $ct");
100 } elsif ($which eq 'min_len') {
101 0 $c->add_ccl($cd, "($dt).length >= $ct");
102 } elsif ($which eq 'max_len') {
103 0 $c->add_ccl($cd, "($dt).length <= $ct");
104 } elsif ($which eq 'len_between') {
105 0 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 0 0
106 0 $c->add_ccl(
107 $cd, "($dt).length >= ($ct)[0] && ".
108 "($dt).length >= ($ct)[1]");
109 } else {
110 # simplify code
111 0 $c->add_ccl(
112 $cd, "($dt).length >= $cv->[0] && ".
113 "($dt).length <= $cv->[1]");
114 }
115 } elsif ($which eq 'has') {
116 0 $c->add_ccl($cd, "($dt).indexOf($ct) > -1");
117 } elsif ($which eq 'each_index') {
118 0 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 0 0
119 0 $self_th->gen_each($cd, $c->expr_array_0_nmin1("($cd->{data_term}).length"), '_x', '_x');
120 0 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 0 0
121 } elsif ($which eq 'each_elem') {
122 0 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 0 0
123 0 $self_th->gen_each($cd, $c->expr_array_0_nmin1("($cd->{data_term}).length"), '_x', "$cd->{data_term}.charAt(_x)");
124 0 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 0 0
125 } elsif ($which eq 'check_each_index') {
126 0 $self_th->compiler->_die_unimplemented_clause($cd);
127 } elsif ($which eq 'check_each_elem') {
128 0 $self_th->compiler->_die_unimplemented_clause($cd);
129 } elsif ($which eq 'uniq') {
130 0 $self_th->compiler->_die_unimplemented_clause($cd);
131 } elsif ($which eq 'exists') {
132 0 $self_th->compiler->_die_unimplemented_clause($cd);
133 }
134 }
135  
136 0 ▶sub clause_encoding {
Subroutine clause_encoding: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my ($self, $cd) = @_;
138 0 my $c = $self->compiler;
139 0 my $cv = $cd->{cl_value};
140 0 my $ct = $cd->{cl_term};
141 0 my $dt = $cd->{data_term};
142  
143 0 ▶ $c->_die($cd, "Only 'utf8' encoding is currently supported")
Branch: 1 branch
BranchTrueFalseTotal
unless $cv eq "utf8" 0 0 0
144 unless $cv eq 'utf8';
145 # currently does nothing
146 }
147  
148 0 ▶sub clause_match {
Subroutine clause_match: not called CC=4 SCAR=30.0
Pod undocumented
149 0 my ($self, $cd) = @_;
150 0 my $c = $self->compiler;
151 0 my $cv = $cd->{cl_value};
152 0 my $ct = $cd->{cl_term};
153 0 my $dt = $cd->{data_term};
154  
155 0 my $re;
156 0 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 0 0
157 0 $re = $ct;
158 } else {
159 0 $re = $c->_str2reliteral($cd, $cv);
160 }
161  
162 $c->add_ccl($cd, join(
163 "",
164 "(function(){ ",
165 "var _sahv_match = true; ",
166 "try { _sahv_match = ($dt).match(RegExp($re)) } catch(e) { if (e.name=='SyntaxError') _sahv_match = false } ",
167 ($cd->{cl_is_expr} ?
168 0 ▶ "return _sahv_match == !!($ct);" :
Branch: 2 branches
BranchTrueFalseTotal
$cd->{'cl_is_expr'} ? : 0 0 0
$cv ? : 0 0 0
169 "return ".($cv ? '':'!')."!!_sahv_match;"),
170 "} )()",
171 ));
172 }
173  
174 0 ▶sub clause_is_re {
Subroutine clause_is_re: not called CC=3 SCAR=24.8
Pod undocumented
175 0 my ($self, $cd) = @_;
176 0 my $c = $self->compiler;
177 0 my $cv = $cd->{cl_value};
178 0 my $ct = $cd->{cl_term};
179 0 my $dt = $cd->{data_term};
180  
181 $c->add_ccl($cd, join(
182 "",
183 "(function(){ var _sahv_is_re = true; ",
184 "try { RegExp($dt) } catch(e) { if (e.name=='SyntaxError') _sahv_is_re = false } ",
185 ($cd->{cl_is_expr} ?
186 0 ▶ "return _sahv_is_re == !!($ct);" :
Branch: 2 branches
BranchTrueFalseTotal
$cd->{'cl_is_expr'} ? : 0 0 0
$cv ? : 0 0 0
187 "return ".($cv ? '':'!')."_sahv_is_re;"),
188 "} )()",
189 ));
190 }
191  
192 3 1;
193 # ABSTRACT: js's type handler for type "str"
194  
195 __END__
« Data/Sah/Compiler/js/TH/array.pm ↑ Summary Data/Sah/Compiler/js/TH/bool.pm »