Selaa lähdekoodia

feat(ts) migrate SAS to TS

dev0
Yash 6 kuukautta sitten
vanhempi
commit
709052d8b2
No account linked to committer's email address
1 muutettua tiedostoa jossa 14 lisäystä ja 12 poistoa
  1. 14
    12
      modules/e2ee/SAS.ts

modules/e2ee/SAS.js → modules/e2ee/SAS.ts Näytä tiedosto

@@ -8,7 +8,7 @@
8 8
  * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
9 9
  * @returns Array<number>
10 10
  */
11
-function generateDecimalSas(sasBytes) {
11
+function generateDecimalSas(sasBytes: Uint8Array): number[] {
12 12
     /**
13 13
      *      +--------+--------+--------+--------+--------+
14 14
      *      | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
@@ -24,7 +24,7 @@ function generateDecimalSas(sasBytes) {
24 24
     ];
25 25
 }
26 26
 
27
-const emojiMapping = [
27
+const emojiMapping: [string, string][] = [
28 28
     [ '🐶', 'dog' ],
29 29
     [ '🐱', 'cat' ],
30 30
     [ '🦁', 'lion' ],
@@ -98,7 +98,7 @@ const emojiMapping = [
98 98
  * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
99 99
  * @returns Array<number>
100 100
  */
101
-function generateEmojiSas(sasBytes) {
101
+function generateEmojiSas(sasBytes: Uint8Array): [string, string][] {
102 102
     // Just like base64.
103 103
     const emojis = [
104 104
         sasBytes[0] >> 2,
@@ -113,25 +113,27 @@ function generateEmojiSas(sasBytes) {
113 113
     return emojis.map(num => emojiMapping[num]);
114 114
 }
115 115
 
116
-const sasGenerators = {
116
+const sasGenerators: { [key: string]: (sasBytes: Uint8Array) => number[] | [string, string][]; } = {
117 117
     decimal: generateDecimalSas,
118 118
     emoji: generateEmojiSas
119 119
 };
120 120
 
121
+export interface ISas {
122
+    [key: string]: number[] | [string, string][];
123
+}
124
+
121 125
 /**
122 126
  * Generates multiple SAS for the given bytes.
123 127
  *
124 128
  * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
125
- * @returns {object}
129
+ * @returns {ISas}
126 130
  */
127
-export function generateSas(sasBytes) {
128
-    const sas = {};
131
+export function generateSas(sasBytes: Uint8Array): ISas {
132
+    const sas: ISas = {};
129 133
 
130
-    for (const method in sasGenerators) {
131
-        if (sasGenerators.hasOwnProperty(method)) {
132
-            sas[method] = sasGenerators[method](sasBytes);
133
-        }
134
-    }
134
+    Object.keys(sasGenerators).forEach(method => {
135
+        sas[method] = sasGenerators[method](sasBytes);
136
+    });
135 137
 
136 138
     return sas;
137 139
 }

Loading…
Peruuta
Tallenna