| blib/lib/Pinwheel/Helpers/Text.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 54 | 54 | 100.0 |
| branch | 16 | 16 | 100.0 |
| condition | 3 | 3 | 100.0 |
| subroutine | 14 | 14 | 100.0 |
| pod | 0 | 9 | 0.0 |
| total | 87 | 96 | 90.6 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Pinwheel::Helpers::Text; | ||||||
| 2 | |||||||
| 3 | 5 | 5 | 34735 | use strict; | |||
| 5 | 11 | ||||||
| 5 | 192 | ||||||
| 4 | 5 | 5 | 25 | use warnings; | |||
| 5 | 10 | ||||||
| 5 | 138 | ||||||
| 5 | |||||||
| 6 | 5 | 5 | 25 | use Exporter; | |||
| 5 | 10 | ||||||
| 5 | 221 | ||||||
| 7 | |||||||
| 8 | 5 | 5 | 646 | use Pinwheel::Context; | |||
| 5 | 9 | ||||||
| 5 | 239 | ||||||
| 9 | 5 | 5 | 575 | use Pinwheel::View::String; | |||
| 5 | 11 | ||||||
| 5 | 4029 | ||||||
| 10 | |||||||
| 11 | our @ISA = qw(Exporter); | ||||||
| 12 | our @EXPORT_OK = qw(h p lc uc tc join simple_format cycle pluralize ordinal_text); | ||||||
| 13 | |||||||
| 14 | |||||||
| 15 | sub h | ||||||
| 16 | { | ||||||
| 17 | 1 | 1 | 0 | 13 | return $_[0]; | ||
| 18 | } | ||||||
| 19 | |||||||
| 20 | sub lc | ||||||
| 21 | { | ||||||
| 22 | 1 | 1 | 0 | 6 | return CORE::lc($_[0]); | ||
| 23 | } | ||||||
| 24 | |||||||
| 25 | sub uc | ||||||
| 26 | { | ||||||
| 27 | 1 | 1 | 0 | 500 | return CORE::uc($_[0]); | ||
| 28 | } | ||||||
| 29 | |||||||
| 30 | sub tc | ||||||
| 31 | { | ||||||
| 32 | 5 | 5 | 0 | 13 | my $t = CORE::lc($_[0]); | ||
| 33 | 5 | 24 | $t =~ s/\b(\w)/CORE::uc $1/eg; | ||||
| 7 | 27 | ||||||
| 34 | 5 | 23 | $t; | ||||
| 35 | } | ||||||
| 36 | |||||||
| 37 | sub join | ||||||
| 38 | { | ||||||
| 39 | 10 | 10 | 0 | 672 | my ($array, $s1, $s2) = @_; | ||
| 40 | 10 | 14 | my ($last); | ||||
| 41 | |||||||
| 42 | 10 | 100 | 32 | return '' unless @$array > 0; | |||
| 43 | 8 | 11 | $last = pop @$array; | ||||
| 44 | 8 | 100 | 26 | return $last unless @$array > 0; | |||
| 45 | |||||||
| 46 | 6 | 100 | 15 | $s2 = $s1 if !defined($s2); | |||
| 47 | 6 | 33 | return CORE::join($s2, CORE::join($s1, @$array), $last); | ||||
| 48 | } | ||||||
| 49 | |||||||
| 50 | sub simple_format | ||||||
| 51 | { | ||||||
| 52 | 16 | 16 | 0 | 37 | my ($s) = @_; | ||
| 53 | 16 | 18 | my ($parts, $p, $line, $i, $j); | ||||
| 54 | |||||||
| 55 | 16 | 38 | $s =~ s{^\s+}{}; | ||||
| 56 | 16 | 41 | $s =~ s{\s+$}{}; | ||||
| 57 | |||||||
| 58 | 16 | 39 | $parts = [[' ']]; |
||||
| 59 | 16 | 96 | foreach $p (split(/ *(?:\r?\n *){2,} */, $s)) { | ||||
| 60 | 22 | 100 | 59 | push @$parts, ["\n "] if $i++; |
|||
| 61 | 22 | 26 | $j = 0; | ||||
| 62 | 22 | 113 | foreach $line (split(/(?<=[^\r\n])\r?\n(?=[^\r\n])/, $p)) { | ||||
| 63 | 28 | 100 | 56 | push @$parts, [" \n"] if $j++; |
|||
| 64 | 28 | 1725 | push @$parts, $line; | ||||
| 65 | } | ||||||
| 66 | } | ||||||
| 67 | 16 | 36 | push @$parts, ['']; | ||||
| 68 | |||||||
| 69 | 16 | 60 | return Pinwheel::View::String->new($parts); | ||||
| 70 | } | ||||||
| 71 | *p = *simple_format; | ||||||
| 72 | |||||||
| 73 | sub cycle | ||||||
| 74 | { | ||||||
| 75 | 7 | 7 | 0 | 499 | my ($ctx, $key, $i); | ||
| 76 | 7 | 33 | $key = (caller)[2] . "\t" . CORE::join("\t", @_); | ||||
| 77 | 7 | 20 | $ctx = Pinwheel::Context::get('render'); | ||||
| 78 | 7 | 18 | $i = $ctx->{cycle}{$key}++; | ||||
| 79 | 7 | 27 | return $_[$i % scalar(@_)]; | ||||
| 80 | } | ||||||
| 81 | |||||||
| 82 | sub pluralize | ||||||
| 83 | { | ||||||
| 84 | 4 | 4 | 0 | 530 | my ($count, $singular, $plural) = @_; | ||
| 85 | 4 | 100 | 16 | return $singular if $count == 1; | |||
| 86 | 2 | 100 | 11 | return $plural if defined($plural); | |||
| 87 | 1 | 6 | return $singular . 's'; | ||||
| 88 | } | ||||||
| 89 | |||||||
| 90 | sub ordinal_text | ||||||
| 91 | { | ||||||
| 92 | 11 | 11 | 0 | 607 | my $i = (0 + shift) % 100; | ||
| 93 | 11 | 100 | 100 | 60 | return 'th' if ($i >= 10 && $i < 20); | ||
| 94 | 8 | 42 | return qw(th st nd rd th th th th th th)[$i % 10]; | ||||
| 95 | } | ||||||
| 96 | |||||||
| 97 | =head1 SYNOPSIS | ||||||
| 98 | |||||||
| 99 | use Pinwheel::Helpers::Text; | ||||||
| 100 | |||||||
| 101 | # The following are listed in @EXPORT_OK, but nothing is exported by default | ||||||
| 102 | |||||||
| 103 | $text = h($text); # ? | ||||||
| 104 | |||||||
| 105 | $text = uc($text); # UPPER CASE | ||||||
| 106 | $text = lc($text); # lower case | ||||||
| 107 | $text = tc($text); # Title Case (uses \b to detect words) | ||||||
| 108 | |||||||
| 109 | # Joins the items of @list using $sep, except for the last item which is | ||||||
| 110 | # joined using $last_sep | ||||||
| 111 | $text = join(\@list, $sep, $last_sep); | ||||||
| 112 | join(["Dave Dee", "Dozy", "Beaky", "Mick", "Tich"], ", ", " & ") -> "Dave Dee, Dozy, Beaky, Mick & Tich" | ||||||
| 113 | |||||||
| 114 | simple_format; # ? | ||||||
| 115 | # p is an alias for simple_format | ||||||
| 116 | |||||||
| 117 | cycle; # ? | ||||||
| 118 | |||||||
| 119 | $text = pluralize($count, $singular[, $plural]); | ||||||
| 120 | pluralize(1, "mouse", "mice") -> "mouse" | ||||||
| 121 | pluralize(2, "mouse", "mice") -> "mice" | ||||||
| 122 | pluralize(1, "dog") -> "dog" | ||||||
| 123 | pluralize(2, "dog") -> "dogs" | ||||||
| 124 | |||||||
| 125 | $text = ordinal_text($n); # One of: st nd rd th | ||||||
| 126 | # depending on $n | ||||||
| 127 | |||||||
| 128 | =pod | ||||||
| 129 | |||||||
| 130 | 1; |