typeguards.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  74. /**
  75. * Text nodes type guard.
  76. */
  77. export function isTextNode(node: Node): node is Text {
  78. return node.nodeType === node.TEXT_NODE;
  79. }
  80. /**
  81. * Element nodes type guard.
  82. */
  83. export function isElementNode(node: Node): node is HTMLElement {
  84. return node.nodeType === node.ELEMENT_NODE;
  85. }
  86. /**
  87. * Emoji info type guard.
  88. */
  89. export function isEmojiInfo(val: string | threema.EmojiInfo): val is threema.EmojiInfo {
  90. return typeof val === 'object'
  91. && val.emojiString !== undefined
  92. && val.imgPath !== undefined
  93. && val.codepoint !== undefined;
  94. }
  95. /**
  96. * Controller model has members.
  97. *
  98. * This returns true if the `ControllerModel` also implements the `ControllerModelWithMembers` interface.
  99. */
  100. export function controllerModelHasMembers<T extends threema.BaseReceiver>(
  101. cm: threema.ControllerModel<T>,
  102. ): cm is threema.ControllerModel<T> & threema.ControllerModelWithMembers {
  103. return cm.receiverType === 'group' || cm.receiverType === 'distributionList';
  104. }
  105. /**
  106. * Return whether this event is a keyboard event.
  107. */
  108. export function isKeyboardEvent(ev: Event): ev is KeyboardEvent {
  109. return ev.type === 'keydown' || ev.type === 'keypress' || ev.type === 'keyup';
  110. }