Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 12 additions & 0 deletions .idea/codingdojo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/dictionaries/giovanicascaes.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 128 additions & 73 deletions 2016-11-05 - Javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,141 @@
var test = require('tape');
var unidades = ['', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez', 'onze', 'doze', 'treze', 'quatorze', 'quinze', 'dezesseis', 'dezessete', 'dezoito', 'dezenove'];
var dezenas = ['', 'dez', 'vinte', 'trinta', 'quarenta', 'cinquenta', 'sessenta', 'setenta', 'oitenta', 'noventa'];
var centenas = ['cem', 'cento', 'duzentos', 'trezentos', 'quatrocentos', 'quinhentos', 'seissentos', 'setessentos', 'oitocentos', 'novecentos'];
var demaisUnidades = [['mil', 'mil'], ['milhão', 'milhões'], ['bilhão', 'bilhões'], ['trilhão', 'trilhões'], ['quatrilhão', 'quatrilhões']];

var arrayUnidade = ['zero', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez', 'onze', 'doze', 'treze', 'quatorze', 'quinze', 'dezesseis', 'dezessete', 'dezoito', 'dezenove'];
var dezena = ['', 'dez', 'vinte', 'trinta', 'quarenta', 'cinquenta', 'sessenta', 'setenta', 'oitenta', 'noventa'];
var and = ' e ';

function capitalizeFirstLetter(string) {
if (string == undefined || !string) return '';
return string.charAt(0).toUpperCase() + string.slice(1);
}

function tratarNumero(valor, tipoNumero) {
var unidadeMonetaria = '';
var valorPorExtenso = '';
var real = false;
var valorEmInteiro = parseInt(valor);
if (valorEmInteiro > 0){
var valorDecimalInt = valorEmInteiro;
var and = ' e ';

if (tipoNumero == 'real') {
unidadeMonetaria = valorDecimalInt > 1 ? 'reais' : 'real';
real = true;
} else {
unidadeMonetaria = valorDecimalInt > 1 ? 'centavos' : 'centavo';
}
module.exports = {
converterValorParaExtenso: function (valor) {
var arrayValores = valor.split(',');

var unidade = arrayUnidade[valorDecimalInt];

if (valorDecimalInt <= 19) {
valorPorExtenso += unidade;
} else {
console.log(valorPorExtenso);
if(real){

valorPorExtenso += and + dezena[valor.charAt(0)];
}
if (valor.charAt(1) != '0') {
valorPorExtenso += and + arrayUnidade[valor.charAt(1)];
}
}
valorPorExtenso += ' ' + unidadeMonetaria;
}
return valorPorExtenso;
}
var reais = arrayValores[0];
var centavos = arrayValores[1];

function tratarReais(reais) {
return tratarNumero(reais, 'real');
}
var valorPorExtenso = converterReais(reais);

var converteValorEmExtenso = function(valor) {
var arrayValores = valor.split(',');
if (parseInt(centavos) > 0) {
valorPorExtenso += and + converterCentavos(centavos);
}

var valorPorExtenso = '';
valorPorExtenso = capitalizeFirstLetter(valorPorExtenso);

var reais = arrayValores[0];
var centavos = arrayValores[1];
var reaisInt = parseInt(reais);
var centavosInt = parseInt(centavos);

var real = reaisInt > 1 ? 'reais' : 'real';
var unidade = arrayUnidade[reais];
return valorPorExtenso;
}
};

valorPorExtenso = tratarReais(reais)
if(centavos != '00') {
valorPorExtenso += tratarNumero(centavos, 'centavo');
}
function converterReais(reais) {
return converterNumero(reais, true);
}

valorPorExtenso = capitalizeFirstLetter(valorPorExtenso);

return valorPorExtenso;
function converterCentavos(reais) {
return converterNumero(reais, false);
}

test('testando cheque', function (t) {
function converterNumero(valor, real) {
var valorPorExtenso = '';

var valorInt = parseInt(valor);

if (valorInt > 0) {
if (valorInt <= 19) {
valorPorExtenso += unidades[valorInt];
} else if (valorInt <= 99) {
var valorIntString = valorInt.toString();
valorPorExtenso += dezenas[valorIntString.charAt(0)];

if (valorIntString.charAt(1) != '0') {
valorPorExtenso += and + unidades[valorIntString.charAt(1)];
}
} else if (valorInt <= 999) {
var dezena = valor.substr(1, 2);
var dezenaInt = parseInt(dezena);

if (valor.charAt(0) == '1' && dezenaInt == 0) {
valorPorExtenso += centenas[0];
} else {
valorPorExtenso += centenas[valor.charAt(0)];

if (dezenaInt > 0) {
valorPorExtenso += and + converterNumero(dezena)
}
}
} else {
var tamanhoNumero = valor.length;
var tamanhoNumeroMod3 = tamanhoNumero % 3;

var countFirstNumbers = tamanhoNumeroMod3 == 0 ? 3 : tamanhoNumeroMod3;
var countUnidades = tamanhoNumeroMod3 == 0 ? Math.floor(tamanhoNumero / 3) - 1 : Math.floor(tamanhoNumero / 3);

var firstNumbers = valor.substr(0, countFirstNumbers);
var lastNumbers = valor.substr(countFirstNumbers);
var firstNumbersInt = parseInt(firstNumbers);
var lastNumbersInt = parseInt(lastNumbers);

if (firstNumbersInt > 0) {
valorPorExtenso += converterNumero(firstNumbers) + ' ' + demaisUnidades[countUnidades - 1][firstNumbersInt > 1 ? 1 : 0];
}

if (lastNumbersInt > 0) {
// Daqui...
if (lastNumbersInt <= 99) {
valorPorExtenso += and;
} else {
var verificouZeros = false;
var anexarAnd = false;

for (var i = countUnidades; i > 0; i--) {
var unidade = lastNumbers.substr(lastNumbers.length - (i * 3), 3);
var unidadeInt = parseInt(unidade);

if (unidadeInt == 0) {
if (!verificouZeros) {
lastNumbers = lastNumbers.substr(3);
countUnidades--;
}
} else {
verificouZeros = true;

if (i == 1 && countUnidades > 1) {
anexarAnd = false;
} else if (unidadeInt < 99 || unidade.substr(1, 2) == '00') {
if (!anexarAnd) {
anexarAnd = true;
} else {
anexarAnd = false;

break;
}
}
}
}

if (anexarAnd) {
valorPorExtenso += and;
} else {
valorPorExtenso += ' ';
}
}
// ...até aqui é só para verificar se deve adicionar o ' e ' ou ' '

valorPorExtenso += converterNumero(lastNumbers);
} else if (countUnidades > 1) {
valorPorExtenso += ' de';
}
}

if (real != undefined) {
if (real) {
valorPorExtenso += ' ' + (valorInt > 1 ? 'reais' : 'real');
} else {
valorPorExtenso += ' ' + (valorInt > 1 ? 'centavos' : 'centavo');
}
}
}

t.equal(converteValorEmExtenso('1,00'), 'Um real', 'Deveria retornar Um real');
t.equal(converteValorEmExtenso('2,00'), 'Dois reais', 'Deveria retornar Dois reais');
t.equal(converteValorEmExtenso('3,00'), 'Três reais', 'Deveria retornar Tres reais');
t.equal(converteValorEmExtenso('3,40'), 'Três reais e quarenta centavos', 'Deveria retornar Tres reais e quarenta centavos');
t.equal(converteValorEmExtenso('15,00'), 'Quinze reais', 'Deveria retornar Quinze reais');
t.equal(converteValorEmExtenso('22,00'), 'Vinte e dois reais', 'Deveria retornar Vinte e dois reais');
t.equal(converteValorEmExtenso('29,03'), 'Vinte e nove reais e três centavos', 'Deveria retornar Vinte e nove reais e três <centavos></centavos>');
t.equal(converteValorEmExtenso('29,33'), 'Vinte e nove reais e trinta e três centavos', 'Deveria retornar Vinte e nove reais e trinta e três <centavos></centavos>');

t.end();
return valorPorExtenso;
}

});
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
50 changes: 50 additions & 0 deletions 2016-11-05 - Javascript/test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/tape

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/balanced-match/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/balanced-match/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading