Skip to content

Instantly share code, notes, and snippets.

@fouric
Last active July 16, 2022 19:56
Show Gist options
  • Save fouric/e68513cf43903455e28c89d2ba799baf to your computer and use it in GitHub Desktop.
Save fouric/e68513cf43903455e28c89d2ba799baf to your computer and use it in GitHub Desktop.
function match(straw, needle, key = identity) {
let penalty = 0;
let start = 0;
let chars = [];
let text = key(straw);
for (char of needle) {
let pos = text.substr(start).search(char);
if (pos < 0) {
return false, chars];
} else {
chars.push(start + pos);
penalty += pos;
start += pos + 1;
}
}
return [penalty, chars];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment