Contactless accent

I tried this solution , but I got this error:

Uncaught ReferenceError: normalized undefined

Here is my code:

var charMap = {
    "à": "a", "â": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
    "ï": "i", "î": "i", "ô": "o", "ö": "o", "û": "u", "ù": "u"
};

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 spectacles = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: queryTokenizer,
    prefetch:'spectacles.json',
    limit:10,
  });
spectacles.initialize();

$('#search').typeahead({
  minLength: 1,
  hint:false,
  highlight: true
}, 
 {
  name: 'spectacles',
  displayKey: 'value',
  source: spectacles.ttAdapter()
}) ;

where is my mistake Thanks

+4
source share
2 answers

Change the normalization function so that it returns a normalized string, i.e.

var normalize = function (input) {
 $.each(charMap, function (unnormalizedChar, normalizedChar) {
    var regex = new RegExp(unnormalizedChar, 'gi');
    input = input.replace(regex, normalizedChar);
 });
 return input;
}

See this fiddle I wrote to see her work:

http://jsfiddle.net/Fresh/SL36H/

You can see the normalized line in the browser debugging console. In my example, "aaàèèèèùùù" is converted to "aaaeeeuuu".

, , (.. , char), .

+2

Bloodhound, "" "" Typeahead, .

typeahead, JavaScript, :

1 - bootstrap3-typeahead-ci.min.js

// function for making a string accent insensitive
$.fn.typeahead.Constructor.prototype.normalize = function (str) {
    // escape chars
    var normalized = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

    // map equivalent chars
    normalized = normalized.replace(/[aãáàâ]/gi, '[aãáàâ]');
    normalized = normalized.replace(/[eẽéèê]/gi, '[eẽéèê]');
    normalized = normalized.replace(/[iĩíìî]/gi, '[iĩíìî]');
    normalized = normalized.replace(/[oõóòô]/gi, '[oõóòô]');
    normalized = normalized.replace(/[uũúùû]/gi, '[uũúùû]');
    normalized = normalized.replace(/[cç]/gi, '[cç]');

    // convert string to a regular expression
    // with case insensitive mode
    normalized = new RegExp(normalized, 'gi');

    // return regular expresion
    return normalized;
}

// change 'matcher' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.matcher = function (item) {

    // get item to be evaluated
    var source = this.displayText(item);

    // make search value case insensitive
    var normalized = this.normalize(this.query);

    // search for normalized value
    return source.match(normalized);
}

// change 'highlighter' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.highlighter = function (item) {

    // get html output
    var source = this.displayText(item);

    // make search value case insensitive
    var normalized = this.normalize(this.query);

    // highlight normalized value in bold
    return source.replace(normalized, '<strong>$&</strong>');
}

2 - bootstrap3-typeahead.min.js

<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>

, , , , , , typeahead, . .

PS: Stack Overflow, , !

+4

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


All Articles