GoSaveTool/Text.pm

filter: Statementstmt 97.7% 86 / 88 Branchbran 100% 14 / 14 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 90.0% 18 / 20 Podpod 100% 12 / 12 totaltotal 97.3% 144 / 148 CC 12 SCAR 9.4
CC
12
Cov
98%
limits
5.0
title
5.0
stats
2.0
CRAP 2.5
« GoSaveTool/Subtitles.pm ↑ Summary GoSaveTool.pm »
1 package GoSaveTool::Text;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature qw(unicode_strings);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Unicode::Normalize qw(NFKD);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.000';
12 1 our @EXPORT_OK = qw(stats limits words upper lower title sentence slug camel snake kebab clean %LIMITS @LIMIT_ORDER);
13  
14 # Character limits of common fields; SEO values are the length search results show before cutting
15 1 our @LIMIT_ORDER = qw(x-post instagram-caption tiktok-caption youtube-title youtube-description linkedin-post pinterest-description seo-title meta-description);
16 1 our %LIMITS = (
17 'x-post' => 280,
18 'instagram-caption' => 2200,
19 'tiktok-caption' => 4000,
20 'youtube-title' => 100,
21 'youtube-description' => 5000,
22 'linkedin-post' => 3000,
23 'pinterest-description' => 500,
24 'seo-title' => 60,
25 'meta-description' => 160,
26 );
27  
28 1 my %SMALL = map { $_ => 1 } qw(a an and as at but by for in nor of on or so the to up yet via vs);
29  
30 4 ▶sub stats {
Subroutine stats: called CC=2 SCAR=6.9
Pod documented
31 4 my ($s) = @_;
32 4 my $words = () = $s =~ /[\p{L}\p{N}][\p{L}\p{N}'\x{2019}_-]*/g;
33 4 (my $no_spaces = $s) =~ s/\s//g;
34 4 my $sentences = () = $s =~ /[^.!?\x{2026}]+[.!?\x{2026}]+/g;
35 return {
36 4 ▶ words => $words,
Branch: 1 branch
BranchTrueFalseTotal
$s eq '' ? : 1 3 4
37 chars => length $s,
38 charsNoSpaces => length $no_spaces,
39 lines => $s eq '' ? 0 : scalar(() = split /\r\n|\r|\n/, $s, -1),
40 sentences => $sentences,
41 # 238 words a minute: the average silent reading speed for English non-fiction
42 readSeconds => int($words / 238 * 60 + 0.5),
43 };
44 }
45  
46 2 ▶sub limits {
Subroutine limits: called CC=5 SCAR=16.1
Pod documented
47 2 my ($s, @only) = @_;
48 2 my $used = length $s;
49 2 my %want = map { $_ => 1 } @only;
50 2 my %out;
51 2 for my $k (@LIMIT_ORDER) {
52 18 ▶ next if @only && !$want{$k};
Branch: 1 branch
BranchTrueFalseTotal
if @only and not $want{$k} 8 10 18
Condition100%3/3
@only and not $want{$k}
!ll&&!rl&&r
918
MC/DC100%2/2
@only and not $want{$k}
@only not $want{$k}
Truth table100%3/3
A and not(B)
A= @onlyB= not $want{$k}
ABresult
0X0
100
111
53 10 ▶ $out{$k} = { limit => $LIMITS{$k}, used => $used, left => $LIMITS{$k} - $used, fits => ($used <= $LIMITS{$k} ? 1 : 0) };
Branch: 1 branch
BranchTrueFalseTotal
$used <= $GoSaveTool::Text::LIMITS{$k} ? : 9 1 10
54 }
55 2 return \%out;
56 }
57  
58 4 ▶sub words {
Subroutine words: called CC=1 SCAR=0.0
Pod documented
59 4 my ($s) = @_;
60 4 $s =~ s/([\p{Ll}\p{N}])(\p{Lu})/$1 $2/g;
61 4 return grep { length } split /[^\p{L}\p{N}]+/, $s;
62 }
63  
64 0 ▶sub upper { return uc $_[0] }
Subroutine upper: not called CC=1 SCAR=6.9
Pod documented
65 0 ▶sub lower { return lc $_[0] }
Subroutine lower: not called CC=1 SCAR=6.9
Pod documented
66  
67 7 ▶sub _capitalize {
Subroutine _capitalize: called CC=1 SCAR=0.0
68 7 my ($w) = @_;
69 7 return uc(substr $w, 0, 1) . substr($w, 1);
70 }
71  
72 1 ▶sub title {
Subroutine title: called CC=5 SCAR=16.1
Pod documented
73 1 my ($s) = @_;
74 1 my $low = lc $s;
75 1 my ($out, $last, $first) = ('', 0, 1);
76 1 ▶ while ($low =~ /([\p{L}\p{N}][\p{L}\p{N}'\x{2019}]*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low =~ /([\p{L}\p{N}][\p{L}\p{N}'\x{2019}]*)/gu) 9 1 10
77 9 my ($w, $start, $end) = ($1, $-[0], $+[0]);
78 9 my $after_colon = substr($low, 0, $start) =~ /[:.!?]\s*\z/;
79 9 ▶ $out .= substr($low, $last, $start - $last) . ($first || $after_colon || !$SMALL{$w} ? _capitalize($w) : $w);
Branch: 1 branch
BranchTrueFalseTotal
$first || $after_colon || !$SMALL{$w} ? : 5 4 9
Condition100%6/6
$first or $after_colon or not $SMALL{$w}
l!l&&r!l&&!r
234
$first or $after_colon
l!l&&r!l&&!r
117
MC/DC100%3/3
$first or $after_colon or not $SMALL{$w}
$first $after_colon not $SMALL{$w}
Truth table100%4/4
A or B or not(C)
A= $firstB= $after_colonC= not $SMALL{$w}
ABCresult
1XX1
01X1
0011
0000
80 9 ($last, $first) = ($end, 0);
81 }
82 1 return $out . substr($low, $last);
83 }
84  
85 1 ▶sub sentence {
Subroutine sentence: called CC=1 SCAR=0.0
Pod documented
86 1 my ($s) = @_;
87 1 (my $low = lc $s) =~ s/(\A\s*|[.!?\x{2026}]\s+)(\p{L})/$1 . uc $2/ge;
88 1 return $low;
89 }
90  
91 1 my %SLUG = (qw(ı i İ i ş s Ş s ğ g Ğ g ç c Ç c ö o Ö o ü u Ü u ß ss æ ae ø o đ d ł l));
92  
93 2 ▶sub slug {
Subroutine slug: called CC=1 SCAR=0.0
Pod documented
94 2 my ($s) = @_;
95 2 $s =~ s/([ıİşŞğĞçÇöÖüÜßæøđł])/$SLUG{$1}/g;
96 2 my $plain = NFKD($s);
97 2 $plain =~ s/[\x{300}-\x{36f}]//g;
98 2 $plain = lc $plain;
99 2 $plain =~ s/[^a-z0-9]+/-/g;
100 2 $plain =~ s/^-+|-+$//g;
101 2 return $plain;
102 }
103  
104 1 ▶sub camel {
Subroutine camel: called CC=2 SCAR=6.9
Pod documented
105 1 my @w = words($_[0]);
106 1 ▶ return join '', map { $_ == 0 ? lc $w[$_] : _capitalize(lc $w[$_]) } 0 .. $#w;
Branch: 1 branch
BranchTrueFalseTotal
$_ == 0 ? : 1 2 3
107 }
108  
109 1 ▶sub snake { return join '_', map { lc } words($_[0]) }
Subroutine snake: called CC=1 SCAR=0.0
Pod documented
110 1 ▶sub kebab { return join '-', map { lc } words($_[0]) }
Subroutine kebab: called CC=1 SCAR=0.0
Pod documented
111  
112 2 ▶sub clean {
Subroutine clean: called CC=2 SCAR=6.9
Pod documented
113 2 my ($s) = @_;
114 2 ▶ my $nl = index($s, "\r\n") >= 0 ? "\r\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
index($s, "\r\n") != -1 ? : 1 1 2
115 2 my @lines = map { (my $l = $_) =~ s/[ \t]+\z//; $l =~ s/(\S)[ \t]{2,}/$1 /g; $l } split /\r\n|\n/, $s, -1;
116 2 my $out = join $nl, @lines;
117 2 my $q = quotemeta $nl;
118 2 $out =~ s/(?:$q){3,}/$nl$nl/g;
119 2 return $out;
120 }
121  
122 1 1;
123  
124 __END__
« GoSaveTool/Subtitles.pm ↑ Summary GoSaveTool.pm »