Skip to content

Instantly share code, notes, and snippets.

@Xhamps
Created August 5, 2022 20:05
Show Gist options
  • Save Xhamps/dc5b3c0eddc8bcfb0fa95f7b417ec726 to your computer and use it in GitHub Desktop.
Save Xhamps/dc5b3c0eddc8bcfb0fa95f7b417ec726 to your computer and use it in GitHub Desktop.
const units:{ [key: number]: String } = {
0: 'zero',
1: 'um',
2: 'dois',
3: 'três',
4: 'quatro',
5: 'cinco',
6: 'seis',
7: 'sete',
8: 'oito',
9: 'nove',
}
const ten1:{ [key: number]: String }= {
10: 'dez',
11: 'onze',
12: 'doze',
13: 'treze',
14: 'quatorze',
15: 'quinze',
16: 'dezesseis',
17: 'dezessete',
18: 'dezoito',
19: 'dezenove'
}
const ten2:{ [key: number]: String } = {
2: 'vinte',
3: 'trinta',
4: 'quarenta',
5: 'cinquenta',
6: 'sessenta',
7: 'setenta',
8: 'oitenta',
9: 'noventa',
}
const handreds:{ [key: number]: String } = {
1: 'cem',
2: 'duzentos',
3: 'trezentos',
4: 'quatrocentos',
5: 'quinhentos',
6: 'seiscentos',
7: 'setessentos',
8: 'oitocentos',
9: 'novecentos'
}
const formatNumber: any= {
1: units,
2: ten2,
3: handreds,
}
const translate_to_protugues = (baseNum: number):String => {
if(baseNum>=10 && baseNum<= 19) return ten1[baseNum];
const arrNums = String(baseNum).split('');
return arrNums.reduce((arr: string[], num, i) => {
const pos = arrNums.length - i;
const format = formatNumber[pos];
// 0
// 20
// 100
// 101
// num === 0
// arrNums.length > 1
//
if(num === '0' && arrNums.length > 1) return arr;
let numStr = format[num];
if(pos === 3 && baseNum > 100 && baseNum <= 199) numStr = 'cento';
arr.push(numStr);
return arr;
}, []).join(' e ');
}
export {
translate_to_protugues
}
import { translate_to_protugues } from "../../src/name_portugese";
describe('Name protugues', () => {
test('should return a name units', () => {
expect(translate_to_protugues(5)).toEqual('cinco');
});
test('should return a name ten', () => {
expect(translate_to_protugues(18)).toEqual('dezoito');
});
test('should return a name of 83', () => {
expect(translate_to_protugues(83)).toEqual('oitenta e três');
});
test('should return a name of 542', () => {
expect(translate_to_protugues(542)).toEqual('quinhentos e quarenta e dois');
});
test('should return a name of 20', () => {
expect(translate_to_protugues(20)).toEqual('vinte');
});
test('should return a name of 50', () => {
expect(translate_to_protugues(50)).toEqual('cinquenta');
});
test('should return a name of 100', () => {
expect(translate_to_protugues(100)).toEqual('cem');
});
test('should return a name of 101', () => {
expect(translate_to_protugues(101)).toEqual('cento e um');
});
// test('should return a name of 111', () => {
// expect(translate_to_protugues(111)).toEqual('cento e unze');
// });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment