Translite query string for typeahead.js

I am using typeaheadjs and I would like to transliterate the search query -before- it is sent to the server.

@Edvad Zagorski gave an excellent php array for this: stack overflow

But I will need to do the same in the beforeSend () method. Therefore, if the user starts typing something like

čikago

he sent

cikago

to the server.

I tried countless branch tricks: json_encode, raw, url_encode ... Nothing worked since I really don't get these encodings.

Is it possible? I think the problem is that the beforeSend () method gets urlEncodedQuery, not the real one.

0
source share
2 answers

, GitHub:

var charMap = {'àáâããäå': 'a', 'èéêë': 'e', 'ç': 'c', 'ß': 'ss', /* ... */};

var normalize = function(str) {
  $.each(charMap, function(chars, normalized) {
    var regex = new RegExp('[' + chars + ']', 'gi');
    str = str.replace(regex, normalized);
  });

  return normalized;
}

var queryTokenizer = function(q) {
  var normalized = normalize(q);
  return Bloodhound.tokenizers.whitespace(normalized);
};

var engine = new Bloodhound({
  // ...
  queryTokenizer: queryTokenizer
});
0
0

Source: https://habr.com/ru/post/1532174/


All Articles