blib/lib/Object/String.pm

filter: Statementstmt 100% 212 / 212 Branchbran 100% 38 / 38 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 77 / 77 Podpod 100% 67 / 67 totaltotal 100% 409 / 409 CC 23 SCAR 4.6
CC
23
Cov
100%
chomp_left
3.0
chomp_right
3.0
to_boolean
3.0
CRAP 1.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Object::String;
7 1 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.11'; # VERSION
11  
12 # ABSTRACT: A string object for Perl 5
13  
14 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19 1 has 'string' => ( is => 'ro' );
20  
21  
22 1 ▶sub to_string { shift->string; }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
23  
24  
25 82 ▶sub to_lower {
Subroutine to_lower: called CC=1 SCAR=0.0
Pod documented
26 82 my $self = shift;
27 82 $self->{string} = lc $self->{string};
28 82 return $self;
29 }
30  
31  
32 5 ▶sub to_lower_first {
Subroutine to_lower_first: called CC=1 SCAR=0.0
Pod documented
33 5 my $self = shift;
34 5 $self->{string} = lcfirst $self->{string};
35 5 return $self;
36 }
37  
38  
39 3 ▶sub to_upper {
Subroutine to_upper: called CC=1 SCAR=0.0
Pod documented
40 3 my $self = shift;
41 3 $self->{string} = uc $self->{string};
42 3 return $self;
43 }
44  
45  
46 28 ▶sub to_upper_first {
Subroutine to_upper_first: called CC=1 SCAR=0.0
Pod documented
47 28 my $self = shift;
48 28 $self->{string} = ucfirst $self->{string};
49 28 return $self;
50 }
51  
52  
53 26 ▶sub capitalize { shift->to_lower->to_upper_first; }
Subroutine capitalize: called CC=1 SCAR=0.0
Pod documented
54  
55  
56 25 ▶sub Object::String::length { return CORE::length shift->string; }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
57  
58  
59 2 ▶sub ensure_left {
Subroutine ensure_left: called CC=2 SCAR=6.9
Pod documented
60 2 my ($self, $prefix) = @_;
61 2 ▶ $self->{string} = $self->prefix($prefix)->string
Branch: 1 branch
BranchTrueFalseTotal
unless $self->starts_with($prefix) 1 1 2
62 unless($self->starts_with($prefix));
63 2 return $self;
64 }
65  
66  
67 2 ▶sub ensure_right {
Subroutine ensure_right: called CC=2 SCAR=6.9
Pod documented
68 2 my ($self, $suffix) = @_;
69 2 ▶ $self->{string} = $self->suffix($suffix)->string
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ends_with($suffix) 1 1 2
70 unless($self->ends_with($suffix));
71 2 return $self;
72 }
73  
74  
75 30 ▶sub trim_left {
Subroutine trim_left: called CC=1 SCAR=0.0
Pod documented
76 30 my $self = shift;
77 30 $self->{string} =~ s/^(\s|\t)+//;
78 30 return $self;
79 }
80  
81  
82 29 ▶sub trim_right {
Subroutine trim_right: called CC=1 SCAR=0.0
Pod documented
83 29 my $self = shift;
84 29 $self->{string} =~ s/(\s|\t)+$//;
85 29 return $self;
86 }
87  
88  
89 25 ▶sub trim { shift->trim_left->trim_right; }
Subroutine trim: called CC=1 SCAR=0.0
Pod documented
90  
91  
92 11 ▶sub clean {
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
93 11 my $self = shift;
94 11 $self->{string} =~ s/(\s|\t)+/ /g;
95 11 return $self->trim;
96 }
97  
98  
99 2 ▶sub collapse_whitespace { shift->clean; }
Subroutine collapse_whitespace: called CC=1 SCAR=0.0
Pod documented
100  
101  
102 2 ▶sub repeat {
Subroutine repeat: called CC=1 SCAR=0.0
Pod documented
103 2 my ($self, $n) = @_;
104 2 $self->{string} = $self->string x $n;
105 2 return $self;
106 }
107  
108  
109 1 ▶sub times { shift->repeat(@_); }
Subroutine times: called CC=1 SCAR=0.0
Pod documented
110  
111  
112 15 ▶sub starts_with {
Subroutine starts_with: called CC=1 SCAR=0.0
Pod documented
113 15 my ($self, $str) = @_;
114 15 return ($self->string =~ /^$str/);
115 }
116  
117  
118 9 ▶sub ends_with {
Subroutine ends_with: called CC=1 SCAR=0.0
Pod documented
119 9 my ($self, $str) = @_;
120 9 return ($self->string =~ /$str$/);
121 }
122  
123  
124 4 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
125 4 my ($self, $str) = @_;
126 4 return index $self->string, $str;
127 }
128  
129  
130 2 ▶sub include { shift->contains(@_); }
Subroutine include: called CC=1 SCAR=0.0
Pod documented
131  
132  
133 3 ▶sub chomp_left {
Subroutine chomp_left: called CC=3 SCAR=11.0
Pod documented
134 3 my $self = shift;
135 3 ▶ if($self->starts_with(" ") || $self->starts_with("\t")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->starts_with(" ") or $self->starts_with("\t")) 2 1 3
Condition100%3/3
$self->starts_with(" ") or $self->starts_with("\t")
l!l&&r!l&&!r
111
MC/DC100%2/2
$self->starts_with(" ") or $self->starts_with("\t")
$self->starts_with(" ") $self->starts_with("\t")
Truth table100%3/3
A or B
A= $self->starts_with(" ")B= $self->starts_with("\t")
ABresult
1X1
011
000
136 2 return $self->chop_left;
137 }
138 1 return $self;
139 }
140  
141  
142 3 ▶sub chomp_right {
Subroutine chomp_right: called CC=3 SCAR=11.0
Pod documented
143 3 my $self = shift;
144 3 ▶ if($self->ends_with(" ") || $self->ends_with("\t")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ends_with(" ") or $self->ends_with("\t")) 2 1 3
Condition100%3/3
$self->ends_with(" ") or $self->ends_with("\t")
l!l&&r!l&&!r
111
MC/DC100%2/2
$self->ends_with(" ") or $self->ends_with("\t")
$self->ends_with(" ") $self->ends_with("\t")
Truth table100%3/3
A or B
A= $self->ends_with(" ")B= $self->ends_with("\t")
ABresult
1X1
011
000
145 2 return $self->chop_right;
146 }
147 1 return $self;
148 }
149  
150  
151 4 ▶sub chop_left {
Subroutine chop_left: called CC=1 SCAR=0.0
Pod documented
152 4 my $self = shift;
153 4 $self->{string} = substr $self->{string}, 1, CORE::length $self->{string};
154 4 return $self;
155  
156 }
157  
158  
159 4 ▶sub chop_right {
Subroutine chop_right: called CC=1 SCAR=0.0
Pod documented
160 4 my $self = shift;
161 4 chop $self->{string};
162 4 return $self;
163 }
164  
165  
166 4 ▶sub is_numeric { shift->string =~ /^\d+$/; }
Subroutine is_numeric: called CC=1 SCAR=0.0
Pod documented
167  
168  
169 4 ▶sub is_alpha { shift->string =~ /^[a-zA-Z]+$/; }
Subroutine is_alpha: called CC=1 SCAR=0.0
Pod documented
170  
171  
172 4 ▶sub is_alpha_numeric { shift->string =~ /^[a-zA-Z0-9]+$/; }
Subroutine is_alpha_numeric: called CC=1 SCAR=0.0
Pod documented
173  
174  
175 4 ▶sub is_lower {
Subroutine is_lower: called CC=1 SCAR=0.0
Pod documented
176 4 my $self = shift;
177 4 return $self->string eq lc $self->string;
178 }
179  
180  
181 4 ▶sub is_upper {
Subroutine is_upper: called CC=1 SCAR=0.0
Pod documented
182 4 my $self = shift;
183 4 return $self->string eq uc $self->string;
184 }
185  
186  
187 26 ▶sub to_boolean {
Subroutine to_boolean: called CC=3 SCAR=11.0
Pod documented
188 26 my $self = shift;
189 26 ▶ return 1 if $self->string =~ /^(on|yes|true)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $self->string =~ /^(on|yes|true)$/i 10 16 26
190 16 ▶ return 0 if $self->string =~ /^(off|no|false)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $self->string =~ /^(off|no|false)$/i 10 6 16
191 6 return;
192 }
193  
194  
195 13 ▶sub to_bool { shift->to_boolean }
Subroutine to_bool: called CC=1 SCAR=0.0
Pod documented
196  
197  
198 6 ▶sub is_empty {
Subroutine is_empty: called CC=3 SCAR=11.0
Pod documented
199 6 my $self = shift;
200 6 ▶ return 1 if $self->string =~ /\s+/ || $self->string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->string =~ /\s+/ or $self->string eq "" 5 1 6
Condition100%3/3
$self->string =~ /\s+/ or $self->string eq ""
l!l&&r!l&&!r
321
MC/DC100%2/2
$self->string =~ /\s+/ or $self->string eq ""
$self->string =~ /\s+/ $self->string eq ""
Truth table100%3/3
A or B
A= $self->string =~ /\s+/B= $self->string eq ""
ABresult
1X1
011
000
201 1 return 0;
202 }
203  
204  
205 1 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
206 1 my ($self, $str) = @_;
207 1 return () = $self->string =~ /$str/g;
208 }
209  
210  
211 3 ▶sub left {
Subroutine left: called CC=2 SCAR=6.9
Pod documented
212 3 my ($self, $count) = @_;
213 3 ▶ if($count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 0) 1 2 3
214 1 $self->{string} = substr $self->string, $count, abs($count);
215 1 return $self;
216 }
217 2 $self->{string} = substr $self->string, 0, $count;
218 2 return $self;
219 }
220  
221  
222 3 ▶sub right {
Subroutine right: called CC=2 SCAR=6.9
Pod documented
223 3 my ($self, $count) = @_;
224 3 ▶ if($count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 0) 1 2 3
225 1 $self->{string} = substr $self->string, 0, abs($count);
226 1 return $self;
227 }
228 2 $self->{string} = substr $self->string, -$count, $count;
229 2 return $self;
230 }
231  
232  
233 51 ▶sub underscore {
Subroutine underscore: called CC=1 SCAR=0.0
Pod documented
234 51 my $self = shift;
235 51 $self->{string} = $self->transliterate(' -', '_')->string;
236 51 $self->{string} =~ s/::/\//g;
237 51 $self->{string} =~ s/^([A-Z])/_$1/;
238 51 $self->{string} =~ s/([A-Z]+)([A-Z][a-z])/$1_$2/g;
239 51 $self->{string} =~ s/([a-z\d])([A-Z])/$1_$2/g;
240 51 return $self->to_lower;
241 }
242  
243  
244 10 ▶sub underscored { shift->underscore; }
Subroutine underscored: called CC=1 SCAR=0.0
Pod documented
245  
246  
247 13 ▶sub dasherize { shift->underscore->transliterate('_', '-'); }
Subroutine dasherize: called CC=1 SCAR=0.0
Pod documented
248  
249  
250 6 ▶sub camelize {
Subroutine camelize: called CC=2 SCAR=6.9
Pod documented
251 6 my $self = shift;
252 6 my $begins_underscore = $self->underscore->starts_with('_');
253 6 $self->{string} = join '', map { ucfirst $_ } split /_/, $self->underscore->string;
254 6 $self->{string} = join '::', map { ucfirst $_ } split /\//, $self->string;
255 6 ▶ return ($begins_underscore ? $self : $self->to_lower_first);
Branch: 1 branch
BranchTrueFalseTotal
$begins_underscore ? : 3 3 6
256 }
257  
258  
259 4 ▶sub latinise {
Subroutine latinise: called CC=1 SCAR=0.0
Pod documented
260 4 my $self = shift;
261 4 $self->{string} = NFKD($self->string);
262 4 $self->{string} =~ s/\p{NonspacingMark}//g;
263 4 return $self;
264 }
265  
266  
267 2 ▶sub escape_html {
Subroutine escape_html: called CC=1 SCAR=0.0
Pod documented
268 2 return shift->replace_all('&', '&amp;')
269 ->replace_all('"', '&quot;')
270 ->replace_all("'", '&#39;')
271 ->replace_all('<', '&lt;')
272 ->replace_all('>', '&gt;');
273 }
274  
275  
276 2 ▶sub unescape_html {
Subroutine unescape_html: called CC=1 SCAR=0.0
Pod documented
277 2 return shift->replace_all('&amp;', '&')
278 ->replace_all('&quot;', '"')
279 ->replace_all('&#39;', "'")
280 ->replace_all('&lt;', '<')
281 ->replace_all('&gt;', '>');
282 }
283  
284  
285 2 ▶sub index_left {
Subroutine index_left: called CC=2 SCAR=6.9
Pod documented
286 2 my ($self, $substr, $position) = @_;
287 2 ▶ return index $self->string, $substr, $position if defined $position;
Branch: 1 branch
BranchTrueFalseTotal
if defined $position 1 1 2
288 1 return index $self->string, $substr;
289 }
290  
291  
292 2 ▶sub index_right {
Subroutine index_right: called CC=2 SCAR=6.9
Pod documented
293 2 my ($self, $substr, $position) = @_;
294 2 ▶ return rindex $self->string, $substr, $position if defined $position;
Branch: 1 branch
BranchTrueFalseTotal
if defined $position 1 1 2
295 1 return rindex $self->string, $substr;
296 }
297  
298  
299 28 ▶sub replace_all {
Subroutine replace_all: called CC=1 SCAR=0.0
Pod documented
300 28 my ($self, $substr1, $substr2) = @_;
301 28 $substr1 = quotemeta $substr1;
302 28 $self->{string} =~ s/$substr1/$substr2/g;
303 28 return $self;
304 }
305  
306  
307 6 ▶sub humanize {
Subroutine humanize: called CC=1 SCAR=0.0
Pod documented
308 return shift->underscore
309 6 ->replace_all('_', ' ')
310 ->trim
311 ->capitalize;
312 }
313  
314  
315 4 ▶sub pad_left {
Subroutine pad_left: called CC=3 SCAR=11.0
Pod documented
316 4 my ($self, $count, $char) = @_;
317 4 ▶ $char = ' ' unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 3 1 4
318 4 ▶ return $self if $count <= $self->length;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= $self->length 2 2 4
319 2 $self->{string} = $char x ($count - $self->length) . $self->string;
320 2 return $self;
321 }
322  
323  
324 4 ▶sub pad_right {
Subroutine pad_right: called CC=3 SCAR=11.0
Pod documented
325 4 my ($self, $count, $char) = @_;
326 4 ▶ $char = ' ' unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 3 1 4
327 4 ▶ return $self if $count <= $self->length;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= $self->length 2 2 4
328 2 $self->{string} = $self->string . $char x ($count - $self->length);
329 2 return $self;
330 }
331  
332  
333 5 ▶sub pad {
Subroutine pad: called CC=3 SCAR=11.0
Pod documented
334 5 my ($self, $count, $char) = @_;
335 5 ▶ $char = ' ' unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 4 1 5
336 5 ▶ return $self if $count <= $self->length;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= $self->length 2 3 5
337 3 my $count_left = 1 + int(($count - $self->length) / 2);
338 3 my $count_right = $count - $self->length - $count_left;
339 3 $self->{string} = $char x $count_left . $self->string;
340 3 $self->{string} = $self->string . $char x $count_right;
341 3 return $self;
342 }
343  
344  
345 2 ▶sub next {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
346 2 my $self = shift;
347 2 $self->{string}++;
348 2 return $self;
349 }
350  
351  
352 3 ▶sub slugify {
Subroutine slugify: called CC=1 SCAR=0.0
Pod documented
353 return shift->trim
354 ->humanize
355 ->latinise
356 ->strip_punctuation
357 ->to_lower
358 3 ->dasherize;
359 }
360  
361  
362 8 ▶sub strip_punctuation {
Subroutine strip_punctuation: called CC=1 SCAR=0.0
Pod documented
363 8 my $self = shift;
364 8 $self->{string} =~ s/[[:punct:]]//g;
365 8 return $self;
366 }
367  
368  
369 1 ▶sub swapcase { shift->transliterate('a-zA-Z', 'A-Za-z'); }
Subroutine swapcase: called CC=1 SCAR=0.0
Pod documented
370  
371  
372 5 ▶sub concat {
Subroutine concat: called CC=1 SCAR=0.0
Pod documented
373 5 my ($self, @strings) = @_;
374 5 $self->{string} = $self->string . join '', @strings;
375 5 return $self;
376 }
377  
378  
379 3 ▶sub suffix { shift->concat(@_); }
Subroutine suffix: called CC=1 SCAR=0.0
Pod documented
380  
381  
382 3 ▶sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
383 3 my ($self, @strings) = @_;
384 3 $self->{string} = join('', @strings) . $self->string;
385 3 return $self;
386 }
387  
388  
389 1 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
390 1 my $self = shift;
391 1 $self->{string} = join '', reverse split //, $self->string;
392 1 return $self;
393 }
394  
395  
396 3 ▶sub count_words {
Subroutine count_words: called CC=1 SCAR=0.0
Pod documented
397 3 my @arr = split /\s/, shift->clean->string;
398 3 return $#arr + 1;
399 }
400  
401  
402 1 ▶sub quote_meta {
Subroutine quote_meta: called CC=1 SCAR=0.0
Pod documented
403 1 my $self = shift;
404 1 $self->{string} = quotemeta $self->string;
405 1 return $self;
406 }
407  
408  
409 3 ▶sub rot13 { shift->transliterate('A-Za-z', 'N-ZA-Mn-za-m'); }
Subroutine rot13: called CC=1 SCAR=0.0
Pod documented
410  
411  
412 1 ▶sub say { CORE::say shift->string; }
Subroutine say: called CC=1 SCAR=0.0
Pod documented
413  
414  
415 4 ▶sub titleize {
Subroutine titleize: called CC=1 SCAR=0.0
Pod documented
416 4 my $self = shift;
417 4 $self->{string} = join ' ', map { str($_)->capitalize->string }
418 split / /,
419 $self->clean
420 ->strip_punctuation
421 ->string;
422 4 return $self;
423 }
424  
425  
426 2 ▶sub titlecase { shift->titleize }
Subroutine titlecase: called CC=1 SCAR=0.0
Pod documented
427  
428  
429 3 ▶sub squeeze {
Subroutine squeeze: called CC=2 SCAR=6.9
Pod documented
430 3 my ($self, $keep) = @_;
431 3 ▶ $keep = '' unless defined $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keep 1 2 3
432 3 $self->{string} =~ eval "\$self->{string} =~ tr/$keep//cs";
433 3 return $self;
434 }
435  
436  
437 1 ▶sub shuffle {
Subroutine shuffle: called CC=1 SCAR=0.0
Pod documented
438 1 my $self = shift;
439 1 $self->{string} = join '', List::Util::shuffle split //, $self->string;
440 1 return $self;
441 }
442  
443  
444 69 ▶sub transliterate {
Subroutine transliterate: called CC=1 SCAR=0.0
Pod documented
445 69 my ($self, $str1, $str2) = @_;
446 69 $self->{string} =~ eval "\$self->{string} =~ tr/$str1/$str2/";
447 69 return $self;
448 }
449  
450 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
451  
452 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
453  
454 1 our @EXPORT = qw {
455 str
456 };
457  
458  
459 228 ▶sub str {
Subroutine str: called CC=1 SCAR=0.0
Pod documented
460 228 my $string = join ' ', @_;
461 228 return Object::String->new(string => $string);
462 }
463  
464 1 1;
465  
466 __END__
↑ Summary