MCPcopy Index your code
hub / github.com/FxEmbed/FxEmbed / encodeSnowcode

Function encodeSnowcode

src/helpers/snowcode.ts:3–17  ·  view source on GitHub ↗
(json: object)

Source from the content-addressed store, hash-verified

1const allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}[]":,.-_';
2
3export 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
19export const decodeSnowcode = (numStr: string) => {
20 const str = numStr.match(/\d+/)?.join('') ?? '';

Callers 1

handleStatusFunction · 0.90

Calls 2

sliceMethod · 0.80
toStringMethod · 0.80

Tested by

no test coverage detected