Skip to content

Commit

Permalink
Merge pull request #2517 from Aspir-ina/main
Browse files Browse the repository at this point in the history
Reto #11 - Typescript
  • Loading branch information
Roswell468 authored Mar 13, 2023
2 parents a79b6bc + ed2b621 commit 5dbc7a2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Retos/Reto #11 - URL PARAMS [Fácil]/typescript/Aspir-ina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Dada una URL con parámetros, crea una función que obtenga sus valores.
* No se pueden usar operaciones del lenguaje que realicen esta tarea directamente.
*
* Ejemplo: En la url https://retosdeprogramacion.com?year=2023&challenge=0
* los parámetros serían ["2023", "0"]
*/

const queryParamParser = (url: string, onlyValues: boolean = false) => {
const urlParts = url.split('?');
const queryParams = urlParts[1].split('&');
const params: Record<string, string> = {};
queryParams.forEach((param) => {
const [key, value] = param.split('=');
params[key] = value;
})

if (onlyValues) {
return Object.values(params);
}

return params;
}



const url = 'https://www.google.com/search?q=typescript&oq=typescript&aqs=chrome..69i57j0l5.1009j0j7&sourceid=chrome&ie=UTF-8';

console.log(queryParamParser(url));
console.log(queryParamParser(url, true));

0 comments on commit 5dbc7a2

Please sign in to comment.