typeguards.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // User defined type guards are small functions that determine the type of an object.
  18. // See https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
  19. // for more information.
  20. /**
  21. * Contact receiver type guard
  22. */
  23. export function isContactReceiver(
  24. receiver: threema.BaseReceiver,
  25. ): receiver is threema.ContactReceiver {
  26. return receiver.type === 'contact';
  27. }
  28. /**
  29. * Group receiver type guard
  30. */
  31. export function isGroupReceiver(
  32. receiver: threema.BaseReceiver,
  33. ): receiver is threema.GroupReceiver {
  34. return receiver.type === 'group';
  35. }
  36. /**
  37. * Distribution list receiver type guard
  38. */
  39. export function isDistributionListReceiver(
  40. receiver: threema.BaseReceiver,
  41. ): receiver is threema.DistributionListReceiver {
  42. return receiver.type === 'distributionList';
  43. }
  44. /**
  45. * Valid receiver types type guard
  46. */
  47. export function isValidReceiverType(
  48. receiverType: string,
  49. ): receiverType is threema.ReceiverType {
  50. switch (receiverType) {
  51. case 'me':
  52. case 'contact':
  53. case 'group':
  54. case 'distributionList':
  55. return true;
  56. }
  57. return false;
  58. }
  59. /**
  60. * Valid disconnect reasons type guard
  61. */
  62. export function isValidDisconnectReason(
  63. reason: string,
  64. ): reason is threema.DisconnectReason {
  65. switch (reason) {
  66. case 'stop':
  67. case 'delete':
  68. case 'disable':
  69. case 'replace':
  70. return true;
  71. }
  72. return false;
  73. }