Created
March 1, 2021 23:24
-
-
Save gonnndc/19a9c70f439ebfaa01f3d378b87677f9 to your computer and use it in GitHub Desktop.
Desafío Modulo 1
This file contains hidden or 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
[ | |
{ | |
"title":"batman: the dark knight returns", | |
"rating": 9.0, | |
"where": "netflix", | |
"view": true, | |
"tags" : ["accion", "drama", "crimen"] | |
}, | |
{ | |
"title":"avengers: endgame", | |
"rating": 8.4, | |
"where": "disney", | |
"view": true, | |
"tags": ["accion", "sci-fi", "superheroes"] | |
}, | |
{ | |
"title":"soul", | |
"rating": 8.1, | |
"where": "disney", | |
"view": false, | |
"tags":["animacion", "comedia", "drama"] | |
}, | |
{ | |
"title":"birdman", | |
"rating": 7.7, | |
"where": "prime", | |
"view": false, | |
"tags": ["comedia", "drama", "suspenso"] | |
}, | |
{ | |
"title":"gladiador", | |
"rating": 8.5, | |
"where": "prime", | |
"view": true, | |
"tags": ["accion", "aventura", "drama"] | |
}, | |
{ | |
"title":"john wick 3: parabellum", | |
"rating": 7.4, | |
"where": "prime", | |
"view": false, | |
"tags": ["accion", "crimen", "suspenso"] | |
} | |
] |
This file contains hidden or 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
const peliculas = require("./pelis.js"); | |
const args = process.argv; | |
//Funcion que parsea y ejecuta | |
const parsear = (args, cont) => { | |
llaveIngresada = args[cont].slice(2, args[cont].length); | |
valorIngresado = args[cont + 1]; | |
return { | |
llaveIngresada, | |
valorIngresado | |
} | |
}; | |
const ejecutar = (argumento, peliBuscada) => { | |
switch (argumento.llaveIngresada) { | |
case "search": | |
peliBuscada = peliculas.buscarPorNombre(argumento.valorIngresado, peliBuscada); | |
break; | |
case "rating": | |
peliBuscada = peliculas.listadoRatingMinimo( | |
argumento.valorIngresado, | |
peliBuscada | |
); | |
break; | |
case "where": | |
peliBuscada = peliculas.listadoDonde(argumento.valorIngresado, peliBuscada); | |
break; | |
case "sort": | |
peliBuscada = peliculas.pelisOrdenadas(argumento.valorIngresado, peliBuscada); | |
break; | |
case "view": | |
peliBuscada = peliculas.listadoVistas(argumento.valorIngresado, peliBuscada); | |
break; | |
case "tag": | |
peliBuscada = peliculas.buscarTag(argumento.valorIngresado, peliBuscada); | |
break; | |
case "no-format": | |
peliBuscada = peliculas.noFormato(peliBuscada); | |
break; | |
default: | |
break; | |
} | |
return peliBuscada; | |
}; | |
const main = () => { | |
let peliBuscada = peliculas.objJson; | |
if (args.length > 2) { | |
cont = 2; | |
//Bucle para recorrer la cantidad de comandos que sean ingresados | |
while (cont < args.length && args[cont].includes("--")) { | |
const argParseado = parsear(args, cont); | |
peliBuscada = ejecutar(argParseado, peliBuscada); | |
cont = cont + 2; | |
} | |
peliculas.imprimirPelis(peliBuscada) | |
} else { | |
//PANTALLA INICIAL | |
console.log( | |
" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" | |
); | |
console.log( | |
"HISTORIAL DE PELICULAS, VISTAS O POR SER VISTAS, CON SU RESPECTIVO RATING Y PLATAFORMA DONDE SE LA PUEDE ENCONTRAR" | |
); | |
console.log( | |
" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" | |
); | |
peliculas.imprimirPelis(peliculas.objJson); | |
console.log(""); | |
console.log("Opciones de filtrado: "); | |
console.log(""); | |
console.log( | |
"Busqueda por nombre: --search (Buscar usando letra o palabras) " | |
); | |
console.log( | |
"Filtrado por rating minimo: --rating (valor minimo que acepta de rating)" | |
); | |
console.log("Busqueda por plataforma: --donde (plataforma elegida) "); | |
console.log("Busqueda por tag: --tag (genero buscado) "); | |
console.log( | |
"Ordenar listado: --sort (ordena segun la propiedad ingresada) " | |
); | |
console.log("Peliculas vistas o no vistas: --view (true o false)"); | |
console.log( | |
"Parseo final a formato JSON despues de recibir los filtros anteriores: --no-format" | |
); | |
console.log(""); | |
} | |
}; | |
main(); |
This file contains hidden or 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
const fs = require('fs') | |
const obj = JSON.parse(fs.readFileSync(__dirname + "/pelis.json")); | |
const printFilms = (peliBuscada) => { | |
console.table(peliBuscada); | |
}; | |
const search = (name, peliBuscada) => { | |
const list = peliBuscada.filter((item) => { | |
return item.title.includes(name); | |
}); | |
return list; | |
}; | |
const sorted = (param, peliBuscada) => { | |
peliBuscada.sort((a, b) => { | |
if (a[param] < b[param]) { | |
return -1; | |
} else if (a[param] > b[param]) { | |
return 1; | |
} else { | |
return 0; | |
} | |
}); | |
return peliBuscada; | |
}; | |
const getWhere = (plataforma, peliBuscada) => { | |
peliBuscada = peliBuscada.filter((item) => { | |
return item.where == plataforma; | |
}); | |
return peliBuscada; | |
}; | |
const getAllWithRatingMin = (minRating, peliBuscada) => { | |
peliBuscada = peliBuscada.filter((item) => { | |
return item.rating >= minRating; | |
}); | |
return peliBuscada; | |
}; | |
const getAllSeen = (valor, peliBuscada) => { | |
if (valor == true) { | |
peliBuscada = peliBuscada.filter((item) => item.view); | |
return peliBuscada; | |
} | |
peliBuscada = peliBuscada.filter((item) => item.view == false); | |
return peliBuscada; | |
}; | |
const getTag = (valor, peliBuscada) => { | |
peliBuscada = peliBuscada.filter((item) => { | |
return item.tags.includes(valor); | |
}); | |
return peliBuscada; | |
}; | |
const noFormat = (peliBuscada) => { | |
return JSON.stringify(peliBuscada); | |
}; | |
exports.imprimirPelis = printFilms; | |
exports.buscarPorNombre = search; | |
exports.pelisOrdenadas = sorted; | |
exports.listadoDonde = getWhere; | |
exports.listadoRatingMinimo = getAllWithRatingMin; | |
exports.listadoVistas = getAllSeen; | |
exports.buscarTag = getTag; | |
exports.noFormato = noFormat; | |
exports.objJson = obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El main tiene mucho código. Mové toda la lógica de parseo de argv a una función que reciba lo justo y necesario (process.argv). No hace falta declarar argv afuera de la función. El while es super complejo, si podés pensalo mejor. El pelis.js está bastante bien. imprimirPelis no es algo que debería hacer pelis.js, el main se encarga de imprimir la salida. Ordenalo un poco y volvé a mandarlo.