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.ts 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * Sanitizes the given string to improve search performance.
  21. *
  22. * This removes the case, accents, spaces and underscores.
  23. *
  24. * @param str The string to sanitize
  25. * @return {string} The sanitized string
  26. */
  27. export function sanitizeString(str: string): string {
  28. return str
  29. .toLowerCase()
  30. .normalize('NFD')
  31. .replace(/[\u0300-\u036f]/g, '')
  32. .replace(/ /g, '')
  33. .replace(/_/g, '');
  34. }
  35. /**
  36. * Checks if the given string matches the query.
  37. *
  38. * @param str The string to check
  39. * @param query The query string used to find a match
  40. * @returns {boolean}
  41. */
  42. export function stringMatchQuery(str: string, query: string): boolean {
  43. return sanitizeString(str).includes(sanitizeString(query));
  44. }
  45. /**
  46. * Checks if the given arrays have an item in common
  47. *
  48. * @param filter The filter array
  49. * @param categories The item's categories tuple
  50. * @returns {boolean} True if at least one entry is in both arrays
  51. */
  52. export function isItemInCategoryFilter(
  53. filter: Array<number>,
  54. categories: Array<number | null>,
  55. ): boolean {
  56. let itemFound = false;
  57. categories.forEach((cat: number | null) => {
  58. if (cat != null && filter.indexOf(cat) !== -1) {
  59. itemFound = true;
  60. }
  61. });
  62. return itemFound;
  63. }