Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 custom matcher, but keep stripDiacritics

I know in previous versions of select2, stripDiacritics was exported so it was accessible outside of the default matcher. In the current version, 4.0.1 it seems impossible to write a custom matcher and using select2's stripDiacritics, since is not exported in $.fn.select2.defaults

What's the best way I can pull the original stripDiacritics for me to write a custom matcher?

I'm trying to write a matcher that matches both the option's text and a data attribute. I'm trying to avoid patching select2's source, I imagine it will be nightmare if I follow that path.

Update

I've highlighted the actual question and posted additional details around the code I'm working with, as suggested in the comments:

I'm not asking for someone to write the new matcher (based on $.fn.select2.defaults.defaults.matcher) for me, I'm simply asking for the best way to pull the original stripDiacritics which is private, so that I can use it.

I could, simply copy the function (and it's dependencies: the DIACRITICS object) to my code, but that's the hole I'm trying to avoid.

like image 789
Leonel Galán Avatar asked Feb 22 '16 15:02

Leonel Galán


1 Answers

Since stripDiacriticss is a private method, there's not much you can do about it (other than parsing the source code).

However, if you're happy with copying only the stripDiacritics method from the select2 codebase and relying on select2 to provide the DIACRITICS dependency, you can always just require the 'select2/diacritics' module:

$.fn.select2.amd.require(['select2/diacritics'], function (DIACRITICS) {
  // stripDiacritics code copied from select2
  function stripDiacritics (text) {
    // Used 'uni range + named function' from http://jsperf.com/diacritics/18
    function match(a) {
      return DIACRITICS[a] || a;
    }

    return text.replace(/[^\u0000-\u007E]/g, match);
  }

  $(".awesome").select2({
    // declare your matcher code here with access to stripDiacritics
  })
});
like image 128
weeniearms Avatar answered Oct 29 '22 09:10

weeniearms