Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"main": "./dist/FuzzySearch.js",
"module": "src/FuzzySearch.js",
"devDependencies": {
"@babel/register": "^7.6.2",
"cross-env": "^6.0.3",
"eslint": "^6.6.0",
"jasmine": "^3.5.0",
"laravel-mix": "^5.0.0",
"vue-template-compiler": "^2.6.11"
"@babel/register": "^7.12.13",
"cross-env": "^7.0.3",
"eslint": "^7.19.0",
"jasmine": "^3.6.4",
"laravel-mix": "^6.0.11",
"vue-template-compiler": "^2.6.12"
},
"scripts": {
"dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js && rm mix-manifest.json",
Expand All @@ -35,4 +35,4 @@
},
"homepage": "https://github.com/wouter2203/fuzzy-search#readme",
"tonicExampleFilename": "example.js"
}
}
8 changes: 7 additions & 1 deletion spec/FuzzySearchSpec.js → spec/FuzzySearchSpec.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FuzzySearch from '../src/FuzzySearch';
import FuzzySearch from '../src/FuzzySearch.mjs';

describe('FuzzySearch', () => {
it('should return an error when called as a function', () => {
Expand Down Expand Up @@ -145,4 +145,10 @@ describe('FuzzySearch', () => {

expect([1, 12, 11]).toEqual(fuzzy.search(1));
});

it('should sort abbreviation as first', () => {
const fuzzy = new FuzzySearch(["General Preferences Table", "Group Preferences Table", "Prefix User Preferences Table", "User Preferences Table"], { sort: true });

expect(["User Preferences Table", "Prefix User Preferences Table", "Group Preferences Table"]).toEqual(fuzzy.search("upt"));
});
});
2 changes: 1 addition & 1 deletion spec/HelperSpec.js → spec/HelperSpec.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Helper from '../src/Helper';
import Helper from '../src/Helper.mjs';

describe('Helper', () => {
it('should allow for deep key search', () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
"**/*[sS]pec.mjs"
],
"helpers": [
"../node_modules/@babel/register/lib/node.js"
Expand Down
18 changes: 15 additions & 3 deletions src/FuzzySearch.js → src/FuzzySearch.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Helper from './Helper';
import Helper from './Helper.mjs';

export default class FuzzySearch {
constructor(haystack = [], keys = [], options = {}) {
Expand Down Expand Up @@ -83,13 +83,25 @@ export default class FuzzySearch {
return 1;
}

// If we hit abbreviation it should go before others (except exact match).
const abbreviationIndicies = [0];
for (let i=0; i<item.length; i++) {
if (item[i] === " ") abbreviationIndicies.push(i + 1);
}

if (indexes.reduce((accumulator, currentValue) => abbreviationIndicies.includes(currentValue) && accumulator, true)) {
return 2 + indexes.reduce((accumulator, currentValue, i) => {
return accumulator + abbreviationIndicies.indexOf(currentValue);
}, 0);
}

// If we have more than 2 letters, matches close to each other should be first.
if (indexes.length > 1) {
return 2 + (indexes[indexes.length - 1] - indexes[0]);
return 3 + (indexes[indexes.length - 1] - indexes[0]);
}

// Matches closest to the start of the string should be first.
return 2 + indexes[0];
return 3 + indexes[0];
}

static nearestIndexesFor(item, query) {
Expand Down
File renamed without changes.