-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
179 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// eslint-disable-next-line import/no-unresolved | ||
import Fuse from 'fuse.js/min-basic'; | ||
|
||
const options = { | ||
threshold: 0.25, | ||
keys: [ | ||
'description', | ||
'label', | ||
'search' | ||
] | ||
}; | ||
|
||
export function searchEntries(entries, value) { | ||
if (!value) { | ||
return entries.filter(entry => (entry.rank || 0) >= 0); | ||
} | ||
|
||
const fuse = new Fuse(entries.filter(entry => entry.searchable !== false), options); | ||
|
||
const result = fuse.search(value); | ||
|
||
return result.map(({ item }) => item); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { searchEntries } from '../../../../lib/features/popup-menu/PopupMenuSearchUtil'; | ||
|
||
|
||
describe('features/popup-menu - PopupMenuSearchUtil', function() { | ||
|
||
describe('ranking', function() { | ||
|
||
it('should hide rank < 0 if not searching', function() { | ||
|
||
// given | ||
const entries = [ | ||
{ id: 'entry-1', rank: -1 }, | ||
{ id: 'entry-2' }, | ||
{ id: 'entry-3' } | ||
]; | ||
|
||
// when | ||
const result = searchEntries(entries, ''); | ||
|
||
// then | ||
expect(result).to.have.length(2); | ||
expect(result[0]).to.equal(entries[1]); | ||
expect(result[1]).to.equal(entries[2]); | ||
}); | ||
|
||
}); | ||
|
||
|
||
describe('searching', function() { | ||
|
||
const entries = [ | ||
{ id: 1, label: 'Apple' }, | ||
{ id: 2, label: 'Banana' }, | ||
{ id: 3, label: 'Cherry' }, | ||
{ id: 4, label: 'Orange' }, | ||
{ id: 5, label: 'Clementine', search: 'Mandarine Tangerine' }, | ||
{ id: 6, label: 'Pineapple', description: 'Tropical fruit' }, | ||
{ id: 7, label: 'Watermelon' } | ||
]; | ||
|
||
expectEntries('should find entries by <label> (substring)', entries, [ | ||
'Banana' | ||
], 'ban'); | ||
|
||
expectEntries('should find entries by <label>', entries, [ | ||
'Banana' | ||
], 'banana'); | ||
|
||
expectEntries('should find entries by <label> (superstring)', entries, [ | ||
'Banana' | ||
], 'bananas'); | ||
|
||
expectEntries('should find entries by <label> (below threshold)', entries, [ | ||
'Banana' | ||
], 'ananas'); | ||
|
||
expectEntries('should not find entries by <label> (above threshold)', entries, [], 'panama'); | ||
|
||
expectEntries('should find entries by <label> (rank by location)', entries, [ | ||
'Apple', | ||
'Pineapple' | ||
], 'apple'); | ||
|
||
expectEntries('shoud find entries by <description>', entries, [ | ||
'Pineapple' | ||
], 'tropical'); | ||
|
||
expectEntries('should find entries by <search>', entries, [ | ||
'Clementine' | ||
], 'mandarine'); | ||
|
||
}); | ||
|
||
}); | ||
|
||
function expectEntries(title, entries, expected, search) { | ||
it(title, function() { | ||
|
||
// when | ||
const result = searchEntries(entries, search); | ||
|
||
// then | ||
expect(result).to.have.length(expected.length); | ||
|
||
expected.forEach((label, index) => { | ||
expect(result[ index ].label).to.equal(label); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.