Merge pull request #3412 from hashicorp/b-ui-disable-fuzzy-search

Disable fuzzy search
This commit is contained in:
Michael Lange 2017-10-18 20:08:15 -07:00 committed by GitHub
commit 79c836d734
6 changed files with 13 additions and 57 deletions

View file

@ -20,7 +20,7 @@ export default Controller.extend(Sortable, Searchable, {
sortProperty: 'modifyIndex', sortProperty: 'modifyIndex',
sortDescending: true, sortDescending: true,
searchProps: computed(() => ['id', 'name']), searchProps: computed(() => ['shortId', 'name']),
allocations: computed('model.allocations.[]', function() { allocations: computed('model.allocations.[]', function() {
return this.get('model.allocations') || []; return this.get('model.allocations') || [];

View file

@ -18,7 +18,7 @@ export default Controller.extend(Sortable, Searchable, {
sortProperty: 'modifyIndex', sortProperty: 'modifyIndex',
sortDescending: true, sortDescending: true,
searchProps: computed(() => ['id', 'name']), searchProps: computed(() => ['shortId', 'name']),
listToSort: computed.alias('model.allocations'), listToSort: computed.alias('model.allocations'),
listToSearch: computed.alias('listSorted'), listToSearch: computed.alias('listSorted'),

View file

@ -1,5 +1,4 @@
import Ember from 'ember'; import Ember from 'ember';
import Fuse from 'npm:fuse.js';
const { Mixin, computed, get } = Ember; const { Mixin, computed, get } = Ember;
@ -14,46 +13,26 @@ const { Mixin, computed, get } = Ember;
- listToSearch: the list of objects to search - listToSearch: the list of objects to search
Properties provided: Properties provided:
- listSearched: a subset of listToSearch of items that meet the search criteria, - listSearched: a subset of listToSearch of items that meet the search criteria
ordered by relevance.
*/ */
export default Mixin.create({ export default Mixin.create({
searchTerm: '', searchTerm: '',
listToSearch: computed(() => []), listToSearch: computed(() => []),
searchProps: null, searchProps: null,
fuse: computed('listToSearch.[]', 'searchProps.[]', function() { listSearched: computed('searchTerm', 'listToSearch.[]', 'searchProps.[]', function() {
return new Fuse(this.get('listToSearch'), { const searchTerm = this.get('searchTerm');
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: this.get('searchProps') || [],
getFn(item, key) {
return get(item, key);
},
});
}),
listSearched: computed('fuse', 'searchTerm', function() {
const { fuse, searchTerm } = this.getProperties('fuse', 'searchTerm');
if (searchTerm && searchTerm.length) { if (searchTerm && searchTerm.length) {
if (searchTerm.startsWith('/')) { return regexSearch(searchTerm, this.get('listToSearch'), this.get('searchProps'));
return regexSearch(searchTerm, fuse);
}
return fuse.search(searchTerm);
} }
return this.get('listToSearch'); return this.get('listToSearch');
}), }),
}); });
function regexSearch(term, { list, options: { keys } }) { function regexSearch(term, list, keys) {
const regexStr = term.slice(1); if (term.length) {
if (regexStr.length) {
try { try {
const regex = new RegExp(regexStr, 'i'); const regex = new RegExp(term, 'i');
// Test the value of each key for each object against the regex // Test the value of each key for each object against the regex
// All that match are returned. // All that match are returned.
return list.filter(item => keys.some(key => regex.test(get(item, key)))); return list.filter(item => keys.some(key => regex.test(get(item, key))));

View file

@ -20,10 +20,7 @@
"prettier --single-quote --trailing-comma es5 --print-width 100 --write", "prettier --single-quote --trailing-comma es5 --print-width 100 --write",
"git add" "git add"
], ],
"ui/app/styles/**/*.*": [ "ui/app/styles/**/*.*": ["prettier --write", "git add"]
"prettier --write",
"git add"
]
} }
}, },
"devDependencies": { "devDependencies": {
@ -69,7 +66,6 @@
"ember-welcome-page": "^3.0.0", "ember-welcome-page": "^3.0.0",
"eslint": "^4.0.0", "eslint": "^4.0.0",
"flat": "^2.0.1", "flat": "^2.0.1",
"fuse.js": "^3.0.5",
"husky": "^0.13.4", "husky": "^0.13.4",
"json-formatter-js": "^2.2.0", "json-formatter-js": "^2.2.0",
"lint-staged": "^3.6.1", "lint-staged": "^3.6.1",
@ -81,9 +77,6 @@
}, },
"private": true, "private": true,
"ember-addon": { "ember-addon": {
"paths": [ "paths": ["lib/bulma", "lib/calendar"]
"lib/bulma",
"lib/calendar"
]
} }
} }

View file

@ -24,18 +24,6 @@ test('the searchable mixin does nothing when there is no search term', function(
assert.deepEqual(subject.get('listSearched'), subject.get('source')); assert.deepEqual(subject.get('listSearched'), subject.get('source'));
}); });
test('the searchable mixin allows for fuzzy match search', function(assert) {
const subject = this.subject();
subject.set('source', [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }]);
subject.set('searchTerm', 'helo');
assert.deepEqual(
subject.get('listSearched'),
[{ id: '1', name: 'hello' }],
'hello matched for the term helo'
);
});
test('the searchable mixin allows for regex search', function(assert) { test('the searchable mixin allows for regex search', function(assert) {
const subject = this.subject(); const subject = this.subject();
subject.set('source', [ subject.set('source', [
@ -44,7 +32,7 @@ test('the searchable mixin allows for regex search', function(assert) {
{ id: '3', name: 'oranges' }, { id: '3', name: 'oranges' },
]); ]);
subject.set('searchTerm', '/.+l+[A-Z]$'); subject.set('searchTerm', '.+l+[A-Z]$');
assert.deepEqual( assert.deepEqual(
subject.get('listSearched'), subject.get('listSearched'),
[{ id: '1', name: 'hello' }, { id: '2', name: 'world' }], [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }],
@ -60,7 +48,7 @@ test('the searchable mixin only searches the declared search props', function(as
{ id: '3', name: 'Mexico', continent: 'North America' }, { id: '3', name: 'Mexico', continent: 'North America' },
]); ]);
subject.set('searchTerm', '/America'); subject.set('searchTerm', 'America');
assert.deepEqual( assert.deepEqual(
subject.get('listSearched'), subject.get('listSearched'),
[{ id: '1', name: 'United States of America', continent: 'North America' }], [{ id: '1', name: 'United States of America', continent: 'North America' }],

View file

@ -4086,10 +4086,6 @@ functional-red-black-tree@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327" resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
fuse.js@^3.0.5:
version "3.0.5"
resolved "https://registry.yarnpkg.com/fuse.js/-/fuse.js-3.0.5.tgz#b58d85878802321de94461654947b93af1086727"
gauge@~2.7.3: gauge@~2.7.3:
version "2.7.4" version "2.7.4"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7" resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"