Comments (3)
it's an optimization shortcut
from fuzzysearch.
I think I have to admit this was the best explanation of confusing part of the code I have ever seen in my entire life!
from fuzzysearch.
I've decided to rewrite it as I consider code of this lib hard to understand and also I needed it to return informations about which parts were fuzzy and which were input
Maybe it'll be useful for someone (it's in typescript)
type MatchRoleType = 'input' | 'fuzzy' | 'suggestion';
interface FuzzyMatchData {
content: string;
type: MatchRoleType;
}
export interface FuzzyMatchOptions {
truncateTooLongInput?: boolean;
}
export function fuzzyMatch(
fuzzyInput: string,
fullString: string,
{ truncateTooLongInput }: FuzzyMatchOptions = {},
): FuzzyMatchData[] | false {
// input is longer than fullString and truncating is disabled
if (fuzzyInput.length > fullString.length && !truncateTooLongInput) {
return false;
}
// truncate if fuzzyinput is longer than fullstring
if (fuzzyInput.length > fullString.length && truncateTooLongInput) {
fuzzyInput = fuzzyInput.substr(0, fullString.length);
}
// both fuzzy and full string are equal - they match without being fuzzy
if (fuzzyInput === fullString) {
return [{ content: fuzzyInput, type: 'input' }];
}
const fuzzyMatchData: FuzzyMatchData[] = [];
const leftFuzzyLetters = fuzzyInput.split('');
let fuzzyLettersBuffer: string[] = [];
let matchingLettersBuffer: string[] = [];
function clearFuzzyBuffer() {
if (fuzzyLettersBuffer.length > 0) {
fuzzyMatchData.push({
content: fuzzyLettersBuffer.join(''),
type: 'fuzzy',
});
fuzzyLettersBuffer = [];
}
}
function clearMatchingBuffer() {
if (matchingLettersBuffer.length > 0) {
fuzzyMatchData.push({
content: matchingLettersBuffer.join(''),
type: 'input',
});
matchingLettersBuffer = [];
}
}
for (let fullStringLetter of fullString) {
const currentFuzzyLetter = leftFuzzyLetters[0];
console.log({ currentFuzzyLetter, fullStringLetter });
// no more input
if (!currentFuzzyLetter) {
// clear buffers
console.log('no more fuzzy letters');
break;
}
// another fuzzy letter to add
if (fullStringLetter !== currentFuzzyLetter) {
// make sure to clean matching letters buffer if we have some
clearMatchingBuffer();
fuzzyLettersBuffer.push(fullStringLetter);
continue;
}
// match!
leftFuzzyLetters.shift();
// clear fuzzy letters buffer
clearFuzzyBuffer();
matchingLettersBuffer.push(fullStringLetter);
if (!leftFuzzyLetters.length) {
clearMatchingBuffer();
}
}
if (leftFuzzyLetters.length > 0) {
return false;
}
const matchedPart = fuzzyMatchData.map((match) => match.content).join('');
const suggestionPart = fullString.replace(matchedPart, '');
if (suggestionPart) {
fuzzyMatchData.push({ content: suggestionPart, type: 'suggestion' });
}
return fuzzyMatchData;
}
[edit]
Actually - I've published it to npm because true/false result was not enough for my case - https://github.com/pie6k/fuzzystring
from fuzzysearch.
Related Issues (14)
- Added to Semantic UI HOT 1
- Case sensitive HOT 2
- Different from indexOf? HOT 2
- Support unicode fuzzy search HOT 2
- Interesing, and I got one too: HOT 2
- indexOf is just as fast HOT 4
- Support for a types file for use in Typescript
- Regex seem to be almost as performant
- Where can I find the chat with Mr. Aleph? HOT 4
- Test framework HOT 1
- levenshtein algorithm HOT 3
- What about... HOT 1
- Circuit breaker improvement HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fuzzysearch.