File Coverage

blib/lib/Mojo/DOM58/_Collection.pm
Criterion Covered Total %
statement 118 129 91.4
branch 37 42 88.1
condition 7 9 77.7
subroutine 37 38 97.3
pod 0 22 0.0
total 199 240 82.9


line stmt bran cond sub pod time code
1             package Mojo::DOM58::_Collection;
2              
3             # This file is part of Mojo::DOM58 which is released under:
4             # The Artistic License 2.0 (GPL Compatible)
5             # See the documentation for Mojo::DOM58 for full license details.
6              
7 3     3   143787 use strict;
  3         7  
  3         127  
8 3     3   18 use warnings;
  3         9  
  3         205  
9 3     3   19 use Carp 'croak';
  3         7  
  3         226  
10 3     3   22 use List::Util;
  3         6  
  3         261  
11 3     3   33 use Scalar::Util 'blessed';
  3         34  
  3         259  
12 3     3   21 use re ();
  3         7  
  3         244  
13              
14 3 50   3   26 use constant REDUCE => ($] >= 5.008009 ? \&List::Util::reduce : \&_reduce);
  3         7  
  3         467  
15 3     3   24 use constant HAS_IS_REGEXP => !!($] >= 5.010000);
  3         6  
  3         446  
16              
17             # Role support requires Role::Tiny 2.000001+
18             use constant ROLES =>
19 3     3   22 !!(eval { require Role::Tiny; Role::Tiny->VERSION('2.000001'); 1 });
  3         4  
  3         8  
  3         1956  
  3         21748  
  3         3955  
20              
21             our $VERSION = '3.002';
22              
23             sub new {
24 761     761 0 649379 my $class = shift;
25 761   66     10570 return bless [@_], ref $class || $class;
26             }
27              
28 1     1 0 71 sub TO_JSON { [@{shift()}] }
  1         6  
29              
30             sub compact {
31 3     3 0 7 my $self = shift;
32 3 100 66     44 return $self->new(grep { defined && (ref || length) } @$self);
  9         52  
33             }
34              
35             sub each {
36 116     116 0 4708 my ($self, $cb) = @_;
37 116 100       378 return @$self unless $cb;
38 106         169 my $i = 1;
39 106         513 $_->$cb($i++) for @$self;
40 106         293 return $self;
41             }
42              
43             sub first {
44 76     76 0 6849 my ($self, $cb) = (shift, shift);
45 76 100       463 return $self->[0] unless $cb;
46 11 100   2   44 return List::Util::first { $_ =~ $cb } @$self if HAS_IS_REGEXP ? re::is_regexp($cb) : ref $cb eq 'Regexp';
  2         18  
47 10     22   79 return List::Util::first { $_->$cb(@_) } @$self;
  22         85  
48             }
49              
50 5     5 0 17 sub flatten { $_[0]->new(_flatten(@{$_[0]})) }
  5         29  
51              
52             sub grep {
53 35     35 0 122 my ($self, $cb) = (shift, shift);
54 35 100       158 return $self->new(grep { $_ =~ $cb } @$self) if HAS_IS_REGEXP ? re::is_regexp($cb) : ref $cb eq 'Regexp';
  9         39  
55 34         87 return $self->new(grep { $_->$cb(@_) } @$self);
  106         415  
56             }
57              
58             sub head {
59 9     9 0 33 my ($self, $size) = @_;
60 9 100       35 return $self->new(@$self) if $size > @$self;
61 8 100       36 return $self->new(@$self[0 .. ($size - 1)]) if $size >= 0;
62 4         21 return $self->new(@$self[0 .. ($#$self + $size)]);
63             }
64              
65             sub join {
66 49 100   49 0 207 join +(defined($_[1]) ? $_[1] : ''), map {"$_"} @{$_[0]};
  114         459  
  49         154  
67             }
68              
69 24     24 0 120 sub last { shift->[-1] }
70              
71             sub map {
72 54     54 0 155 my ($self, $cb) = (shift, shift);
73 54         148 return $self->new(map { $_->$cb(@_) } @$self);
  113         385  
74             }
75              
76             sub reduce {
77 3     3 0 1098 my $self = shift;
78 3         13 @_ = (@_, @$self);
79 3         7 goto &{REDUCE()};
  3         22  
80             }
81              
82 8     8 0 27 sub reverse { $_[0]->new(reverse @{$_[0]}) }
  8         23  
83              
84 2     2 0 235 sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }
  2         497  
85              
86 54     54 0 139 sub size { scalar @{$_[0]} }
  54         368  
87              
88             sub slice {
89 8     8 0 25 my $self = shift;
90 8         35 return $self->new(@$self[@_]);
91             }
92              
93             sub sort {
94 6     6 0 32 my ($self, $cb) = @_;
95              
96 6 100       29 return $self->new(sort @$self) unless $cb;
97              
98 4         55 my $caller = caller;
99 3     3   33 no strict 'refs';
  3         7  
  3         2882  
100             my @sorted = sort {
101 4         20 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\$a, \$b);
  12         45  
  12         31  
  12         27  
102 12         28 $a->$cb($b);
103             } @$self;
104 4         29 return $self->new(@sorted);
105             }
106              
107             sub tail {
108 9     9 0 82 my ($self, $size) = @_;
109 9 100       38 return $self->new(@$self) if $size > @$self;
110 8 100       35 return $self->new(@$self[($#$self - ($size - 1)) .. $#$self]) if $size >= 0;
111 4         23 return $self->new(@$self[(0 - $size) .. $#$self]);
112             }
113              
114             sub tap {
115 2     2 0 8 my ($self, $cb) = (shift, shift);
116 2         13 $_->$cb(@_) for $self;
117 2         15 return $self;
118             }
119              
120 57     57 0 163 sub to_array { [@{shift()}] }
  57         393  
121              
122             sub uniq {
123 7     7 0 40 my ($self, $cb) = (shift, shift);
124 7         59 my %seen;
125 7 100       75 return $self->new(grep { my $r = $_->$cb(@_); !$seen{defined $r ? $r : ''}++ } @$self) if $cb;
  12 100       31  
  12         61  
126 4 100       12 return $self->new(grep { !$seen{defined $_ ? $_ : ''}++ } @$self);
  27         89  
127             }
128              
129             sub with_roles {
130 2     2 0 4 croak 'Role::Tiny 2.000001+ is required for roles' unless ROLES;
131 2         9 my ($self, @roles) = @_;
132            
133             return Role::Tiny->create_class_with_roles($self,
134 2 50       10 map { /^\+(.+)$/ ? "${self}::Role::$1" : $_ } @roles)
  1 100       10  
135             unless my $class = blessed $self;
136            
137             return Role::Tiny->apply_roles_to_object($self,
138 1 50       3 map { /^\+(.+)$/ ? "${class}::Role::$1" : $_ } @roles);
  1         16  
139             }
140              
141             sub _flatten {
142 16 100   16   40 map { _ref($_) ? _flatten(@$_) : $_ } @_;
  41         79  
143             }
144              
145             # For perl < 5.8.9
146             sub _reduce (&@) {
147 0     0   0 my $code = shift;
148              
149 0 0       0 return shift unless @_ > 1;
150              
151 0         0 my $caller = caller;
152              
153 3     3   29 no strict 'refs';
  3         6  
  3         751  
154              
155 0         0 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\my $x, \my $y);
  0         0  
  0         0  
156              
157 0         0 $x = shift;
158 0         0 foreach my $e (@_) {
159 0         0 $y = $e;
160 0         0 $x = $code->();
161             }
162              
163 0         0 $x;
164             }
165              
166 41 100 100 41   290 sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) }
167              
168             1;
169              
170             =for Pod::Coverage *EVERYTHING*
171              
172             =cut