Map/Tube/Utils.pm

filter: Statementstmt 46.0% 46 / 100 Branchbran 13.6% 3 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 54.5% 12 / 22 Podpod 0.0% 0 / 7 totaltotal 36.7% 61 / 166 CC 20 SCAR 30.5
CC
20
Cov
37%
filter
72.0
is_same
30.0
is_valid_color
20.0
CRAP 21.2
« Map/Tube/Route.pm ↑ Summary Map/Tube/Line.pm »
1 package Map::Tube::Utils;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = qv('v5.2.0');
8 26 our $AUTHORITY = 'cpan:MANWAR';
9  
10 =head1 NAME
11  
12 Map::Tube::Utils - Helper package for Map::Tube.
13  
14 =head1 VERSION
15  
16 Version v5.2.0
17  
18 =cut
19  
20 26 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 26 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 26 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 26 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 26 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 26 ▶use File::ShareDir ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 26 ▶use Unicode::Normalize 'NFC';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 26 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 26 our $COLOR_NAMES = _color_names();
31 26 our @EXPORT_OK = qw/trim filter to_perl is_same common_lines get_method_map is_valid_color/;
32  
33 =head1 DESCRIPTION
34  
35 B<FOR INTERNAL USE ONLY>.
36  
37 =cut
38  
39 0 ▶sub to_perl {
Subroutine to_perl: not called CC=2 SCAR=17.9
Pod undocumented
40 0 my ($file) = @_;
41  
42 0 my $json_text = do {
43 0 ▶ open(my $json_fh, "<", $file) or die("ERROR: Can't open $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $json_fh, "<", $file or die "ERROR: Can't open ${file}: $!\n" 0 0 0
44 0 local $/;
45 0 my $text = <$json_fh>;
46 0 close($json_fh);
47 0 $text;
48 };
49  
50 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 return JSON::MaybeXS->new->allow_nonref->utf8(1)->decode($json_text);
52 }
53 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 die $@;
55 0 };
56 }
57  
58 0 ▶sub trim {
Subroutine trim: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my ($data) = @_;
60  
61 0 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
62  
63 0 $data =~ s/\s+/ /g;
64 0 $data =~ s/^\s+|\s+$//g;
65  
66 0 return $data;
67 }
68  
69 0 ▶sub is_same {
Subroutine is_same: not called CC=5 SCAR=34.0
Pod undocumented
70 0 my ($this, $that) = @_;
71  
72 0 ▶ return 0 unless (defined($this) && defined($that));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this and defined $that 0 0 0
Condition0%0/3
defined $this and defined $that
!ll&&!rl&&r
000
MC/DC0%0/2
defined $this and defined $that
defined $this defined $that
Truth table0%0/3
A and B
A= defined $thisB= defined $that
ABresult
0X0
100
111
73  
74 0 ▶ (_is_number($this) && _is_number($that))
Branch: 1 branch
BranchTrueFalseTotal
_is_number($this) && _is_number($that) ? : 0 0 0
Condition0%0/3
_is_number($this) and _is_number($that)
!ll&&!rl&&r
000
MC/DC0%0/2
_is_number($this) and _is_number($that)
_is_number($this) _is_number($that)
Truth table0%0/3
A and B
A= _is_number($this)B= _is_number($that)
ABresult
0X0
100
111
75 ?
76 (return ($this == $that))
77 :
78 (uc($this) eq uc($that));
79 }
80  
81 0 ▶sub common_lines {
Subroutine common_lines: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ($a, $b) = @_;
83  
84 0 my %element = map { $_ => undef } @{$a};
85 0 return grep { exists($element{$_}) } @{$b};
86 }
87  
88 0 ▶sub filter {
Subroutine filter: not called CC=8 SCAR=42.8
Pod undocumented
89 0 my ($data) = @_;
90  
91 0 my %c;
92 0 for my $i (0 .. $#$data) {
93 0 for my $m (@{ $data->[$i] }) {
94 0 undef $c{$m}{$i};
95 }
96 }
97  
98 0 my @common = sort { $a cmp $b }
99 0 grep @$data == keys %{ $c{$_} },
100 keys %c;
101  
102 0 ▶ return [ map [@common], @$data ] if @common;
Branch: 1 branch
BranchTrueFalseTotal
if @common 0 0 0
103  
104 0 my %r;
105 0 for my $i (0 .. $#$data - 1) {
106 0 for my $m (@{ $data->[$i] }) {
107 0 ▶ if (exists $c{$m}{ $i + 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c{$m}{$i + 1}) 0 0 0
108 0 undef $r{$_}{$m} for $i, $i + 1;
109 }
110 }
111 }
112  
113 return [
114 0 map [ sort keys %{ $r{$_} } ],
115 0 sort { $a <=> $b } keys %r
116 ];
117 }
118  
119 0 ▶sub get_method_map {
Subroutine get_method_map: not called CC=1 SCAR=6.9
Pod undocumented
120  
121 return {
122 0 fuzzy_find => {
123 module => 'Map::Tube::Plugin::FuzzyFind',
124 exception => 'Map::Tube::Exception::MissingPluginFuzzyFind',
125 },
126 as_image => {
127 module => 'Map::Tube::Plugin::Graph',
128 exception => 'Map::Tube::Exception::MissingPluginGraph',
129 },
130 to_xml => {
131 module => 'Map::Tube::Plugin::Formatter',
132 exception => 'Map::Tube::Exception::MissingPluginFormatter',
133 },
134 to_json => {
135 module => 'Map::Tube::Plugin::Formatter',
136 exception => 'Map::Tube::Exception::MissingPluginFormatter',
137 },
138 to_yaml => {
139 module => 'Map::Tube::Plugin::Formatter',
140 exception => 'Map::Tube::Exception::MissingPluginFormatter',
141 },
142 to_string => {
143 module => 'Map::Tube::Plugin::Formatter',
144 exception => 'Map::Tube::Exception::MissingPluginFormatter',
145 },
146 };
147 }
148  
149 0 ▶sub is_valid_color {
Subroutine is_valid_color: not called CC=4 SCAR=30.0
Pod undocumented
150 0 my ($color) = @_;
151  
152 0 ▶ return 0 unless defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 0 0
153  
154 0 ▶ return $color if ($color =~ /^#[a-f0-9]{6}$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^#[a-f0-9]{6}$/iu 0 0 0
155  
156 0 my $hexcode = $COLOR_NAMES->{lc($color)};
157 0 ▶ if (defined $hexcode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hexcode) { } 0 0 0
158 0 return $hexcode;
159 }
160 else {
161 0 return 0;
162 }
163 }
164  
165 #
166 #
167 # PRIVATE METHODS
168  
169 0 ▶sub _is_number {
Subroutine _is_number: not called CC=2 SCAR=17.9
170 0 my ($this) = @_;
171  
172 0 ▶ return (defined($this)
Condition0%0/3
defined $this && $this =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $this && $this =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/u
defined $this $this =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/u
Truth table0%0/3
A && B
A= defined $thisB= $this =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/u
ABresult
0X0
100
111
173 && ($this =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/));
174 }
175  
176 26 ▶sub _color_names {
Subroutine _color_names: called CC=3 SCAR=11.0
177  
178 26 my $source = dist_file('Map-Tube', 'color-names.txt');
179 26 ▶ open (my $SOURCE_COLOR, "<", $source)
Branch: 1 branch
BranchTrueFalseTotal
open my $SOURCE_COLOR, "<", $source or die "ERROR: Can't open ${source}: $!\n" 26 0 26
180 or die("ERROR: Can't open $source: $!\n");
181  
182 26 my $color_names = {};
183 26 ▶ while (my $line = <$SOURCE_COLOR>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $SOURCE_COLOR)) 3822 26 3848
184 3822 chomp $line;
185 3822 my ($name, $hashcode) = split /\,/,$line,2;
186 3822 $color_names->{lc($name)} = $hashcode;
187 }
188 26 close ($SOURCE_COLOR);
189  
190 26 return $color_names;
191 }
192  
193 =head1 AUTHOR
194  
195 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
196  
197 =head1 REPOSITORY
198  
199 L<https://github.com/manwar/Map-Tube>
200  
201 =head1 BUGS
202  
203 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube/issues>.
204 I will be notified, and then you'll automatically be notified of progress on your
205 bug as I make changes.
206  
207 =head1 SUPPORT
208  
209 You can find documentation for this module with the perldoc command.
210  
211 perldoc Map::Tube::Utils
212  
213 You can also look for information at:
214  
215 =over 4
216  
217 =item * BUG Report
218  
219 L<https://github.com/manwar/Map-Tube/issues>
220  
221 =item * Search MetaCPAN
222  
223 L<https://metacpan.org/dist/Map-Tube/>
224  
225 =back
226  
227 =head1 LICENSE AND COPYRIGHT
228  
229 Copyright (C) 2010 - 2026 Mohammad Sajid Anwar.
230  
231 This program is free software; you can redistribute it and / or modify it under
232 the terms of the the Artistic License (2.0). You may obtain a copy of the full
233 license at:
234  
235 L<http://www.perlfoundation.org/artistic_license_2_0>
236  
237 Any use, modification, and distribution of the Standard or Modified Versions is
238 governed by this Artistic License.By using, modifying or distributing the Package,
239 you accept this license. Do not use, modify, or distribute the Package, if you do
240 not accept this license.
241  
242 If your Modified Version has been derived from a Modified Version made by someone
243 other than you,you are nevertheless required to ensure that your Modified Version
244 complies with the requirements of this license.
245  
246 This license does not grant you the right to use any trademark, service mark,
247 tradename, or logo of the Copyright Holder.
248  
249 This license includes the non-exclusive, worldwide, free-of-charge patent license
250 to make, have made, use, offer to sell, sell, import and otherwise transfer the
251 Package with respect to any patent claims licensable by the Copyright Holder that
252 are necessarily infringed by the Package. If you institute patent litigation
253 (including a cross-claim or counterclaim) against any party alleging that the
254 Package constitutes direct or contributory patent infringement,then this Artistic
255 License to you shall terminate on the date that such litigation is filed.
256  
257 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
258 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
259 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
260 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
261 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
262 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
263 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264  
265 =cut
266  
267 26 1; # End of Map::Tube::Utils
« Map/Tube/Route.pm ↑ Summary Map/Tube/Line.pm »