File Coverage

blib/lib/List/Util/Uniq.pm
Criterion Covered Total %
statement 51 51 100.0
branch 20 26 76.9
condition 8 18 44.4
subroutine 9 9 100.0
pod 7 7 100.0
total 95 111 85.5


line stmt bran cond sub pod time code
1             package List::Util::Uniq;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-07-31'; # DATE
5             our $DIST = 'List-Util-Uniq'; # DIST
6             our $VERSION = '0.004'; # VERSION
7              
8 1     1   69049 use strict;
  1         10  
  1         31  
9 1     1   6 use warnings;
  1         2  
  1         558  
10              
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(
14             uniq_adj
15             uniq_adj_ci
16             uniq_ci
17             is_uniq
18             is_uniq_ci
19             is_monovalued
20             is_monovalued_ci
21             );
22              
23             sub uniq_adj {
24 2     2 1 4363 my @res;
25              
26 2 50       8 return () unless @_;
27 2         6 my $last = shift;
28 2         5 push @res, $last;
29 2         7 for (@_) {
30 12 0 33     20 next if !defined($_) && !defined($last);
31             # XXX $_ becomes stringified
32 12 100 33     60 next if defined($_) && defined($last) && $_ eq $last;
      66        
33 10         18 push @res, $_;
34 10         18 $last = $_;
35             }
36 2         29 @res;
37             }
38              
39             sub uniq_adj_ci {
40 1     1 1 2 my @res;
41              
42 1 50       4 return () unless @_;
43 1         3 my $last = shift;
44 1         3 push @res, $last;
45 1         3 for (@_) {
46 6 0 33     13 next if !defined($_) && !defined($last);
47             # XXX $_ becomes stringified
48 6 100 33     29 next if defined($_) && defined($last) && lc($_) eq lc($last);
      66        
49 4         7 push @res, $_;
50 4         7 $last = $_;
51             }
52 1         7 @res;
53             }
54              
55             sub uniq_ci {
56 2     2 1 2511 my @res;
57              
58             my %mem;
59 2         0 my $undef_added;
60 2         15 for (@_) {
61 13 100       25 if (defined) {
62 11 100       32 push @res, $_ unless $mem{lc $_}++;
63             } else {
64 2 100       7 push @res, $_ unless $undef_added++;
65             }
66             }
67 2         15 @res;
68             }
69              
70             sub is_uniq {
71 5     5 1 2466 my %vals;
72 5         11 for (@_) {
73 7 100       24 return 0 if $vals{$_}++;
74             }
75 4         18 1;
76             }
77              
78             sub is_uniq_ci {
79 5     5 1 2264 my %vals;
80 5         13 for (@_) {
81 7 100       27 return 0 if $vals{lc $_}++;
82             }
83 3         13 1;
84             }
85              
86             sub is_monovalued {
87 5     5 1 2257 my %vals;
88 5         11 for (@_) {
89 7         15 $vals{$_} = 1;
90 7 100       25 return 0 if keys(%vals) > 1;
91             }
92 3         15 1;
93             }
94              
95             sub is_monovalued_ci {
96 5     5 1 2278 my %vals;
97 5         8 for (@_) {
98 7         20 $vals{lc $_} = 1;
99 7 100       24 return 0 if keys(%vals) > 1;
100             }
101 4         15 1;
102             }
103              
104             1;
105             # ABSTRACT: List utilities related to finding unique items
106              
107             __END__