-
-
Save RossiniMaximo/bef8844fae3d1f20df708acfa756d97f to your computer and use it in GitHub Desktop.
const peliculas = require("./pelis.js"); | |
function parsed (argv) { | |
var obj = {}; | |
argv.forEach((item,ind) => { | |
if (item.startsWith("--")){ | |
var itemLimpio = item.slice(2); | |
obj[itemLimpio] = argv [ind + 1]; | |
} | |
}) | |
return obj | |
} | |
function main () { | |
const argumentos = process.argv.slice(2); | |
const ArgsParseados = parsed(argumentos); | |
const ejecucion = peliculas.searchByCriteria(ArgsParseados); | |
console.log(ejecucion); | |
}; | |
main(); |
const fs = require("fs"); | |
const archivoPelis = fs.readFileSync( __dirname +"/pelis.json"); | |
const archivoToString = archivoPelis.toString(); | |
const parseArchivo = JSON.parse(archivoToString); | |
const getAll = (collection) => { | |
return parseArchivo; | |
} | |
const sortBy = function (propiedad, arrayDePelis) { | |
const datos = arrayDePelis; | |
var datosOrdenados = datos.sort((a, b) => { | |
if (a[propiedad] > b[propiedad]) { | |
return 1; | |
} | |
if (a[propiedad] < b[propiedad]) { | |
return -1; | |
} | |
return 0; | |
}); | |
return datosOrdenados; | |
}; | |
const getSimilarities = (palabra,arrayDePelis) => { | |
const datos = arrayDePelis | |
const encuentraSimilitudes = datos.filter((objeto) => { | |
if (objeto.title == palabra || objeto.title.includes(palabra)){ | |
return objeto; | |
}; | |
}); | |
return encuentraSimilitudes; | |
}; | |
const getByTag = (palabra,arrayDePelis) => { | |
const datos = arrayDePelis | |
const encuentraTags = datos.filter((objeto) => { | |
if ( objeto.tags.includes(palabra)){ | |
return objeto | |
} | |
}) | |
return encuentraTags; | |
} | |
const noFormat = (collection) => { | |
return JSON.stringify(collection); | |
}; | |
exports.searchByCriteria = function (criterio) { | |
var peliculas = getAll(); | |
if (criterio.length < 1){ | |
return peliculas | |
} | |
if (criterio.sort ){ | |
peliculas = (sortBy(criterio.sort,peliculas)); | |
}; | |
if ( criterio.search){ | |
peliculas = (getSimilarities(criterio.search,peliculas)); | |
}; | |
if (criterio.tags){ | |
peliculas = (getByTag(criterio.tags,peliculas)); | |
}; | |
if (criterio.hasOwnProperty("no-format")){ | |
peliculas = (noFormat(peliculas)) | |
}; | |
return peliculas | |
}; |
[{ | |
"title" : "Interstellar", | |
"rating" : 10, | |
"tags" : "[ciencia ficción]" | |
}, | |
{ | |
"title" : "Batman begins", | |
"rating" : 8.5, | |
"tags" : "[acción,Aventura drama,Suspenso]" | |
}, | |
{ | |
"title" : "The joker", | |
"rating" : 9.5, | |
"tags" : "[Drama,Suspenso,Crimen,Thriller psicológico]" | |
}, | |
{ | |
"title" : "Relatos salvajes", | |
"rating" : 7, | |
"tags" : "[Comedia,Thriller cómico,drama,suspenso]" | |
}, | |
{ | |
"title" : "Avatar", | |
"rating" : 10, | |
"tags" : "[ciencia ficción,aventura,acción,fantástico,animación]" | |
}, | |
{ | |
"title" : "Avatar la leyenda de Anng", | |
"rating" : 6, | |
"tags" : "[fantasia,aventura]" | |
} | |
] |
Muchas gracias x la revision!!
Hola Maximo! revise tu código y tengo unas cositas para sugerirte.
Primero, como dice Pablo arriba, en la función getAll() que no hace falta que le pases ningún parámetro para darte el return
Segundo, probe los tags y le haría un arreglo a getTag para que le pase todos los nombres, tanto de los tags como el de la búsqueda en minúscula, ya que por ejemplo el tag "suspenso" lo pusiste en mayúsculas en algunas pelis y en otras en minúscula, lo que hizo que cuando busque ese tag me filtre algunas pelis, no todas. Sugerencia como para que la búsqueda difiera de que este en mayúscula o no y sea mas prolija.
Y por ultimo, me parece que no hace falta que uses:
if (criterio.length < 1){ return peliculas }
Ya que de todas formas la función siempre te devuelve la lista entera de películas en el final, yo se lo saque, lo probé y funciono igual!
Mas allá de eso, el código funciona excelente y me gusto como quedo planteado! así que LGTM!
Gracias facu por la revision, me gustaron las recomendaciones!
Hola compa! fijate que la función getAll no recibe parámetros, solamente devuelve las pelis al ser invocada. Por lo que debería quedar así:
const getAll = () => {
return parseArchivo;
}
Aparte de ese detalle lo veo super bien!! LGMT :)