(json: object)
| 1 | const allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}[]":,.-_'; |
| 2 | |
| 3 | export const encodeSnowcode = (json: object) => { |
| 4 | const jsonStr = JSON.stringify(json).slice(1, -1); |
| 5 | let result = ''; |
| 6 | for (const char of jsonStr) { |
| 7 | // Get the index of the character in the allowedChars string. |
| 8 | const index = allowedChars.indexOf(char); |
| 9 | if (index === -1) { |
| 10 | throw new Error('Character not allowed: ' + char); |
| 11 | } |
| 12 | // Convert the index to a two-digit string (e.g., 3 -> "03"). |
| 13 | const code = index.toString().padStart(2, '0'); |
| 14 | result += code; |
| 15 | } |
| 16 | return result; |
| 17 | }; |
| 18 | |
| 19 | export const decodeSnowcode = (numStr: string) => { |
| 20 | const str = numStr.match(/\d+/)?.join('') ?? ''; |
no test coverage detected