From 8e2147d0d69c21bffdd5e119315b8bcac3978314 Mon Sep 17 00:00:00 2001 From: Sjoerd Date: Sat, 21 Feb 2015 11:50:43 +0100 Subject: [PATCH 1/3] Put functions in matches array instead of regexes --- jquery.select-to-autocomplete.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jquery.select-to-autocomplete.js b/jquery.select-to-autocomplete.js index 9791c6d..f6f620c 100755 --- a/jquery.select-to-autocomplete.js +++ b/jquery.select-to-autocomplete.js @@ -186,9 +186,11 @@ THE SOFTWARE. for (var i=0; i < split_term.length; i++) { if ( split_term[i].length > 0 ) { var matcher = {}; - matcher['partial'] = new RegExp( $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + var partial = new RegExp( $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + matcher['partial'] = partial.test.bind(partial); if ( context.settings['relevancy-sorting'] ) { - matcher['strict'] = new RegExp( "^" + $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + var strict = new RegExp( "^" + $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); + matcher['strict'] = strict.test.bind(strict); } matchers.push( matcher ); } @@ -201,12 +203,12 @@ THE SOFTWARE. var split_option_matches = option.matches.split(' '); } for ( var i=0; i < matchers.length; i++ ) { - if ( matchers[i]['partial'].test( option.matches ) ) { + if ( matchers[i]['partial']( option.matches ) ) { partial_matches++; } if ( context.settings['relevancy-sorting'] ) { for (var q=0; q < split_option_matches.length; q++) { - if ( matchers[i]['strict'].test( split_option_matches[q] ) ) { + if ( matchers[i]['strict']( split_option_matches[q] ) ) { strict_match = true; break; } From 125c801c148921cfcb54c67622e7c2a4e7f4fc53 Mon Sep 17 00:00:00 2001 From: Sjoerd Date: Sat, 21 Feb 2015 12:00:11 +0100 Subject: [PATCH 2/3] Get matchers from functions --- jquery.select-to-autocomplete.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/jquery.select-to-autocomplete.js b/jquery.select-to-autocomplete.js index f6f620c..12f5538 100755 --- a/jquery.select-to-autocomplete.js +++ b/jquery.select-to-autocomplete.js @@ -179,6 +179,16 @@ THE SOFTWARE. var adapters = { jquery_ui: function( context ) { + function create_str_contains(term) { + var partial = new RegExp( $.ui.autocomplete.escapeRegex( term ), "i" ); + return partial.test.bind(partial); + } + + function create_str_startswith(term) { + var strict = new RegExp( "^" + $.ui.autocomplete.escapeRegex( term ), "i" ); + return strict.test.bind(strict); + } + // loose matching of search terms var filter_options = function( term ) { var split_term = term.split(' '); @@ -186,11 +196,9 @@ THE SOFTWARE. for (var i=0; i < split_term.length; i++) { if ( split_term[i].length > 0 ) { var matcher = {}; - var partial = new RegExp( $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); - matcher['partial'] = partial.test.bind(partial); + matcher['partial'] = create_str_contains( split_term[i] ); if ( context.settings['relevancy-sorting'] ) { - var strict = new RegExp( "^" + $.ui.autocomplete.escapeRegex( split_term[i] ), "i" ); - matcher['strict'] = strict.test.bind(strict); + matcher['strict'] = create_str_startswith( split_term[i] ); } matchers.push( matcher ); } From b71cc4a00425a23e0d4079f0907245e477475670 Mon Sep 17 00:00:00 2001 From: Sjoerd Date: Sat, 21 Feb 2015 12:13:24 +0100 Subject: [PATCH 3/3] Use Intl.Collator to implement contains and startswith --- jquery.select-to-autocomplete.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/jquery.select-to-autocomplete.js b/jquery.select-to-autocomplete.js index 12f5538..346c089 100755 --- a/jquery.select-to-autocomplete.js +++ b/jquery.select-to-autocomplete.js @@ -41,6 +41,8 @@ THE SOFTWARE. 'minLength': 0, 'delay': 0, 'autoFocus': true, + 'locale': 'en', + 'sensitivity': 'base', handle_invalid_input: function( context ) { var selected_finder = 'option:selected:first'; if ( context.settings['remove-valueless-options'] ) { @@ -180,13 +182,28 @@ THE SOFTWARE. var adapters = { jquery_ui: function( context ) { function create_str_contains(term) { - var partial = new RegExp( $.ui.autocomplete.escapeRegex( term ), "i" ); - return partial.test.bind(partial); + var locale = context.settings['locale']; + var sensitivity = context.settings['sensitivity']; + var collator = Intl.Collator(locale, {'sensitivity': sensitivity}); + return function (haystack) { + for (var i = 0; i < 1 + haystack.length - term.length; i++) { + var first_part = haystack.slice(i, i + term.length); + if (collator.compare(first_part, term) === 0) { + return true; + } + } + return false; + }; } function create_str_startswith(term) { - var strict = new RegExp( "^" + $.ui.autocomplete.escapeRegex( term ), "i" ); - return strict.test.bind(strict); + var locale = context.settings['locale']; + var sensitivity = context.settings['sensitivity']; + var collator = Intl.Collator(locale, {'sensitivity': sensitivity}); + return function (haystack) { + var first_part = haystack.slice(0, term.length); + return collator.compare(first_part, term) === 0; + }; } // loose matching of search terms