Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 #46

Merged
merged 11 commits into from
Sep 30, 2019
15 changes: 15 additions & 0 deletions ed/#05-27-09-2019/group-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Grupo 5

Felipe Alves Ferreira <[email protected]>

Willian Nalbert <[email protected]>

Julio Hintze <[email protected]>


## Desafios entregues
- Desafio 1 (em PHP) `arquivo-des-1.php` - &lt;Completo&gt;
- Desafio 1 (em TypeScript) `typescript/arquivo-des-1.ts` - &lt;Completo&gt;
- Desafio 2 (em TypeScript) `typescript/arquivo-des-2.ts` - &lt;Completo&gt;
- Desafio 3 e 4 (em TypeScript) `typescript/arquivo-des-3-4.ts` - &lt;Completo&gt;
- Desafio 5 (em TypeScript) `typescript/arquivo-des-5.ts` - &lt;Completo&gt;
31 changes: 31 additions & 0 deletions ed/#05-27-09-2019/group-5/arquivo-des-1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

$args = array(
"Log" => "Inválido",
"Porta" => "0",
"Diretório" => '""'
);

foreach($argv as $key => $arg){

if($arg == "-l"){
$args["Log"] = "Válido";
}
if($arg == "-p"){
if(is_numeric($argv[$key+1])){
$args["Porta"] = $argv[$key+1];
}
}
if($arg == "-d"){
if(isset($argv[$key+1])){
$args['Diretório'] = $argv[$key+1];
}
}

}

echo "\n";
foreach($args as $chave => $valor){
echo $chave . ": " . $valor . "\n";
}
echo "\n";
155 changes: 155 additions & 0 deletions ed/#05-27-09-2019/group-5/typescript/arquivo-des-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Interfaces
// ------------------------------------

interface Args {
[sinalizador: string]: any
}

interface EsquemaArg {
sinalizador: string;
tipo: 'string' | 'boolean' | 'inteiro' | 'array';
padrao: any;
}

interface RespostaValidacao {
validos: Args[];
invalidos: Args[];
}




/**
* Plano:
*
* 1. Pegar do argsExemplo cada sinalizador com seu possível valor.
*
* 2. Separar os args válidos dos inválidos em objetos separados.
* Os válidos são os que o sinalizador está nos esquemas e o valor
* atende ao tipo pedido. Inválidos são o resto.
*
* 3. Criar um objeto com os args padrões.
*
* 4. Sobrepor os args padrões com os válidos.
*
* 5. Printar os args válidos e inválidos.
*
* 6. ???
*
* 7. PROFIT!
*/





// Variáveis
// ------------------------------------

// 1º Passo:
let argsExemplo = '-l -p 4200 -d / usr / logs -g isto, é, uma, lista -r';
let argsRx = /(-\w)(.*?)(?=(-|$))/g;
let rxRes: RegExpExecArray | null;
let todosArgs: Args = {};

// 2º Passo:
let esquemas: EsquemaArg[] = [

{
tipo: 'boolean',
sinalizador: '-l',
padrao: false
},
{
tipo: 'inteiro',
sinalizador: '-p',
padrao: 8080
},
{
tipo: 'string',
sinalizador: '-d',
padrao: './'
},
{
tipo: 'array',
sinalizador: '-g',
padrao: []
}
];
let argsValidos: Args = {};
let argsInvalidos: Args = {};

// 3º Passo:
let argsPadrao: Args = {};





// Lógica
// ------------------------------------

// 1º Passo
while (rxRes = argsRx.exec(argsExemplo)) {
todosArgs[rxRes[1]] = rxRes[2].trim();
}

// 2º Passo
for (let sin in todosArgs) {
let valor = todosArgs[sin];
let esq = esquemas.find(esq => esq.sinalizador === sin);

if (esq) {

switch(esq.tipo) {
case 'string':
if (valor) argsValidos[sin] = valor;
else argsInvalidos[sin] = valor;
break;

case 'inteiro':
if (isFinite(+valor)) argsValidos[sin] = +valor;
else argsInvalidos[sin] = valor;
break;

case 'boolean':
argsValidos[sin] = true;
break;

case 'array':
argsValidos[sin] = valor.split(',');
argsValidos[sin] = argsValidos[sin].map((val: string) => val.trim());
break;

default:
argsInvalidos[sin] = valor;
break;
}

} else {
argsInvalidos[sin] = valor;
}
}

// 3º Passo
esquemas.forEach(esq => {
argsPadrao[esq.sinalizador] = esq.padrao;
});

// 4º Passo
argsValidos = {
...argsPadrao,
...argsValidos
};

// 5º Passo
console.log('============================Args============================');
console.log(argsExemplo, '\n');

console.log('===========================Válidos==========================');
console.table(JSON.stringify(argsValidos, null, 4));
console.log('\n');

console.log('==========================Inválidos=========================');
console.table(JSON.stringify(argsInvalidos, null, 4));
console.log('\n');
Loading