From 69c3e37b8dc73c22cb12a6394ed239d0ac2c1f17 Mon Sep 17 00:00:00 2001 From: dfabian Date: Mon, 13 Mar 2023 21:59:41 +0100 Subject: [PATCH] Reto #11 URL PARAMS - typscript --- .../typescript/Aspir-ina.ts" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "Retos/Reto #11 - URL PARAMS [F\303\241cil]/typescript/Aspir-ina.ts" diff --git "a/Retos/Reto #11 - URL PARAMS [F\303\241cil]/typescript/Aspir-ina.ts" "b/Retos/Reto #11 - URL PARAMS [F\303\241cil]/typescript/Aspir-ina.ts" new file mode 100644 index 0000000000..d08a18892c --- /dev/null +++ "b/Retos/Reto #11 - URL PARAMS [F\303\241cil]/typescript/Aspir-ina.ts" @@ -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 = {}; + 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)); \ No newline at end of file