Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Search.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // @flow
  2. /**
  3. * Sanitizes the given string to improve search performance.
  4. *
  5. * This removes the case, accents, spaces and underscores.
  6. *
  7. * @param str The string to sanitize
  8. * @return {string} The sanitized string
  9. */
  10. export function sanitizeString(str: string): string {
  11. return str
  12. .toLowerCase()
  13. .normalize('NFD')
  14. .replace(/[\u0300-\u036f]/g, '')
  15. .replace(/ /g, '')
  16. .replace(/_/g, '');
  17. }
  18. /**
  19. * Checks if the given string matches the query.
  20. *
  21. * @param str The string to check
  22. * @param query The query string used to find a match
  23. * @returns {boolean}
  24. */
  25. export function stringMatchQuery(str: string, query: string): boolean {
  26. return sanitizeString(str).includes(sanitizeString(query));
  27. }
  28. /**
  29. * Checks if the given arrays have an item in common
  30. *
  31. * @param filter The filter array
  32. * @param categories The item's categories tuple
  33. * @returns {boolean} True if at least one entry is in both arrays
  34. */
  35. export function isItemInCategoryFilter(
  36. filter: Array<number>,
  37. categories: Array<number | null>,
  38. ): boolean {
  39. let itemFound = false;
  40. categories.forEach((cat: number | null) => {
  41. if (cat != null && filter.indexOf(cat) !== -1) itemFound = true;
  42. });
  43. return itemFound;
  44. }