forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reto mouredev#11 URL PARAMS - typscript
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Retos/Reto #11 - URL PARAMS [Fácil]/typescript/Aspir-ina.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |