File Coverage

blib/lib/String/Query/To/Regexp.pm
Criterion Covered Total %
statement 25 25 100.0
branch 12 14 85.7
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             package String::Query::To::Regexp;
2              
3 1     1   66071 use 5.010001;
  1         15  
4 1     1   6 use strict;
  1         2  
  1         21  
5 1     1   4 use warnings;
  1         3  
  1         48  
6              
7 1     1   6 use Exporter 'import';
  1         2  
  1         340  
8             our @EXPORT_OK = qw(query2re);
9              
10             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11             our $DATE = '2022-11-01'; # DATE
12             our $DIST = 'String-Query-To-Regexp'; # DIST
13             our $VERSION = '0.001'; # VERSION
14              
15             sub query2re {
16 6 100   6 1 18126 my $opts = ref($_[0]) eq 'HASH' ? shift : {};
17 6   100     30 my $bool = $opts->{bool} // 'and';
18 6         11 my $ci = $opts->{ci};
19 6         13 my $word = $opts->{word};
20              
21 6 50       16 return qr// unless @_;
22 6         10 my @re_parts;
23 6         13 for my $query0 (@_) {
24 10         53 my ($neg, $query) = $query0 =~ /\A(-?)(.*)/;
25 10         22 $query = quotemeta $query;
26 10 100       22 if ($word) {
27 1 50       7 push @re_parts, $neg ? "(?!.*\\b$query\\b)" : "(?=.*\\b$query\\b)";
28             } else {
29 9 100       33 push @re_parts, $neg ? "(?!.*$query)" : "(?=.*$query)";
30             }
31             }
32 6 100       20 my $re = $bool eq 'or' ? "(?:".join("|", @re_parts).")" : join("", @re_parts);
33 6 100       143 return $ci ? qr/\A$re.*\z/is : qr/\A$re.*\z/s;
34             }
35              
36             1;
37             # ABSTRACT: Convert query to regular expression
38              
39             __END__