|
17 | 17 | <script> |
18 | 18 | const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json'; |
19 | 19 |
|
| 20 | +const cities = []; |
| 21 | + |
| 22 | +// Fetch will fetch whatever you pass it, and return a promise. |
| 23 | +fetch(endpoint) |
| 24 | + .then(blob => blob.json()) // When resolved, we have the data. Now we want to |
| 25 | + // turn that data into JSON. The json() method returns |
| 26 | + // anther promise. |
| 27 | + .then(data => cities.push(...data)); // When that promise resolves (we have JSON now), we want |
| 28 | + // to put that into our array using the spread operator |
| 29 | + // and push(). If you give multiple args to push, it pushes |
| 30 | + // each one onto the array. This will give us an array of the |
| 31 | + // cities that were returned. |
| 32 | + |
| 33 | +// A function to locate matches in our array of data. |
| 34 | +function findMatches(wordToMatch, cities) { |
| 35 | + // Use filter() to return a subset of cities which match the search. |
| 36 | + return cities.filter(place => { |
| 37 | + // here we need to figure out if the city or state matches what was searched |
| 38 | + const regex = new RegExp(wordToMatch, 'gi'); |
| 39 | + // check if this regex (the search string) matches against the current city |
| 40 | + // or state. `place` is the entry to match. |
| 41 | + // If either of these matches, the function returns true, therefore this |
| 42 | + // entry will be included in the filtered set. |
| 43 | + return place.city.match(regex) || place.state.match(regex); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +// Format the number nicely with commas |
| 48 | +function numberWithCommas(x) { |
| 49 | + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); |
| 50 | +} |
| 51 | + |
| 52 | +// Display whatever matches we have found |
| 53 | +function displayMatches() { |
| 54 | + // Get the matches using findMatches |
| 55 | + const matchArray = findMatches(this.value, cities); |
| 56 | + // Build a little HTML to insert for each match. |
| 57 | + // Assign to each element using map() |
| 58 | + // Make one string instead of array using join() |
| 59 | + const html = matchArray.map(place => { |
| 60 | + // Use a regex to replace the portion of the data that matches the search |
| 61 | + // string with a span of class "hl". This will highlight the part of the |
| 62 | + // word which was the search, and leave the rest as regular text. |
| 63 | + const regex = new RegExp(this.value, 'gi'); |
| 64 | + const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`) |
| 65 | + // Use cityName instead of place.city in this (to get the matched data). |
| 66 | + // Same for state. |
| 67 | + const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`) |
| 68 | + return ` |
| 69 | + <li> |
| 70 | + <span class="name">${cityName}, ${stateName}</span> |
| 71 | + <span class="population">${numberWithCommas(place.population)}</span> |
| 72 | + </li> |
| 73 | + `; |
| 74 | + }).join(''); |
| 75 | + // And stick this into the DOM as an HTML node |
| 76 | + suggestions.innerHTML = html; |
| 77 | +} |
| 78 | + |
| 79 | +// Grab a couple of DOM nodes... |
| 80 | +const searchInput = document.querySelector('.search'); |
| 81 | +const suggestions = document.querySelector('.suggestions'); |
| 82 | + |
| 83 | +// Event listener - when the input box changes, run the findMatches function |
| 84 | +searchInput.addEventListener('change', displayMatches); |
| 85 | +searchInput.addEventListener('keyup', displayMatches); |
| 86 | + |
20 | 87 | </script> |
21 | 88 | </body> |
22 | 89 | </html> |
0 commit comments