File Coverage

blib/lib/Data/Sah/Filter/perl/Array/check_uniq.pm
Criterion Covered Total %
statement 8 17 47.0
branch 0 2 0.0
condition 0 2 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 11 28 39.2


line stmt bran cond sub pod time code
1             package Data::Sah::Filter::perl::Array::check_uniq;
2              
3 1     1   616232 use 5.010001;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         51  
5 1     1   5 use warnings;
  1         1  
  1         340  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-07-17'; # DATE
9             our $DIST = 'Data-Sah-Filter'; # DIST
10             our $VERSION = '0.025'; # VERSION
11              
12             sub meta {
13             +{
14 0     0 0   v => 1,
15             summary => 'Check that an array has unique elements, using List::Util\'s uniq()',
16             target_type => 'array',
17             might_fail => 1,
18             args => {
19             reverse => {
20             summary => 'If set to true, then will *fail* when array is unique',
21             schema => 'bool*',
22             },
23             },
24             examples => [
25             {value=>[], valid=>1},
26             {value=>["a","b"], valid=>1},
27             {value=>["a","b","a"], valid=>0},
28             {value=>["a","b","A"], valid=>1, summary=>'Use Array::check_uniqstr filter for case insensitivity option'},
29              
30             {value=>[], filter_args=>{reverse=>1}, valid=>0},
31             {value=>["a","b"], filter_args=>{reverse=>1}, valid=>0},
32             {value=>["a","b","a"], filter_args=>{reverse=>1}, valid=>1},
33             {value=>["a","b","A"], filter_args=>{reverse=>1}, valid=>0, summary=>'Use Array::check_uniqstr filter for case insensitivity option'},
34             ],
35             };
36             }
37              
38             sub filter {
39 0     0 0   my %fargs = @_;
40              
41 0           my $dt = $fargs{data_term};
42 0   0       my $gen_args = $fargs{args} // {};
43 0           my $res = {};
44 0           $res->{modules}{'List::Util::Uniq'} = "0.005";
45 0           $res->{modules}{'Data::Dmp'} = "0.242";
46             $res->{expr_filter} = join(
47             "",
48             "do { my \$tmp=$dt; my \@dupes = List::Util::Uniq::uniq( List::Util::Uniq::dupe(\@\$tmp) ); ",
49 0 0         ($gen_args->{reverse} ? "\@dupes ? [undef,\$tmp] : [\"Array does not have duplicate element(s)\"]" : "!\@dupes ? [undef,\$tmp] : [\"Array has duplicate element(s): \".join(', ', map { Data::Dmp::dmp(\$_) } \@dupes)]"),
50             "}",
51             );
52              
53 0           $res;
54             }
55              
56             1;
57             # ABSTRACT: Check that an array has unique elements, using List::Util's uniq()
58              
59             __END__