Created
May 15, 2025 04:19
-
-
Save AntonioReyes-creator/9c32f2297c9bb09ecfb4a28e29930299 to your computer and use it in GitHub Desktop.
Buscar pelisculas
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 { | |
buscarPeliculas, | |
buscarPorGenero, | |
buscarPorAño, | |
ordenarPeliculasPorCampo, | |
getPeliculas, | |
} = require('./pelis'); | |
const args = process.argv.slice(2); | |
function todasLasPeliculas(){ | |
if (args.length === 0) { | |
const todas = require("./pelis").getPeliculas(); | |
return console.table(todas); | |
} | |
} | |
const mapa = { | |
'search': buscarPeliculas, | |
'tag': buscarPorGenero, | |
'year': buscarPorAño, | |
'sort': ordenarPeliculasPorCampo, | |
}; | |
if (args[0] && args[0].startsWith('--')) { | |
const comando = args[0].substring(2); | |
const argumento = args[1]; | |
const funcion = mapa[comando]; | |
if (funcion && argumento) { | |
const resultado = funcion(argumento); | |
console.table(resultado); | |
} else { | |
console.log("Comando o argumento no válido."); | |
} | |
} else { | |
console.log(todasLasPeliculas()); | |
console.log("Uso correcto:"); | |
console.log(" node index.js --search <title>"); | |
console.log(" node index.js --tags <nombre>"); | |
console.log(" node index.js --year <año>"); | |
console.log(" node index.js --sort <campo>"); | |
} |
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"); | |
function getPeliculas() { | |
// const data = fs.readFileSync("./pelis.json", "utf-8"); | |
const data = fs.readFileSync(__dirname + "/pelis.json", "utf-8"); | |
return JSON.parse(data); | |
}; | |
function buscarPeliculas(title) { | |
const pelis = getPeliculas(); | |
return pelis.filter((p) => | |
p.title.toLowerCase().includes(title.toLowerCase()) | |
); | |
} | |
// function buscarPorGenero(tag) { | |
// const pelis = getPeliculas(); | |
// return pelis.filter(p => p.tag.toLowerCase().includes(tag.toLowerCase())); | |
// } | |
function buscarPorGenero(tagBuscado) { | |
const pelis = getPeliculas(); | |
return pelis.filter((p) => { | |
if (typeof p.tags === "string") { | |
return p.tags.toLowerCase().includes(tagBuscado.toLowerCase()); | |
} else if (Array.isArray(p.tags)) { | |
return p.tags.some((t) => | |
t.toLowerCase().includes(tagBuscado.toLowerCase()) | |
); | |
} | |
return false; | |
}); | |
} | |
function buscarPorAño(año) { | |
const pelis = getPeliculas(); | |
return pelis.filter((p) => p.año == año); // comparación flexible | |
} | |
function ordenarPeliculasPorCampo(campo) { | |
const pelis = getPeliculas(); | |
return pelis.sort((a, b) => { | |
if (typeof a[campo] === "string") { | |
return a[campo].localeCompare(b[campo]); | |
} | |
return a[campo] - b[campo]; | |
}); | |
} | |
module.exports = { | |
buscarPeliculas, | |
buscarPorGenero, | |
buscarPorAño, | |
ordenarPeliculasPorCampo, | |
getPeliculas, | |
}; | |
// //const vista =getPeliculas(); | |
// // const vista2=buscarPeliculas('matrix'); | |
// const vista3 = buscarPorGenero("Drama"); | |
// // //console.log(vista); | |
// console.log(vista3); |
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": "Matrix", | |
"año": 1999, | |
"tags": "Ciencia ficción", | |
"rating": 5 | |
}, | |
{ | |
"title": "El Padrino", | |
"año": 1972, | |
"tags": "Drama", | |
"rating": 5 | |
}, | |
{ | |
"title": "Avatar", | |
"año": 2009, | |
"tags": "Fantasía", | |
"rating": 5 | |
}, | |
{ | |
"title": "Titulo de la película", | |
"año": 2023, | |
"tags": ["acción", "favorita", "nueva"], | |
"rating": 4 | |
}, | |
{ | |
"title": "Titulo de la segunda película", | |
"año": 2023, | |
"tags": ["favorita", "nueva"], | |
"rating": 3 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment