Select2 custom matcher, but keep stripDiacritics
Asked Answered
S

1

8

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.

Sanctuary answered 22/2, 2016 at 15:25 Comment(1)
you did't provide your code, if you add then it is better for the viewer to understand and may be someone give the solution.Lustral
P
9

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
  })
});
Pleven answered 1/3, 2016 at 14:54 Comment(4)
Thanks, this is closer to what I was looking? Would you consider an oversight the fact that both DIACRITICS and the default matcher are public but stripDiacritics is not an oversight/something that should be fixed?? I don't know how familiar you are with select2, I'm asking this more from a JavaScript developer perspective?Bremser
Select2's codebase changed a lot between 3.5.4 and 4.0.0. I searched the commit messages for something like "making stripDiacritics private because X" but haven't found anything. Therefore I think it was a mistake. You can always start a discussion by opening an issue in their repository If it bothers you.Leone
Thanks @jannis, I will! I wasn't sure, as Javascript isn't my stronger, but I will follow my gut and your advice and start a discussion there. Thanks for your help.Bremser
@Leito, did you manage eventually? I am using v 4.0.6 and whenever I use this code my custom matcher is disregarded...Pycnometer

© 2022 - 2024 — McMap. All rights reserved.