File Coverage

blib/lib/Sorter/by_similarity.pm
Criterion Covered Total %
statement 11 26 42.3
branch 0 8 0.0
condition n/a
subroutine 4 7 57.1
pod 0 2 0.0
total 15 43 34.8


line stmt bran cond sub pod time code
1             package Sorter::by_similarity;
2              
3 1     1   295585 use 5.010001;
  1         5  
4 1     1   7 use strict;
  1         2  
  1         38  
5 1     1   7 use warnings;
  1         3  
  1         77  
6              
7 1     1   695 use Text::Levenshtein::XS;
  1         905  
  1         302  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2024-01-24'; # DATE
11             our $DIST = 'Sorter-by_similarity'; # DIST
12             our $VERSION = '0.004'; # VERSION
13              
14             sub meta {
15             return +{
16 0     0 0   v => 1,
17             args => {
18             string => {schema=>'str*', req=>1},
19             reverse => {schema => 'bool*'},
20             ci => {schema => 'bool*'},
21             },
22             };
23             }
24              
25             sub gen_sorter {
26 0     0 0   my %args = @_;
27              
28 0           my $reverse = $args{reverse};
29 0           my $ci = $args{ci};
30              
31             sub {
32 0     0     my @items = @_;
33 0           my @distances;
34 0 0         if ($ci) {
35 0           @distances = map { Text::Levenshtein::XS::distance($args{string}, $_) } @items;
  0            
36             } else {
37 0           @distances = map { Text::Levenshtein::XS::distance(lc($args{string}), (lc $_)) } @items;
  0            
38             }
39              
40 0           map { $items[$_] } sort {
41 0 0         ($reverse ? $distances[$b] <=> $distances[$a] : $distances[$a] <=> $distances[$b]) ||
  0 0          
    0          
42             ($reverse ? $b <=> $a : $a <=> $b)
43             } 0 .. $#items;
44 0           };
45             }
46              
47             1;
48             # ABSTRACT: Sort by most similar to a reference string
49              
50             __END__