Created
May 22, 2025 16:40
-
-
Save sebaroldaan/88b7b5b8ba93f439f5bda26e44882d87 to your computer and use it in GitHub Desktop.
Desafío Buscador de Películas.
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
//index.js | |
const peliculas = require("./pelis"); | |
const pelis = peliculas.peliculas; | |
const filterbytitle = peliculas.filterbytitle; | |
const sortByProperty = peliculas.sortByProperty; | |
const filterbytags = peliculas.filterbytags; | |
function ejecutarPelis (args) { | |
const mapa = { | |
"--search": filterbytitle, | |
"--sort": sortByProperty, | |
"--tag": filterbytags | |
}; | |
const comando = args[0]; | |
const valor = args[1]; | |
const ejecutor = mapa[comando]; | |
if(ejecutor) { | |
return ejecutor(pelis, valor); | |
}else { | |
return pelis; | |
} | |
}; | |
function main(){ | |
const args = process.argv.slice(2); | |
const resultado = ejecutarPelis(args) | |
console.log(resultado); | |
} | |
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
//pelis.js | |
const fs = require('fs'); | |
const peli = fs.readFileSync(__dirname +"/pelis.json") | |
const peliculasEnTexto = peli.toString(); | |
const peliculas = JSON.parse(peliculasEnTexto) | |
function filterbytitle (peliculas, texto) { | |
return peliculas.filter(item => { | |
return item.title.toLowerCase().includes(texto.toLowerCase()) | |
}) | |
}; | |
function sortByProperty(peliculas, propiedad) { | |
return peliculas.sort((a, b) => { | |
if (typeof a[propiedad] === 'number' && typeof b[propiedad] === 'number') { | |
return a[propiedad] - b[propiedad]; // Orden ascendente | |
} | |
// Para propiedades de tipo string, ordena alfabéticamente | |
return a[propiedad].localeCompare(b[propiedad]); | |
}); | |
}; | |
function filterbytags (peliculas, tags) { | |
return peliculas.filter(item => { | |
return item.tags.some(tag => tag.toLowerCase() === tags.toLowerCase()); | |
}) | |
}; | |
module.exports = { | |
filterbytitle, | |
sortByProperty, | |
filterbytags, | |
peliculas | |
}; |
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": "rapidos y furiosos", | |
"rating": 4, | |
"tags": ["acción", "favorita", "nueva"] | |
}, | |
{ | |
"title": "la bella y la bestia", | |
"rating": 7, | |
"tags": ["romance", "drama", "historia"] | |
}, | |
{ | |
"title": "cars", | |
"rating": 5, | |
"tags": ["acción", "superhéroes", "drama"] | |
}, | |
{ | |
"title": "mi villano favorito", | |
"rating": 1, | |
"tags": ["drama", "thriller", "psicológico"] | |
}, | |
{ | |
"title": "la cenicienta", | |
"rating": 9, | |
"tags": ["acción", "drama", "historia"] | |
}, | |
{ | |
"title": "elite", | |
"rating": 8, | |
"tags": ["ciencia ficción", "thriller", "acción"] | |
}, | |
{ | |
"title": "dark", | |
"rating": 4, | |
"tags": ["ciencia ficción", "acción", "cyberpunk"] | |
}, | |
{ | |
"title": "clear", | |
"rating": 6, | |
"tags": ["ciencia ficción", "drama", "aventura"] | |
}, | |
{ | |
"title": "living the crazy life", | |
"rating": 3, | |
"tags": ["nueva"] | |
}, | |
{ | |
"title": "hola soy yo", | |
"rating": 2, | |
"tags": ["favorita"] | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment