findLikeConsumers.js
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 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;