blib/lib/Database/BI/Util.pm

filter: Statementstmt 100% 47 / 47 Branchbran 90.0% 18 / 20 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 95.8% 93 / 97 CC 14 SCAR 13.3
CC
14
Cov
96%
parse_number
7.0
iso_datetime
4.0
urlencode
3.0
CRAP 3.8
« blib/lib/Database/BI/Extract.pm ↑ Summary blib/lib/Database/BI/Tutorial.pm »
1 package Database::BI::Util;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.023.1';
7  
8 6 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our @EXPORT_OK = qw( urlencode parse_number iso_datetime minute_stamp iso8601_utc );
13  
14 # A list rather than any three capitals, or 'SKU 100' would be read as 100.
15 6 Readonly my $CURRENCY_CODES => join '|', qw(AUD BRL CAD CHF CNY DKK EUR GBP HKD INR JPY MXN NOK NZD SEK SGD USD ZAR);
16 # The largest number a chart can use: a C double's, which is what the browser
17 # has (parseAccounting refuses what is not isFinite there). Not perl's own
18 # infinity: a perl built with -Duselongdouble or -Dusequadmath holds 1e999 as
19 # a finite number, so comparing with infinity let it through on those perls
20 # alone (GitHub issue 6).
21 6 Readonly my $LARGEST_NUMBER => POSIX::DBL_MAX();
22  
23 # A date, or a date and a time with T or a space between them; the time may
24 # carry seconds, a fraction of a second, and a zone (Z, or an offset such as
25 # +01:00, +0100 or +01). The last three are what JSON APIs and exports write.
26 6 Readonly my $ISO_DATETIME_RE => qr{
27 \A (\d{4}-\d{2}-\d{2})
28 (?: [T ] (\d{2}) : (\d{2}) (?: : (\d{2}) (?: [.,] \d+ )? )?
29 (?: [Zz] | [+-] \d{2} (?: :? \d{2} )? )? )?
30 \z
31 }x;
32  
33 # urlencode($str) -> $encoded
34 #
35 # Percent-encodes a string for use as a URI query-parameter value.
36 # Preserves only the RFC 3986 unreserved characters (letters, digits,
37 # hyphen, dot, underscore, tilde) and encodes everything else as %XX.
38 #
39 # Why a shared copy rather than Mojo::Util::url_escape:
40 # Ask.pm and Insights.pm are plain Perl modules with no Mojo dependency;
41 # pulling in Mojo::Util for a 3-line function would be an undue coupling.
42 #
43 # Multi-byte characters: a decoded string is encoded to its UTF-8 bytes first,
44 # because ord() returns the codepoint and U+263A would print as "%263A", which
45 # decodes to "&3A". A string of bytes (the usual CSV cell) is left as it is, so
46 # text that is already UTF-8 is not encoded twice.
47 233 ▶sub urlencode {
Subroutine urlencode: called CC=3 SCAR=11.0
Pod documented
48 233 my ($str) = @_;
49 233 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 231 233
50 231 ▶ utf8::encode($str) if utf8::is_utf8($str);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($str) 2 229 231
51 # '-' placed at end of character class to avoid ambiguity with a range.
52 231 $str =~ s/([^A-Za-z0-9._~-])/sprintf('%%%02X', ord($1))/ge;
53 231 return $str;
54 }
55  
56 # Only what dresses up a number is removed: spaces, brackets, thousands
57 # separators, a percent sign, currency symbols and a known currency code. Any
58 # other letter stays and fails the match, so 'SKU123' and 'AB-100' are text
59 # rather than 123 and -100. The bracket is read before it is removed.
60 # The browser's parseAccounting (layouts/parse_accounting.html.tt) is a copy,
61 # so a table sorts the same on the server as on the page: change one, change
62 # the other.
63 6256 ▶sub parse_number {
Subroutine parse_number: called CC=7 SCAR=19.5
Pod documented
64 6256 my $value = $_[0];
65  
66 6256 ▶ return undef unless defined($value) && length($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 16 6240 6256
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
6106240
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
67  
68 6240 my $is_accounting = ($value =~ /\A\s*\(/);
69 6240 (my $text = $value) =~ s/(?:\A|(?<=[\s(]))(?:$CURRENCY_CODES)(?=[\s\d.])|(?<=[\s\d])(?:$CURRENCY_CODES)(?=[\s)]|\z)//g;
70 6240 $text =~ s/[\s(),\$%]|[^\x00-\x7F]//g;
71 6240 ▶ $text = "-$text" if $is_accounting && $text =~ /\A\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $is_accounting and $text =~ /\A\d/ 18 6222 6240
Condition100%3/3
$is_accounting and $text =~ /\A\d/
!ll&&!rl&&r
6218418
MC/DC100%2/2
$is_accounting and $text =~ /\A\d/
$is_accounting $text =~ /\A\d/
Truth table100%3/3
A and B
A= $is_accountingB= $text =~ /\A\d/
ABresult
0X0
100
111
72 6240 ▶ return undef unless $text =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/ 1457 4783 6240
73  
74 4783 my $number = $text + 0;
75  
76 # 1e999 spells a number, but one past a double's range is infinity in the
77 # browser, which is no use to a total or an axis -- on every perl.
78 4783 ▶ return undef unless abs($number) <= $LARGEST_NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
unless abs $number <= $LARGEST_NUMBER 6 4777 4783
79 4777 return $number;
80 }
81  
82 # iso_datetime($value) -> ($day, $hour, $minute, $second) | ()
83 #
84 # The one reading of an ISO date or timestamp, for the filters, the time range
85 # and the formulas alike. A time is taken as written: a fraction of a second
86 # and a zone are accepted and set aside, so 2026-11-06T23:30:00Z is 23:30 on
87 # the 6th, the day the table shows, for every reader.
88 2499 ▶sub iso_datetime {
Subroutine iso_datetime: called CC=4 SCAR=13.9
Pod documented
89 2499 my ($value) = @_;
90 2499 ▶ return () unless defined $value && !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 1 2498 2499
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
102498
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
91 2498 ▶ my ($day, $hour, $minute, $second) = $value =~ $ISO_DATETIME_RE or return ();
Branch: 1 branch
BranchTrueFalseTotal
() = $value =~ /$ISO_DATETIME_RE/ or return () 1194 1304 2498
92 1194 return ($day, $hour, $minute, $second);
93 }
94  
95 # minute_stamp(@tm) -> 'YYYY-MM-DD HH:MM'
96 # iso8601_utc($epoch) -> 'YYYY-MM-DDTHH:MM:SSZ'
97 #
98 # Written with sprintf rather than POSIX::strftime: on Windows, perl 5.38's
99 # strftime did not return (CI sat in t/function.t until the job's limit),
100 # and these fixed formats need nothing from the C library.
101 202 ▶sub minute_stamp {
Subroutine minute_stamp: called CC=2 SCAR=7.1
Pod documented
102 202 my @tm = @_;
103 202 ▶ return '' unless @tm >= 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @tm >= 6 0 202 202
104 202 return sprintf '%04d-%02d-%02d %02d:%02d', $tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2], $tm[1];
105 }
106  
107 4 ▶sub iso8601_utc {
Subroutine iso8601_utc: called CC=2 SCAR=7.1
Pod documented
108 4 my ($epoch) = @_;
109 4 ▶ my @tm = gmtime(defined $epoch ? $epoch : time);
Branch: 1 branch
BranchTrueFalseTotal
defined $epoch ? : 0 4 4
110 4 return sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', $tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2], $tm[1], $tm[0];
111 }
112  
113 6 1;
114  
115 __END__
« blib/lib/Database/BI/Extract.pm ↑ Summary blib/lib/Database/BI/Tutorial.pm »