Skip to content

Instantly share code, notes, and snippets.

@davidcostadev
Created December 14, 2018 18:29
Show Gist options
  • Save davidcostadev/303b57eaa66e87b8b357e9e7d9cfff8f to your computer and use it in GitHub Desktop.
Save davidcostadev/303b57eaa66e87b8b357e9e7d9cfff8f to your computer and use it in GitHub Desktop.
Meu transpilador de Potifol para javascript
console.clear();
const compressLine = (line) => line.replace(/[ ]/g, '');
const assigmentSimple = ([ name, value ]) => `const ${name} = ${value};`
const assigment = string => {
const [names, values] = string.split('=');
const namesList = names.split(',');
const valuesList = values.split(',');
return namesList.map((name, index) => {
if (valuesList.length === 1) {
return assigmentSimple([name, valuesList[0]])
}
return assigmentSimple([name, valuesList[index]])
}).join('')
}
const compiler = (string) => {
const line = compressLine(string);
const pipe = assigment(line);
return pipe;
}
expect(compiler('algo = 1')).toBe('const algo = 1;');
expect(compiler('algo = \'string\'')).toBe('const algo = \'string\';');
expect(compiler('x, y = 20')).toBe('const x = 20;const y = 20;');
expect(compiler('x, y = 1, 2')).toBe('const x = 1;const y = 2;');
console.log('all tests are passed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment