| 17 | }; |
| 18 | |
| 19 | export const decodeSnowcode = (numStr: string) => { |
| 20 | const str = numStr.match(/\d+/)?.join('') ?? ''; |
| 21 | if (str.length % 2 !== 0) { |
| 22 | throw new Error('Invalid encoded string length.'); |
| 23 | } |
| 24 | let result = ''; |
| 25 | for (let i = 0; i < str.length; i += 2) { |
| 26 | const codeStr = str.slice(i, i + 2); |
| 27 | const index = parseInt(codeStr, 10); |
| 28 | if (index < 0 || index >= allowedChars.length) { |
| 29 | throw new Error('Invalid code: ' + codeStr); |
| 30 | } |
| 31 | result += allowedChars[index]; |
| 32 | } |
| 33 | const resultStr = `{${result}}`; |
| 34 | return JSON.parse(resultStr); |
| 35 | }; |