1var localitySearch = {}; 2localitySearch.storateObject = localStorage; 3localitySearch.cityKey = 'municipios-busca'; 4localitySearch.stateKey = 'estados-busca'; 5localitySearch.ibgeApiUrl = 'https://servicodados.ibge.gov.br/api/v1/localidades'; 6 7localitySearch.search = function(searchTerm) { 8 if (!searchTerm) { 9 return; 10 } 11 12 searchTerm = searchTerm.normalize('NFD').replace(/[\u0300-\u036f]/g, ""); 13 searchTerm = searchTerm.toLowerCase(); 14 15 localitySearch.loadData(); 16 17 let cities = localitySearch.storateObject.getItem(localitySearch.cityKey); 18 cities = JSON.parse(cities); 19 20 let states = localitySearch.storateObject.getItem(localitySearch.stateKey); 21 states = JSON.parse(states); 22 23 if (!cities || !states) { 24 return; 25 } 26 27 let results = []; 28 29 Object.entries(cities).forEach(([key, item]) => { 30 itemName = item.nome; 31 item.nome = item.nome.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase(); 32 33 if (item.nome.includes(searchTerm)) { 34 results.push({ 35 'type': 'city', 36 'name': itemName 37 }); 38 } 39 }); 40 41 Object.entries(states).forEach(([key, item]) => { 42 itemName = item.nome; 43 item.nome = item.nome.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase(); 44 45 if (item.nome.includes(searchTerm)) { 46 results.push({ 47 'type': 'state', 48 'name': itemName, 49 'uf': item.sigla 50 }); 51 } 52 }); 53 54 return results; 55} 56 57localitySearch.loadData = function() { 58 if (localitySearch.storateObject.getItem(localitySearch.stateKey) 59 && localitySearch.storateObject.getItem(localitySearch.cityKey) 60 ) { 61 return; 62 } 63 64 $.get(`${localitySearch.ibgeApiUrl}/municipios/`, function(data) { 65 localitySearch.storateObject.setItem(localitySearch.cityKey, JSON.stringify(data)); 66 $(document).trigger('locality.search.city.loaded'); 67 }); 68 69 $.get(`${localitySearch.ibgeApiUrl}/estados`, function(data) { 70 localitySearch.storateObject.setItem(localitySearch.stateKey, JSON.stringify(data)); 71 $(document).trigger('locality.search.state.loaded'); 72 }); 73 74 $(document).trigger('locality.search.loaded'); 75}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.