Skip to content

Instantly share code, notes, and snippets.

@ByxhLoop
Created February 7, 2025 01:42
Show Gist options
  • Save ByxhLoop/97249cf81a2f4d130ad54d58016f07aa to your computer and use it in GitHub Desktop.
Save ByxhLoop/97249cf81a2f4d130ad54d58016f07aa to your computer and use it in GitHub Desktop.
Filtro De Peliculas
const { obtenerPeliculas, ordenarPeliculas, buscarPeliculas, filtrarPorTag } = require('./pelis.js');
const main = () => {
const args = process.argv.slice(2);
if (args.length === 0) {
console.table(obtenerPeliculas());
} else if (args[0] === '--sort') {
const propiedad = args[1];
const peliculasOrdenadas = ordenarPeliculas(propiedad);
console.table(peliculasOrdenadas);
} else if (args[0] === '--search') {
const criterio = args[1];
console.table(buscarPeliculas(criterio));
} else if (args[0] === '--tag') {
const tag = args[1];
console.table(filtrarPorTag(tag));
} else {
console.log('Argumento no reconocido. Usa --sort, --search o --tag.');
}
};
main();
const fs = require('fs');
const leerPeliculas = () => {
const data = fs.readFileSync('./pelis.json');
return JSON.parse(data);
};
const obtenerPeliculas = () => {
return leerPeliculas();
};
const ordenarPeliculas = (propiedad, orden) => {
const peliculas = obtenerPeliculas();
return peliculas.sort((a, b) => {
if (orden === 'asc') {
return a[propiedad] - b[propiedad];
} else {
return b[propiedad] - a[propiedad];
}
});
};
const buscarPeliculas = (criterio) => {
const peliculas = obtenerPeliculas();
return peliculas.filter(pelicula => pelicula.title.toLowerCase().includes(criterio.toLowerCase()));
};
const filtrarPorTag = (tag) => {
const peliculas = obtenerPeliculas();
return peliculas.filter(pelicula => pelicula.tags.includes(tag));
};
module.exports = {
obtenerPeliculas,
ordenarPeliculas,
buscarPeliculas,
filtrarPorTag,
};
[
{
"title": "Inception",
"rating": 8.8,
"tags": ["acción", "sci-fi", "favorita"]
},
{
"title": "The Godfather",
"rating": 9.2,
"tags": ["drama", "clásico"]
},
{
"title": "Toy Story",
"rating": 8.3,
"tags": ["animación", "familia", "favorita","1"]
},
{
"title": "The Dark Knight",
"rating": 9.0,
"tags": ["acción", "superhéroes","1"]
},
{
"title": "Pulp Fiction",
"rating": 8.9,
"tags": ["drama", "acción", "clásico"]
},
{
"title": "The Shawshank Redemption",
"rating": 9.3,
"tags": ["drama", "clásico"]
},
{
"title": "Forrest Gump",
"rating": 8.8,
"tags": ["drama", "romance"]
},
{
"title": "Fight Club",
"rating": 8.8,
"tags": ["drama", "acción"]
},
{
"title": "The Matrix",
"rating": 8.7,
"tags": ["acción", "sci-fi"]
},
{
"title": "The Lord of the Rings: The Return of the King",
"rating": 8.9,
"tags": ["fantasía", "aventura"]
},
{
"title": "Interstellar",
"rating": 9.8,
"tags": ["sci-fi", "drama","1"]
},
{
"title": "Gladiator",
"rating": 8.5,
"tags": ["acción", "drama"]
},
{
"title": "The Silence of the Lambs",
"rating": 8.6,
"tags": ["thriller", "drama"]
},
{
"title": "Saving Private Ryan",
"rating": 8.6,
"tags": ["acción", "drama", "bélico"]
},
{
"title": "Schindler's List",
"rating": 8.9,
"tags": ["drama", "histórico"]
},
{
"title": "The Social Network",
"rating": 7.7,
"tags": ["biografía", "drama"]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment