/* * EasyAutocomplete - Data proccess module * * Process list to display: * - sort * - decrease number to specific number * - show only matching list * */ var EasyAutocomplete = (function (scope) { scope.proccess = function DataProcessor(config, listBuilder, phrase) { scope.proccess.match = match; var list = listBuilder.data, inputPhrase = phrase; // TODO REFACTOR list = findMatch(list, inputPhrase); list = reduceElementsInList(list); list = sort(list); return list; 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 (match(value, phrase)) { preparedList.push(list[i]); } } } else { preparedList = list; } return preparedList; } function match(value, phrase) { if (!config.get('list').match.caseSensitive) { if (typeof value === 'string') { value = value.toLowerCase(); } phrase = phrase.toLowerCase(); } return (config.get('list').match.method(value, phrase)); } function reduceElementsInList(list) { //MAX NUMBER OF ELEMENTS if (listBuilder.maxNumberOfElements !== undefined && list.length > listBuilder.maxNumberOfElements) { list = list.slice(0, listBuilder.maxNumberOfElements); } return list; } function sort(list) { //SORT if (config.get('list').sort.enabled) { list.sort(config.get('list').sort.method); } return list; } }; return scope; })(EasyAutocomplete || {});