diff --git a/README.md b/README.md index 3dbb224..f471862 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,11 @@ Or include Choices directly: flippedState: 'is-flipped', loadingState: 'is-loading', }, + // Choices uses the great Fuse library for searching. You + // can find more options here: https://github.com/krisk/Fuse#options + fuseOptions: { + include: 'score', + }, callbackOnInit: null, callbackOnCreateTemplates: null, }); diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index c6b858b..b890c10 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -105,6 +105,9 @@ class Choices { flippedState: 'is-flipped', loadingState: 'is-loading', }, + fuseOptions: { + include: 'score', + }, callbackOnInit: null, callbackOnCreateTemplates: null, }; @@ -1138,12 +1141,10 @@ class Choices { const haystack = this.store.getChoicesFilteredBySelectable(); const needle = newValue; const keys = isType('Array', this.config.sortFields) ? this.config.sortFields : [this.config.sortFields]; - const fuse = new Fuse(haystack, { - keys, - shouldSort: true, - include: 'score', - }); + const options = Object.assign(this.config.fuseOptions, { keys }); + const fuse = new Fuse(haystack, options); const results = fuse.search(needle); + this.currentValue = newValue; this.highlightPosition = 0; this.isSearching = true; diff --git a/tests/spec/choices_spec.js b/tests/spec/choices_spec.js index e11d144..cd17478 100644 --- a/tests/spec/choices_spec.js +++ b/tests/spec/choices_spec.js @@ -2,6 +2,30 @@ import 'whatwg-fetch'; import 'es6-promise'; import Choices from '../../assets/scripts/src/choices.js'; +if (typeof Object.assign != 'function') { + Object.assign = function (target, varArgs) { // .length of function is 2 + if (target == null) { // TypeError if undefined or null + throw new TypeError('Cannot convert undefined or null to object'); + } + + var to = Object(target); + + for (var index = 1; index < arguments.length; index++) { + var nextSource = arguments[index]; + + if (nextSource != null) { // Skip over if undefined or null + for (var nextKey in nextSource) { + // Avoid bugs when hasOwnProperty is shadowed + if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { + to[nextKey] = nextSource[nextKey]; + } + } + } + } + return to; + }; +} + describe('Choices', () => { afterEach(function() {