Skip to content

Commit 6f222b5

Browse files
committed
Add a did-you-mean function for fuzzy matching
Previously only usable for finding the closest commit, now generalized for everything else.
1 parent 2911e5f commit 6f222b5

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

lib/Whateverable/Bits.pm6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ my token commit-list is export {
5858
[<-[\s] -[‘,’]>+]+ % [‘,’\s*]
5959
}
6060

61+
#| Get the closest fuzzy match
62+
sub did-you-mean($string, @options, :$default=Nil,
63+
:$max-offset=7, :$max-distance=10) is export {
64+
my $answer = $default;
65+
my $answer-min = ∞;
66+
my $distance-limit = $max-distance + 1;
67+
68+
use Text::Diff::Sift4;
69+
for @options {
70+
my $distance = sift4 $_, $string, $max-offset, $distance-limit;
71+
if $distance < $answer-min {
72+
$answer = $_;
73+
$answer-min = $distance;
74+
}
75+
}
76+
return $default if $answer-min > $max-distance;
77+
$answer
78+
}
79+
6180
sub time-left(Instant() $then, :$already-there?) is export {
6281
my $time-left = $then - now;
6382
return $already-there if $already-there and $time-left < 0;

lib/Whateverable/Builds.pm6

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,8 @@ sub get-similar($tag-or-hash, @other?, :$repo=$CONFIG<rakudo>) is export {
198198
.lines.map(*.substr: 0, $cutoff);
199199

200200
# flat(@options, @tags, @commits).min: { sift4($_, $tag-or-hash, 5, 8) }
201-
my $ans = HEAD;
202-
my $ans_min = ∞;
203-
204-
use Text::Diff::Sift4;
205-
for flat @options, @tags, @commits {
206-
my $dist = sift4 $_, $tag-or-hash, $cutoff;
207-
if $dist < $ans_min {
208-
$ans = $_;
209-
$ans_min = $dist;
210-
}
211-
}
212-
$ans
201+
did-you-mean $tag-or-hash, flat(@options, @tags, @commits),
202+
:default(HEAD), :max-offset($cutoff);
213203
}
214204

215205
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)