Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SAS.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* eslint-disable no-bitwise */
  2. /* eslint-disable no-mixed-operators */
  3. /**
  4. * Generates a SAS composed of decimal numbers.
  5. * Borrowed from the Matrix JS SDK.
  6. *
  7. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  8. * @returns Array<number>
  9. */
  10. function generateDecimalSas(sasBytes) {
  11. /**
  12. * +--------+--------+--------+--------+--------+
  13. * | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
  14. * +--------+--------+--------+--------+--------+
  15. * bits: 87654321 87654321 87654321 87654321 87654321
  16. * \____________/\_____________/\____________/
  17. * 1st number 2nd number 3rd number
  18. */
  19. return [
  20. (sasBytes[0] << 5 | sasBytes[1] >> 3) + 1000,
  21. ((sasBytes[1] & 0x7) << 10 | sasBytes[2] << 2 | sasBytes[3] >> 6) + 1000,
  22. ((sasBytes[3] & 0x3f) << 7 | sasBytes[4] >> 1) + 1000
  23. ];
  24. }
  25. const emojiMapping = [
  26. [ '🐶', 'dog' ],
  27. [ '🐱', 'cat' ],
  28. [ '🦁', 'lion' ],
  29. [ '🐎', 'horse' ],
  30. [ '🦄', 'unicorn' ],
  31. [ '🐷', 'pig' ],
  32. [ '🐘', 'elephant' ],
  33. [ '🐰', 'rabbit' ],
  34. [ '🐼', 'panda' ],
  35. [ '🐓', 'rooster' ],
  36. [ '🐧', 'penguin' ],
  37. [ '🐢', 'turtle' ],
  38. [ '🐟', 'fish' ],
  39. [ '🐙', 'octopus' ],
  40. [ '🦋', 'butterfly' ],
  41. [ '🌷', 'flower' ],
  42. [ '🌳', 'tree' ],
  43. [ '🌵', 'cactus' ],
  44. [ '🍄', 'mushroom' ],
  45. [ '🌏', 'globe' ],
  46. [ '🌙', 'moon' ],
  47. [ '☁️', 'cloud' ],
  48. [ '🔥', 'fire' ],
  49. [ '🍌', 'banana' ],
  50. [ '🍎', 'apple' ],
  51. [ '🍓', 'strawberry' ],
  52. [ '🌽', 'corn' ],
  53. [ '🍕', 'pizza' ],
  54. [ '🎂', 'cake' ],
  55. [ '❤️', 'heart' ],
  56. [ '🙂', 'smiley' ],
  57. [ '🤖', 'robot' ],
  58. [ '🎩', 'hat' ],
  59. [ '👓', 'glasses' ],
  60. [ '🔧', 'spanner' ],
  61. [ '🎅', 'santa' ],
  62. [ '👍', 'thumbs up' ],
  63. [ '☂️', 'umbrella' ],
  64. [ '⌛', 'hourglass' ],
  65. [ '⏰', 'clock' ],
  66. [ '🎁', 'gift' ],
  67. [ '💡', 'light bulb' ],
  68. [ '📕', 'book' ],
  69. [ '✏️', 'pencil' ],
  70. [ '📎', 'paperclip' ],
  71. [ '✂️', 'scissors' ],
  72. [ '🔒', 'lock' ],
  73. [ '🔑', 'key' ],
  74. [ '🔨', 'hammer' ],
  75. [ '☎️', 'telephone' ],
  76. [ '🏁', 'flag' ],
  77. [ '🚂', 'train' ],
  78. [ '🚲', 'bicycle' ],
  79. [ '✈️', 'aeroplane' ],
  80. [ '🚀', 'rocket' ],
  81. [ '🏆', 'trophy' ],
  82. [ '⚽', 'ball' ],
  83. [ '🎸', 'guitar' ],
  84. [ '🎺', 'trumpet' ],
  85. [ '🔔', 'bell' ],
  86. [ '⚓️', 'anchor' ],
  87. [ '🎧', 'headphones' ],
  88. [ '📁', 'folder' ],
  89. [ '📌', 'pin' ]
  90. ];
  91. /**
  92. * Generates a SAS composed of defimal numbers.
  93. * Borrowed from the Matrix JS SDK.
  94. *
  95. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  96. * @returns Array<number>
  97. */
  98. function generateEmojiSas(sasBytes) {
  99. // Just like base64.
  100. const emojis = [
  101. sasBytes[0] >> 2,
  102. (sasBytes[0] & 0x3) << 4 | sasBytes[1] >> 4,
  103. (sasBytes[1] & 0xf) << 2 | sasBytes[2] >> 6,
  104. sasBytes[2] & 0x3f,
  105. sasBytes[3] >> 2,
  106. (sasBytes[3] & 0x3) << 4 | sasBytes[4] >> 4,
  107. (sasBytes[4] & 0xf) << 2 | sasBytes[5] >> 6
  108. ];
  109. return emojis.map(num => emojiMapping[num]);
  110. }
  111. const sasGenerators = {
  112. decimal: generateDecimalSas,
  113. emoji: generateEmojiSas
  114. };
  115. /**
  116. * Generates multiple SAS for the given bytes.
  117. *
  118. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  119. * @returns {object}
  120. */
  121. export function generateSas(sasBytes) {
  122. const sas = {};
  123. for (const method in sasGenerators) {
  124. if (sasGenerators.hasOwnProperty(method)) {
  125. sas[method] = sasGenerators[method](sasBytes);
  126. }
  127. }
  128. return sas;
  129. }