Last active
July 16, 2021 18:37
-
-
Save caferrari/178c7ea5da8710b297d3083ca9ca2e59 to your computer and use it in GitHub Desktop.
Resposta do Desafio #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const decode = (input: string) => { | |
const chars = input.trim().replace(/\n/g, ''); | |
const data = Buffer.from(chars, 'base64').toString('utf-8'); | |
const payload = JSON.parse(data); | |
const dic = Buffer.from(payload.dic, 'base64').toString('utf-8'); | |
const letters = payload.message.split(',').map((char: string) => dic[Number(char)]); | |
return letters.join(''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const encode = (input: string) => { | |
const chars = input.toLowerCase().split(''); | |
const set = new Set(chars); | |
const dic = [...Array.from(set)].sort(() => Math.random() > .5 ? 1 : -1).join('') | |
const message = chars.map(c => dic.indexOf(c)).join(','); | |
const payload = JSON.stringify({dic: Buffer.from(dic).toString('base64'), message}); | |
const stringData = Buffer.from(payload).toString('base64'); | |
return stringData.match(/.{1,64}/g)?.join("\n") as string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment