Last active
May 28, 2021 13:22
-
-
Save rje1974/63cc5ce9ba9355d7de5bf971e08601f7 to your computer and use it in GitHub Desktop.
Desafio Módulo 1
This file contains 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 pelis = require("./pelis"); | |
function parsearARGV() { | |
// const comandosAzules = [] | |
const argumento = process.argv.slice(2); | |
const listaDeArgumentos = [] | |
mapearArgumento = argumento.map(function (element,index) { | |
if (element.includes("--no-format")) { | |
element = "noformat"; | |
return listaDeArgumentos.push({[element]: 1}) | |
} if (element.includes("--")) { | |
element = element.slice(2); | |
return listaDeArgumentos.push({[element]: argumento[index + 1]}) | |
} | |
} | |
) | |
return listaDeArgumentos; | |
} | |
function main() { | |
const comandosAEjecutar = parsearARGV(process.argv); | |
pelis.searchByCriteria(comandosAEjecutar); | |
} | |
main(); |
This file contains 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 { title } = require('process'); | |
const getAll = function () { | |
const fs = require('fs'); | |
const archivoOriginal = fs.readFileSync(__dirname + "/pelis.json"); | |
const archivo2String = archivoOriginal.toString(); | |
const arrayDePelis = JSON.parse(archivo2String); | |
return arrayDePelis; | |
}; | |
const searchBy = function (texto, arrayDePelis) { | |
return arrayDePelis.filter(item => item.title.toLowerCase().indexOf(texto.toLowerCase()) !== -1); | |
}; | |
const searchByTags = function (texto, arrayDePelis) { | |
return arrayDePelis.filter(item => item.tags.indexOf(texto.toLowerCase()) !== -1) | |
}; | |
const sortBy = function (propiedad, arrayDePelis) { | |
const sorteada = arrayDePelis.sort(function(a, b){ | |
if(a[propiedad] > b[propiedad]){ | |
return 1 | |
} if(a[propiedad] < b[propiedad]){ | |
return -1 | |
} | |
return 0; | |
}) | |
return sorteada; | |
}; | |
exports.searchByCriteria = function (criterios){ | |
// comienzo un array vacio que voy a empezar a rellenar con las respuestas de las funciones | |
let tmp = getAll(); | |
criterios.map(function(criterios,b,c){ | |
if (criterios.search) { | |
// console.log("hay search y es", criterios.search); | |
tmp = searchBy(criterios.search, tmp); | |
} else { | |
// console.log("no hay search"); | |
} if (criterios.sort) { | |
//console.log("hay sort y es", criterios.sort); | |
tmp = sortBy(criterios.sort, tmp); | |
} else { | |
//console.log("no hay sort"); | |
} if (criterios.tags) { | |
//console.log("hay tags y es", criterios.tags); | |
tmp = searchByTags(criterios.tags, tmp); | |
} else { | |
// console.log("no hay tags"); | |
} if (criterios.noformat) { | |
//console.log("Hay noformat."); | |
tmp = JSON.stringify(tmp); | |
//console.log(tmp) | |
} else {// console.log('no hay noformat') | |
} | |
} | |
) | |
console.table(tmp)} |
This file contains 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": "Sospercha mortal", | |
"rating": 4, | |
"tags": ["acción", "favorita", "nueva"] | |
}, | |
{ | |
"title": "Descuida, Yo te cuido", | |
"rating": 4, | |
"tags": ["favorita", "nueva"] | |
}, | |
{ | |
"title": "La apariencia de las cosas", | |
"rating": 2, | |
"tags": ["terror", "favorita", "nueva"] | |
}, | |
{ | |
"title": "Volver al futuro III", | |
"rating": 3, | |
"tags": ["peliculas familiares", "comedias familiares", "westerns"] | |
}, | |
{ | |
"title": "Escuela de rock", | |
"rating": 3, | |
"tags": ["comedia", "favorita", "nueva"] | |
}, | |
{ | |
"title": "Policias de repuesto", | |
"rating": 4, | |
"tags": ["comedia", "favorita", "nueva"] | |
}, | |
{ | |
"title": "I am mother", | |
"rating": 2, | |
"tags": ["ciencia ficción", "robots", "nueva"] | |
}, | |
{ | |
"title": "MIB", | |
"rating": 4, | |
"tags": ["comedia", "favorita", "nueva"] | |
}, | |
{ | |
"title": "Rambo II", | |
"rating": 2, | |
"tags": ["accion", "favorita", "secuela"] | |
}, | |
{ | |
"title": "Rambo", | |
"rating": 3, | |
"tags": ["accion", "favorita", "stallone"] | |
}, | |
{ | |
"title": "Indiana jones, el templo de la perdición", | |
"rating": 5, | |
"tags": ["favorita", "aventura", "malditos nazis"] | |
}, | |
{ | |
"title": "Indiana jones, la ultima cruzada", | |
"rating": 4, | |
"tags": ["favorita", "aventura", "malditos nazis"] | |
}, | |
{ | |
"title": "Indiana jones y los cazadores del arca perdida", | |
"rating": 5, | |
"tags": ["favorita", "aventura", "malditos nazis"] | |
}, | |
{ | |
"title": "Indiana jones y el reino de la calavera de cristal", | |
"rating": 4, | |
"tags": ["favorita", "aventura", "malditos comunistas"] | |
}, | |
{ | |
"title": "Magic", | |
"rating": 4, | |
"tags": ["drama", "horror", ""] | |
}, | |
{ | |
"title": "Strange Magic", | |
"rating": 3, | |
"tags": ["animacion", "aventura", "comedia"] | |
}, | |
{ | |
"title": "Magic Mike", | |
"rating": 4, | |
"tags": ["comedia", "drama"] | |
}] |
Author
rje1974
commented
May 24, 2021
via email
•
Muchas gracias por la devolución. Cometí varios errores en el armado del
json que corregí y ese se me escapó. Como vos decís, en un contexto de
trabajo o evaluación, eso rompe todo. Me quedó la duda si debía resolver la
búsqueda del tag por la función searchBy; pero como vi que se me escapaba a
mis capacidades lo resolví con otra función más.. Como era la forma
correcta..? Saludos y buen fin de semana..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment