Skip to content

Commit 2bec6e2

Browse files
committed
Automatically strip diacritics
This adds a dictionary for converting diacritics to a normalized representation. This dictionary is used in the default matcher. The code for stripping the diacritics was pulled from the current implementation in Select2 3.x.
1 parent 5ec201c commit 2bec6e2

9 files changed

Lines changed: 4357 additions & 13 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 863 additions & 2 deletions
Large diffs are not rendered by default.

dist/js/select2.amd.js

Lines changed: 863 additions & 2 deletions
Large diffs are not rendered by default.

dist/js/select2.full.js

Lines changed: 863 additions & 2 deletions
Large diffs are not rendered by default.

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 863 additions & 2 deletions
Large diffs are not rendered by default.

dist/js/select2.min.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples.html

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,42 @@ <h2>Example code</h2>
358358
</div>
359359
</section>
360360

361+
<section id="diacritics" class="row">
362+
<div class="col-md-4">
363+
<h1>Diacritics support</h1>
364+
365+
<p>
366+
Select2's default matcher will ignore diacritics, making it easier for
367+
users to filter results in international selects. Type "aero" into the
368+
select below.
369+
</p>
370+
371+
<p>
372+
<select class="js-example-diacritics form-control">
373+
<option>Aeróbics</option>
374+
<option>Aeróbics en Agua</option>
375+
<option>Aerografía</option>
376+
<option>Aeromodelaje</option>
377+
<option>Águilas</option>
378+
<option>Ajedrez</option>
379+
<option>Ala Delta</option>
380+
<option>Álbumes de Música</option>
381+
<option>Alusivos</option>
382+
<option>Análisis de Escritura a Mano</option>
383+
</select>
384+
</p>
385+
</div>
386+
<div class="col-md-8">
387+
<h2>Example code</h2>
388+
389+
<pre data-fill-from=".js-code-language"></pre>
390+
391+
<script type="text/x-example-code" class="js-code-language">
392+
$(".js-example-diacritics").select2();
393+
</script>
394+
</div>
395+
</section>
396+
361397
<section id="language" class="row">
362398
<div class="col-md-4">
363399
<h1>Multiple languages</h1>
@@ -388,7 +424,7 @@ <h2>Example code</h2>
388424
<script type="text/x-example-code" class="js-code-language">
389425
$(".js-example-language").select2({
390426
language: "es"
391-
})
427+
});
392428
</script>
393429
</div>
394430
</section>
@@ -500,6 +536,7 @@ <h2>Example code</h2>
500536

501537
var $matcherStart = $('.js-example-matcher-start');
502538

539+
var $diacritics = $(".js-example-diacritics");
503540
var $language = $(".js-example-language");
504541

505542
$basicSingle.select2({
@@ -590,6 +627,8 @@ <h2>Example code</h2>
590627
matcher: oldMatcher(matchStart)
591628
});
592629

630+
$diacritics.select2();
631+
593632
$language.select2({
594633
language: "es"
595634
});

src/js/select2/defaults.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ define([
88

99
'./utils',
1010
'./translation',
11+
'./diacritics',
1112

1213
'./data/select',
1314
'./data/array',
@@ -23,7 +24,7 @@ define([
2324
'./i18n/en'
2425
], function ($, ResultsList,
2526
SingleSelection, MultipleSelection, Placeholder,
26-
Utils, Translation,
27+
Utils, Translation, DIACRITICS,
2728
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
2829
Dropdown, Search, HidePlaceholder, InfiniteScroll,
2930
EnglishTranslation) {
@@ -129,6 +130,15 @@ define([
129130
};
130131

131132
Defaults.prototype.reset = function () {
133+
function stripDiacritics (text) {
134+
// Used 'uni range + named function' from http://jsperf.com/diacritics/18
135+
function match(a) {
136+
return DIACRITICS[a] || a;
137+
}
138+
139+
return text.replace(/[^\u0000-\u007E]/g, match);
140+
}
141+
132142
function matcher (params, data) {
133143
// Always return the object if there is nothing to compare
134144
if ($.trim(params.term) === '') {
@@ -162,8 +172,11 @@ define([
162172
return matcher(params, match);
163173
}
164174

175+
var original = stripDiacritics(data.text).toUpperCase();
176+
var term = stripDiacritics(params.term).toUpperCase();
177+
165178
// Check if the text contains the term
166-
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
179+
if (original.indexOf(term) > -1) {
167180
return data;
168181
}
169182

0 commit comments

Comments
 (0)