add fuzzySearch
This commit is contained in:
33
snippets/fuzzySearch.md
Normal file
33
snippets/fuzzySearch.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
### fuzzySearch
|
||||||
|
|
||||||
|
Determines if the `patrn` matches with `str`
|
||||||
|
|
||||||
|
Loops through `str` and determines if it contains all characters of `patrn` and in the correct order. Both the strings are converted to lower case.
|
||||||
|
|
||||||
|
Taken from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18).
|
||||||
|
``` js
|
||||||
|
fuzzySearch = (patrn, str) => {
|
||||||
|
const pattern = patrn;
|
||||||
|
const string = str;
|
||||||
|
let patternIdx = 0;
|
||||||
|
let strIdx = 0;
|
||||||
|
let patternLength = pattern.length;
|
||||||
|
let strLength = string.length;
|
||||||
|
|
||||||
|
while (patternIdx !== patternLength && strIdx !== strLength) {
|
||||||
|
let patternChar = pattern[patternIdx].toLowerCase();
|
||||||
|
let strChar = string[strIdx].toLowerCase();
|
||||||
|
if (patternChar === strChar)
|
||||||
|
++patternIdx;
|
||||||
|
++strIdx;
|
||||||
|
}
|
||||||
|
|
||||||
|
return patternLength !== 0 && strLength !== 0 && patternIdx === patternLength ? true : false;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
``` js
|
||||||
|
fuzzySearch('rt','Rohit); // true
|
||||||
|
fuzzySearch('tr','Rohit); // false
|
||||||
|
```
|
||||||
19
test/fuzzySearch/fuzzySearch.js
Normal file
19
test/fuzzySearch/fuzzySearch.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
fuzzySearch = (patrn, str) => {
|
||||||
|
const pattern = patrn;
|
||||||
|
const string = str;
|
||||||
|
let patternIdx = 0;
|
||||||
|
let strIdx = 0;
|
||||||
|
let patternLength = pattern.length;
|
||||||
|
let strLength = string.length;
|
||||||
|
|
||||||
|
while (patternIdx !== patternLength && strIdx !== strLength) {
|
||||||
|
let patternChar = pattern[patternIdx].toLowerCase();
|
||||||
|
let strChar = string[strIdx].toLowerCase();
|
||||||
|
if (patternChar === strChar)
|
||||||
|
++patternIdx;
|
||||||
|
++strIdx;
|
||||||
|
}
|
||||||
|
|
||||||
|
return patternLength !== 0 && strLength !== 0 && patternIdx === patternLength ? true : false;
|
||||||
|
}
|
||||||
|
module.exports = fuzzySearch;
|
||||||
13
test/fuzzySearch/fuzzySearch.test.js
Normal file
13
test/fuzzySearch/fuzzySearch.test.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const test = require('tape');
|
||||||
|
const fuzzySearch = require('./fuzzySearch.js');
|
||||||
|
|
||||||
|
test('Testing fuzzySearch', (t) => {
|
||||||
|
//For more information on all the methods supported by tape
|
||||||
|
//Please go to https://github.com/substack/tape
|
||||||
|
t.true(typeof fuzzySearch === 'function', 'fuzzySearch is a Function');
|
||||||
|
//t.deepEqual(fuzzySearch(args..), 'Expected');
|
||||||
|
//t.equal(fuzzySearch(args..), 'Expected');
|
||||||
|
//t.false(fuzzySearch(args..), 'Expected');
|
||||||
|
//t.throws(fuzzySearch(args..), 'Expected');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
2014
test/testlog
2014
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user