Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easy-autocomplete can't compare text with accents and without accents #419

Open
eusebiomarquesbenitez opened this issue Nov 21, 2019 · 2 comments

Comments

@eusebiomarquesbenitez
Copy link

eusebiomarquesbenitez commented Nov 21, 2019

Please add a new option for normalize strings or not when compare values and phrase.

Adding in findMatch function: four lines:

function findMatch(list, phrase) {
			var preparedList = [],
				value = "";
			if (config.get("list").match.enabled) {
				for(var i = 0, length = list.length; i < length; i += 1) {
					value = config.get("getValue")(list[i]);
+					if (config.get("normalize")){
+						value=value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
+						phrase=phrase.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
+					}
					//console.log("findMatch:: value='"+value+"' phrase='"+phrase+"'");
					if (match(value, phrase)) {
						preparedList.push(list[i]);	
					}
				}
			} else {
				preparedList = list;
			}
			return preparedList;
		}

and scope.Configuration: one line of configuration:

var EasyAutocomplete = (function(scope){
	scope.Configuration = function Configuration(options) {
		var defaults = {
			data: "list-required",
			url: "list-required",
			dataType: "json",
			listLocation: function(data) {
				return data;
			},
			xmlElementName: "",
			getValue: function(element) {
				return element;
			},
			autocompleteOff: true,
			placeholder: false,
			ajaxCallback: function() {},
			matchResponseProperty: false,
			list: {
				sort: {
					enabled: false,
					method: function(a, b) {
						a = defaults.getValue(a);
						b = defaults.getValue(b);
						if (a < b) {
							return -1;
						}
						if (a > b) {
							return 1;
						}
						return 0;
					}
				},
				maxNumberOfElements: 6,
				hideOnEmptyPhrase: true,
				match: {
					enabled: false,
					caseSensitive: false,
					method: function(element, phrase) {
						if (element.search(phrase) > -1) {
							return true;
						} else {
							return false;
						}
					}
				},
				showAnimation: {
					type: "normal", //normal|slide|fade
					time: 400,
					callback: function() {}
				},
				hideAnimation: {
					type: "normal",
					time: 400,
					callback: function() {}
				},
				/* Events */
				onClickEvent: function() {},
				onSelectItemEvent: function() {},
				onLoadEvent: function() {},
				onChooseEvent: function() {},
				onKeyEnterEvent: function() {},
				onMouseOverEvent: function() {},
				onMouseOutEvent: function() {},	
				onShowListEvent: function() {},
				onHideListEvent: function() {}
			},
			highlightPhrase: true,
			theme: "",
			cssClasses: "",
			minCharNumber: 0,
			requestDelay: 0,
+			normalize:false,
			adjustWidth: true,
			ajaxSettings: {},
			preparePostData: function(data, inputPhrase) {return data;},
			loggerEnabled: true,
			template: "",
			categoriesAssigned: false,
			categories: [{
				maxNumberOfElements: 4
			}]
		};

then in var options only adding normalize:true can normalized to find the Matches.


var options = {
                data: Elements,
                requestDelay: 500,+
+              normalize:true,
                list: { ...........

thanks for all.

@fhauck
Copy link

fhauck commented Jan 29, 2020

Yes, great Idea!

Thanks @eusebiomarquesbenitez ... your solution helped me a lot! The only thing that is not working is the Highlighting, maybe someone has an idea how to customize the highlightPhrase() function?

@gabrielcramer
Copy link

gabrielcramer commented Jan 31, 2020

@eusebiomarquesbenitez You can accomplish this with the current version of the library. But keep in mind that String.prototype.normalize() is not supported by Internet Explorer.

const options = {
  data: ['ă', 'a'],
  list: {
    match: {
      enabled: true,
      method: function (element, phrase) {
        const normalizedElement=element.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
        const normalizedPhrase=phrase.normalize("NFD").replace(/[\u0300-\u036f]/g, "");

        return normalizedElement.search(normalizedPhrase) > -1;
      }
    }
  }
}

$('#search').easyAutocomplete(options);

@fhauck Regarding the highlight, maybe you don't want to highlight in this case since it's not a perfect match. But in case you do, the ability to use a custom highlightPhrase method would be a nice way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants