benjamincharity
9/27/2019 - 5:24 PM

Search for characters in a string without requiring them to be consecutive

public queryStates(query: string): State[] {
  query = query.toLowerCase();

  if (query) {
    const letters = query.split('').map(l => `${l}.*`).join('');
    const regex = new RegExp(letters, 'ig');
    return this.states.filter(s => !!s.name.match(regex));
  } else {
    // if no query, return first 10 states
    return STATES.slice(0, 10);
  }
}