File Coverage

blib/lib/DOM/Tiny/_Collection.pm
Criterion Covered Total %
statement 91 103 88.3
branch 21 24 87.5
condition 7 9 77.7
subroutine 31 32 96.8
pod 0 19 0.0
total 150 187 80.2


line stmt bran cond sub pod time code
1             package DOM::Tiny::_Collection;
2              
3 2     2   14559 use strict;
  2         3  
  2         44  
4 2     2   7 use warnings;
  2         2  
  2         42  
5 2     2   6 use Carp 'croak';
  2         4  
  2         92  
6 2     2   8 use List::Util;
  2         2  
  2         94  
7 2     2   6 use Scalar::Util 'blessed';
  2         3  
  2         184  
8              
9 2 50   2   6 use constant REDUCE => ($] >= 5.008009 ? \&List::Util::reduce : \&_reduce);
  2         3  
  2         1116  
10              
11             our $VERSION = '0.003';
12              
13             sub new {
14 663     663 0 2107 my $class = shift;
15 663   66     5519 return bless [@_], ref $class || $class;
16             }
17              
18 1     1 0 48 sub TO_JSON { [@{shift()}] }
  1         4  
19              
20             sub compact {
21 3     3 0 3 my $self = shift;
22 3 100 66     4 return $self->new(grep { defined && (ref || length) } @$self);
  9         32  
23             }
24              
25             sub each {
26 113     113 0 561 my ($self, $cb) = @_;
27 113 100       257 return @$self unless $cb;
28 103         114 my $i = 1;
29 103         401 $_->$cb($i++) for @$self;
30 103         186 return $self;
31             }
32              
33             sub first {
34 74     74 0 816 my ($self, $cb) = (shift, shift);
35 74 100       1252 return $self->[0] unless $cb;
36 11 100   2   38 return List::Util::first { $_ =~ $cb } @$self if ref $cb eq 'Regexp';
  2         15  
37 10     22   64 return List::Util::first { $_->$cb(@_) } @$self;
  22         70  
38             }
39              
40 5     5 0 13 sub flatten { $_[0]->new(_flatten(@{$_[0]})) }
  5         18  
41              
42             sub grep {
43 9     9 0 33 my ($self, $cb) = (shift, shift);
44 9 100       30 return $self->new(grep { $_ =~ $cb } @$self) if ref $cb eq 'Regexp';
  9         35  
45 8         16 return $self->new(grep { $_->$cb(@_) } @$self);
  60         178  
46             }
47              
48             sub join {
49 37 100   37 0 87 join +(defined($_[1]) ? $_[1] : ''), map {"$_"} @{$_[0]};
  83         258  
  37         81  
50             }
51              
52 18     18 0 76 sub last { shift->[-1] }
53              
54             sub map {
55 37     37 0 71 my ($self, $cb) = (shift, shift);
56 37         67 return $self->new(map { $_->$cb(@_) } @$self);
  76         205  
57             }
58              
59             sub reduce {
60 3     3 0 307 my $self = shift;
61 3         10 @_ = (@_, @$self);
62 3         4 goto &{REDUCE()};
  3         34  
63             }
64              
65 8     8 0 43 sub reverse { $_[0]->new(reverse @{$_[0]}) }
  8         22  
66              
67 2     2 0 137 sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }
  2         262  
68              
69 46     46 0 506 sub size { scalar @{$_[0]} }
  46         199  
70              
71             sub slice {
72 8     8 0 15 my $self = shift;
73 8         23 return $self->new(@$self[@_]);
74             }
75              
76             sub sort {
77 6     6 0 24 my ($self, $cb) = @_;
78              
79 6 100       21 return $self->new(sort @$self) unless $cb;
80              
81 4         10 my $caller = caller;
82 2     2   8 no strict 'refs';
  2         2  
  2         505  
83             my @sorted = sort {
84 4         16 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\$a, \$b);
  12         33  
  12         27  
  12         22  
85 12         24 $a->$cb($b);
86             } @$self;
87 4         20 return $self->new(@sorted);
88             }
89              
90             sub tap {
91 2     2 0 6 my ($self, $cb) = (shift, shift);
92 2         8 $_->$cb(@_) for $self;
93 2         9 return $self;
94             }
95              
96 37     37 0 41 sub to_array { [@{shift()}] }
  37         178  
97              
98             sub uniq {
99 5     5 0 23 my ($self, $cb) = (shift, shift);
100 5         7 my %seen;
101 5 100       14 return $self->new(grep { !$seen{$_->$cb(@_)}++ } @$self) if $cb;
  6         23  
102 3         6 return $self->new(grep { !$seen{$_}++ } @$self);
  21         47  
103             }
104              
105             sub _flatten {
106 16 100   16   16 map { _ref($_) ? _flatten(@$_) : $_ } @_;
  41         46  
107             }
108              
109             # For perl < 5.8.9
110             sub _reduce (&@) {
111 0     0   0 my $code = shift;
112              
113 0 0       0 return shift unless @_ > 1;
114              
115 0         0 my $caller = caller;
116              
117 2     2   8 no strict 'refs';
  2         3  
  2         210  
118              
119 0         0 local(*{$caller."::a"}) = \my $x;
  0         0  
120 0         0 local(*{$caller."::b"}) = \my $y;
  0         0  
121              
122 0         0 $x = shift;
123 0         0 foreach (@_) {
124 0         0 $y = $_;
125 0         0 $x = $code->();
126             }
127              
128 0         0 $x;
129             }
130              
131 41 100 100 41   239 sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) }
132              
133             1;
134              
135             =for Pod::Coverage *EVERYTHING*
136              
137             =cut