| 1 | package Term::Fabulous::Widget::Table::Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.32; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(is_blank number_of date_epoch date_interval compare_values natural_compare); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Time::Local 1.30 qw(timelocal_posix timegm_posix); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # The column types: how a raw value is turned into something that orders. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my %IS_TYPE = map { $_ => 1 } qw(string number date); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 52 | ▶ | sub is_blank ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_blank: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 52 | ▶ | return !defined $value || ( !ref $value && $value eq '' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $value or not ref $value and $value eq ""
not ref $value and $value eq ""
MC/DC100%3/3
not defined $value or not ref $value and $value eq ""
not defined $value
not ref $value
$value eq ""
Truth table100%4/4
not(A) or not(B) and C
A= not defined $valueB= not ref $valueC= $value eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # A finite number, or undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 31 | ▶ | sub number_of ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
number_of: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 31 | ▶ | return undef if is_blank($value) || ref $value || !looks_like_number($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
is_blank($value) or ref $value or not &looks_like_number($value)
is_blank($value) or ref $value
MC/DC100%3/3
is_blank($value) or ref $value or not &looks_like_number($value)
is_blank($value)
ref $value
not &looks_like_number($value)
Truth table100%4/4
A or B or not(C)
A= is_blank($value)B= ref $valueC= not &looks_like_number($value)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 19 | my $number = $value + 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 19 | ▶ | return $number == $number && $number - $number == 0 ? $number : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$number == $number and $number - $number == 0
MC/DC50%1/2
$number == $number and $number - $number == 0
$number == $number
$number - $number == 0
Truth table66%2/3
A and B
A= $number == $numberB= $number - $number == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # The span of time a date string names: [ first second, first second | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # after it ). Its granularity is the last part given: "2024" is a year, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # "2024-05" a month, "2024-05-03" a day, "2024-05-03 14:30" a minute. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Local time, or UTC with a trailing Z. Undef for anything else. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 17 | ▶ | sub date_interval ($text) { ## no critic (Subroutines::RequireFinalReturn) PPI does not parse try/catch | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
date_interval: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 17 | ▶ | return undef unless defined $text && !ref $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and not ref $text
MC/DC50%1/2
defined $text and not ref $text
defined $text
not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 16 | ▶ | my ( $year, $month, $day, $hour, $minute, $second, $utc ) = $text =~ m{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | \A \s* ([0-9]{4}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | (?: - ([0-9]{1,2}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | (?: - ([0-9]{1,2}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | (?: [ T] ([0-9]{1,2}) : ([0-9]{2}) (?: : ([0-9]{2}) )? )? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | )? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | )? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | \s* (Z)? \s* \z | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | }x or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 14 | ▶ | return undef if defined $utc && !defined $hour; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $utc and not defined $hour
MC/DC100%2/2
defined $utc and not defined $hour
defined $utc
not defined $hour
Truth table100%3/3
A and not(B)
A= defined $utcB= not defined $hour
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | ▶ | my @start = ( $second // 0, $minute // 0, $hour // 0, $day // 1, ( $month // 1 ) - 1, $year - 1900 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%10/10
$second // 0
$minute // 0
$hour // 0
$day // 1
$month // 1
MC/DC100%1/1
$second // 0
$second
MC/DC100%1/1
$minute // 0
$minute
MC/DC100%1/1
$hour // 0
$hour
MC/DC100%1/1
$day // 1
$day
MC/DC100%1/1
$month // 1
$month
Truth table100%2/2
A
A= $second
Truth table100%2/2
A
A= $minute
Truth table100%2/2
A
A= $hour
Truth table100%2/2
A
A= $day
Truth table100%2/2
A
A= $month
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | my $to_epoch = defined $utc ? \&timegm_posix : \&timelocal_posix; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 13 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 13 | my $first = $to_epoch->(@start); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 11 | ▶ | return undef unless _names_a_real_day( $first, \@start, defined $utc ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | ▶ | my $after | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | = defined $second ? $first + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | : defined $minute ? $first + 60 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | : defined $day ? $to_epoch->( 0, 0, 0, _next_day( @start[ 3, 4, 5 ] ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | : defined $month ? $to_epoch->( 0, 0, 0, 1, ( $start[4] + 1 ) % 12, $start[5] + ( $start[4] == 11 ? 1 : 0 ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | : $to_epoch->( 0, 0, 0, 1, 0, $start[5] + 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | return [ $first, $after ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | catch ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # Time::Local accepts 31 April as 1 May; a date that does not exist is no | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 11 | ▶ | sub _names_a_real_day ( $epoch, $parts, $utc ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_names_a_real_day: called CC=5 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 11 | ▶ | my @back = $utc ? gmtime $epoch : localtime $epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 11 | ▶ | return $back[3] == $parts->[3] && $back[4] == $parts->[4] && $back[5] == $parts->[5] ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$back[3] == $parts->[3] and $back[4] == $parts->[4] and $back[5] == $parts->[5]
$back[3] == $parts->[3] and $back[4] == $parts->[4]
MC/DC0%0/3
$back[3] == $parts->[3] and $back[4] == $parts->[4] and $back[5] == $parts->[5]
$back[3] == $parts->[3]
$back[4] == $parts->[4]
$back[5] == $parts->[5]
Truth table25%1/4
A and B and C
A= $back[3] == $parts->[3]B= $back[4] == $parts->[4]C= $back[5] == $parts->[5]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # ( day, month, year ) of the day after a calendar day, in Time::Local's | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # numbering; counted in UTC at noon, so no clock change gets in the way. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | sub _next_day ( $day, $month, $year ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_next_day: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | my @next = gmtime( timegm_posix( 0, 0, 12, $day, $month, $year ) + 86400 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | return @next[ 3, 4, 5 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # The epoch seconds of a date value: a number (already epoch seconds), an | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # object with an epoch method (DateTime, Time::Piece, Time::Moment), or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # date string as date_interval reads it (its first second). Undef for | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # anything else. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | sub date_epoch ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
date_epoch: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 10 | ▶ | return undef if is_blank($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | ▶ | return $value->epoch + 0 if blessed $value && $value->can('epoch'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($value) and $value->can("epoch")
MC/DC50%1/2
builtin::blessed($value) and $value->can("epoch")
builtin::blessed($value)
$value->can("epoch")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->can("epoch")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | my $number = number_of($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | ▶ | return $number if defined $number; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | my $interval = date_interval($value) // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
date_interval($value) // (return undef)
MC/DC100%1/1
date_interval($value) // (return undef)
date_interval($value)
Truth table100%2/2
A
A= date_interval($value)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | return $interval->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Orders text the way people count: digit runs compare as numbers, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # "file9" comes before "file10". Case-insensitive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | sub natural_compare ( $left, $right ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
natural_compare: called CC=7 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | my @left = split /([0-9]+)/, fc "$left"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | my @right = split /([0-9]+)/, fc "$right"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | while ( @left && @right ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@left and @right
MC/DC0%0/2
@left and @right
@left
@right
Truth table33%1/3
A and B
A= @leftB= @right
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my ( $mine, $theirs ) = ( shift @left, shift @right ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | my $order | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$mine =~ /\A[0-9]+\z/u and $theirs =~ /\A[0-9]+\z/u
$mine <=> $theirs or length $mine <=> length $theirs
MC/DC50%1/2
$mine =~ /\A[0-9]+\z/u and $theirs =~ /\A[0-9]+\z/u
$mine =~ /\A[0-9]+\z/u
$theirs =~ /\A[0-9]+\z/u
MC/DC0%0/2
$mine <=> $theirs or length $mine <=> length $theirs
$mine <=> $theirs
length $mine <=> length $theirs
Truth table66%2/3
A and B
A= $mine =~ /\A[0-9]+\z/uB= $theirs =~ /\A[0-9]+\z/u
Truth table33%1/3
A or B
A= $mine <=> $theirsB= length $mine <=> length $theirs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | = ( $mine =~ /\A[0-9]+\z/ && $theirs =~ /\A[0-9]+\z/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ? ( $mine <=> $theirs || length $mine <=> length $theirs ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | : $mine cmp $theirs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | ▶ | return $order if $order; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return @left <=> @right; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Orders two raw values of a column type; blank values and values that | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # are not of the type come last, in either direction of the sort (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # caller reverses only the result for known values). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | sub compare_values ( $type, $left, $right ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compare_values: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | die "Term::Fabulous::Widget::Table::Value: unknown column type '$type'" unless $IS_TYPE{$type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | return fc("$left") cmp fc("$right") if $type eq 'string'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | my $of = $type eq 'number' ? \&number_of : \&date_epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | return $of->($left) <=> $of->($right); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||