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.

Services.ts 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * Gets the given services list without items of the given ids
  21. *
  22. * @param idList The ids of items to remove
  23. * @param sourceList The item list to use as source
  24. * @returns {[]}
  25. */
  26. export default function getStrippedServicesList<T extends {key: string}>(
  27. idList: Array<string>,
  28. sourceList: Array<T>,
  29. ) {
  30. const newArray: Array<T> = [];
  31. sourceList.forEach((item: T) => {
  32. if (!idList.includes(item.key)) {
  33. newArray.push(item);
  34. }
  35. });
  36. return newArray;
  37. }
  38. /**
  39. * Gets a sublist of the given list with items of the given ids only
  40. *
  41. * The given list must have a field id or key
  42. *
  43. * @param idList The ids of items to find
  44. * @param originalList The original list
  45. * @returns {[]}
  46. */
  47. export function getSublistWithIds<T extends {key: string}>(
  48. idList: Array<string>,
  49. originalList: Array<T>,
  50. ) {
  51. const subList: Array<T | null> = [];
  52. for (let i = 0; i < idList.length; i += 1) {
  53. subList.push(null);
  54. }
  55. let itemsAdded = 0;
  56. for (let i = 0; i < originalList.length; i += 1) {
  57. const item = originalList[i];
  58. if (idList.includes(item.key)) {
  59. subList[idList.indexOf(item.key)] = item;
  60. itemsAdded += 1;
  61. if (itemsAdded === idList.length) {
  62. break;
  63. }
  64. }
  65. }
  66. return subList;
  67. }