Created
August 8, 2019 21:35
-
-
Save agm1984/6fdcbe95c0388ff49f65349e59548f9d to your computer and use it in GitHub Desktop.
Maybe you have an autocomplete field and you want to filter a list to case insensitive matches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// collect a haystack | |
const countries = [ | |
'Afghanistan', | |
'Åland Islands', | |
'Albania', | |
'Algeria', | |
'American Samoa', | |
'Andorra', | |
'Angola', | |
'Anguilla', | |
'Antarctica', | |
'Antigua and Barbuda', | |
'Argentina', | |
'Armenia', | |
'Aruba', | |
'Australia', | |
'Austria', | |
'Azerbaijan', | |
'Bahamas', | |
'Bahrain', | |
'Bangladesh', | |
'Barbados', | |
'Belarus', | |
'Belgium', | |
'Belize', | |
'Benin', | |
'Bermuda', | |
'Bhutan', | |
'Bolivia, Plurinational State of', | |
'Bonaire, Sint Eustatius and Saba', | |
'Bosnia and Herzegovina', | |
'Botswana', | |
'Bouvet Island', | |
'Brazil', | |
'British Indian Ocean Territory', | |
'Brunei Darussalam', | |
'Bulgaria', | |
'Burkina Faso', | |
'Burundi', | |
'Cambodia', | |
'Cameroon', | |
'Canada', | |
'Cape Verde', | |
'Cayman Islands', | |
'Central African Republic', | |
'Chad', | |
'Chile', | |
'China', | |
'Christmas Island', | |
'Cocos (Keeling) Islands', | |
'Colombia', | |
'Comoros', | |
'Congo', | |
'Congo, the Democratic Republic of the', | |
'Cook Islands', | |
'Costa Rica', | |
'Côte d\'Ivoire', | |
'Croatia', | |
'Cuba', | |
'Curaçao', | |
'Cyprus', | |
'Czech Republic', | |
'Denmark', | |
'Djibouti', | |
'Dominica', | |
'Dominican Republic', | |
'Ecuador', | |
'Egypt', | |
'El Salvador', | |
'Equatorial Guinea', | |
'Eritrea', | |
'Estonia', | |
'Ethiopia', | |
'Falkland Islands (Malvinas)', | |
'Faroe Islands', | |
'Fiji', | |
'Finland', | |
'France', | |
'French Guiana', | |
'French Polynesia', | |
'French Southern Territories', | |
'Gabon', | |
'Gambia', | |
'Georgia', | |
'Germany', | |
'Ghana', | |
'Gibraltar', | |
'Greece', | |
'Greenland', | |
'Grenada', | |
'Guadeloupe', | |
'Guam', | |
'Guatemala', | |
'Guernsey', | |
'Guinea', | |
'Guinea-Bissau', | |
'Guyana', | |
'Haiti', | |
'Heard Island and McDonald Islands', | |
'Holy See (Vatican City State)', | |
'Honduras', | |
'Hong Kong', | |
]; | |
// define a needle | |
const searchTerm = 'can'; | |
// sprinkle bonus sauce on the needle | |
const lowerCaseSearchTerm = searchTerm.toLowerCase().trim(); | |
// define predicate function | |
const hasMatchForSearchTerm = c => (c && (c.toString().toLowerCase().indexOf(searchTerm.toLowerCase()) >= 0)); | |
// collect filtered haystack | |
const filteredCountries = countries.filter(hasMatchForSearchTerm); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment