findLikeConsumers.js 1.16 KB
// React will search the list of all consumers that match part of its name
//
/**
 * 
 * @param {*} partOfConsumerName 
 * @param {*} todosAqui 
 */
function FindLikeConsumers(partOfConsumerName, todosAqui) {
	var partOfConsumerNameUppercase = partOfConsumerName.toUpperCase();
	return new Promise(function(resolve, reject) {
		var foundConsumers = [];
		var maximumSearchLines = 10;
		var context = { foundConsumers, maximumSearchLines, partOfConsumerNameUppercase };
		todosAqui.every(testIfContainsText, context);
		if (foundConsumers === null) {
			console.log('Não encontrou consumidores com o texto digitado.');
			reject('indefinido');
		} else {
			resolve(context.foundConsumers);
		}
	});
}
/**
 * 
 * @param {*} element 
 * @param {*} index 
 */
function testIfContainsText(element, index) {
	if (element.label.indexOf(this.partOfConsumerNameUppercase) !== -1 && this.maximumSearchLines > 0) {
		var obj = {};
		obj.label = element.label;
		obj.value = element.value;
		this.foundConsumers.push(obj);
		this.maximumSearchLines--;
		return true;
	} else {
		if (this.maximumSearchLines > 0) {
			return true;
		} else {
			return false;
		}
	}
}

export default FindLikeConsumers;