{"id":22345545,"url":"https://github.com/gerhynes/type-ahead","last_synced_at":"2025-10-07T18:43:50.053Z","repository":{"id":106048140,"uuid":"105983615","full_name":"gerhynes/type-ahead","owner":"gerhynes","description":"A page created to parse and display an array of US cities. Built for Wes Bos' JavaScript 30 course. ","archived":false,"fork":false,"pushed_at":"2018-01-14T19:36:07.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-31T11:34:27.367Z","etag":null,"topics":["fetch","javascript","javascript30"],"latest_commit_sha":null,"homepage":"https://gk-hynes.github.io/type-ahead/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gerhynes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-06T08:39:46.000Z","updated_at":"2018-01-23T21:47:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"0c1c7086-4c87-4922-8956-c4e957773e48","html_url":"https://github.com/gerhynes/type-ahead","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Ftype-ahead","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Ftype-ahead/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Ftype-ahead/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Ftype-ahead/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gerhynes","download_url":"https://codeload.github.com/gerhynes/type-ahead/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245632415,"owners_count":20647194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch","javascript","javascript30"],"created_at":"2024-12-04T09:18:03.527Z","updated_at":"2025-10-07T18:43:49.950Z","avatar_url":"https://github.com/gerhynes.png","language":"JavaScript","readme":"# [Type Ahead](https://gk-hynes.github.io/type-ahead/)\n\nA page created to parse and display an array of US cities. Built for Wes Bos' [JavaScript 30](https://javascript30.com/) course.\n\n[![Screenshot of Type Ahead page](https://screenshots.firefoxusercontent.com/images/5d9f882d-22ab-44dd-9a59-4a2668a84e84.png)](https://gk-hynes.github.io/type-ahead/)\n\n## Notes\n\nThis page uses the Fetch API instead of jQuery etc.\n\nFetch returns a promise, not the data itself. Call `.then` against it and it will return a 'blob' of data.\n\nThis 'blob' doesn't know what kind of data it is. Use `blob.json().then()` to return another promise and then get the raw data from it.\n\nTo get the data into the empty `cities` array you could set `cities` as a `let` variable.\n\nAlternatively, if you want to keep cities as a `const` variable you can spread the data into it: `cities.push(...data)`.\n\nCall `filter` on the cities array and use a regex to check if the city/state matches the search. (You need the syntax below to put a variable into a regular expression.)\n\n```js\nfunction findMatches(wordToMatch, cities) {\n  return cities.filter(place =\u003e {\n    // check if city or state matches what is searched for\n    const regex = new RegExp(wordToMatch, \"gi\");\n    return place.city.match(regex) || place.state.match(regex);\n  });\n}\n```\n\nSelect the search box and suggestions, listen for a change event or keyup and run the `displayMatches` function.\n\nUse `map` to loop over the returned array and return the html you want to display. (`map` will return an array so you can call `.join('')` on the end to return a string.)\n\nTo highlight the search term in the returned city and state names, replace the word with a span with a class of hl and the matched term. Use regexes again here.\n\n```js\nconst cityName = place.city.replace(\n  regex,\n  `\u003cspan class=\"hl\"\u003e${this.value}\u003c/span\u003e`\n);\n```\n\nYou can also use regexes to format the population figure which is returned:\n\n```js\nfunction numberWithCommas(x) {\n  return x.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Ftype-ahead","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgerhynes%2Ftype-ahead","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Ftype-ahead/lists"}