Skip to content

Instantly share code, notes, and snippets.

@seba-campo
Last active February 28, 2022 00:34
Show Gist options
  • Save seba-campo/ca91f3c670a27a3bdeb8eb4282cb5030 to your computer and use it in GitHub Desktop.
Save seba-campo/ca91f3c670a27a3bdeb8eb4282cb5030 to your computer and use it in GitHub Desktop.
dwf desafío modulo 1
const pelis = require("./pelis.js");
function parsearARGV() {
//... acá ocurre la magia
const arguments = process.argv.slice(2);
let argObj = {
};
// Identificar los diferentes parámetros
if(arguments.includes('--help') || arguments.includes('--comands') || arguments.length == 0){
const leyenda = `
-----Comandos-----
"--no-format": Devuelve la lista de películas sin formato.
"--search": Buscar película por título
"--sort": Ordenar listado de películas por parametro (title, rating, etc)
"--tag": Devuelve las películas filtradas por el tag deseado.
Si no enviás ningún parámetro, mostrará una tabla con películas y este mensaje de --help
`;
console.log(leyenda);
};
if(arguments.length == 0 || arguments == null ){
argObj.noParameter = true;
};
if(arguments.includes('--no-format') || arguments.includes('--no-Format') ){
argObj.noFormat = true;
};
if(arguments.includes('--search')){
const argumentIndex = arguments.indexOf('--search');
const parameterValue = arguments[argumentIndex + 1];
argObj.search = parameterValue;
};
if(arguments.includes('--sort')){
const argumentIndex = arguments.indexOf('--sort');
const parameterValue = arguments[argumentIndex + 1];
argObj.sort = parameterValue;
};
if(arguments.includes('--tag')){
const argumentIndex = arguments.indexOf('--tag');
const parameterValue = arguments[argumentIndex + 1];
argObj.tag = parameterValue;
};
return argObj
}
function main() {
const comandosAEjecutar = parsearARGV();
console.log(pelis.searchByCriteria(comandosAEjecutar));
}
main();
const fs = require('fs');
const listaRaw = fs.readFileSync(__dirname + "/pelis.json");
// sacado del discord
const listaString = listaRaw.toString();
const lista = JSON.parse(listaString)
const getAll = function () {
// retorna todas las pelis (collection)
var arrayDePelis = lista;
return arrayDePelis
};
const noFormat = function(array){
// Recibe collection y lo retorna sin formato
const parsed = JSON.stringify(array);
const string = parsed.toString();
return string
};
const searchBy = function (texto, arrayDePelis) {
// Busca dentro del "arrayDePelis" el "texto" indicado
// y lo incluye en un nuevo array
var resultado = arrayDePelis.filter((x) => {
var titleLowered = x.title.toLowerCase();
const textoLowered = texto.toLowerCase();
return titleLowered.includes(textoLowered);
});
return resultado
};
const sortBy = function (propiedad, arrayDePelis) {
// ordena el arrayDePelis según la propiedad pasada
// NO CREA UN ARRAY NUEVO, modifica el existente.
var arrayOrdenado = arrayDePelis.sort((a,b) =>{
if(a[propiedad] < b[propiedad]){
return -1
}
if(a[propiedad] > b[propiedad]){
return 1
}else{
return 0
}
});
return arrayOrdenado
};
const sortByTag = function (propiedad, arrayDePelis) {
// Recibe un arrayDePelis y los filtra por la propiedad
// retorna otro array con los valores que correspondan
const propiedadLowered = propiedad.toLowerCase();
var filtrado = arrayDePelis.filter((x) => {
return x.tags.includes(propiedadLowered);
});
return filtrado
};
exports.searchByCriteria = function (criterios) {
let tmp = getAll();
if (criterios.search){
tmp = searchBy(criterios.search, tmp);
} else {
// console.log("no hay search");
};
if (criterios.sort){
tmp = sortBy(criterios.sort, tmp);
} else {
// console.log("no hay sort");
};
if (criterios.tag){
tmp = sortByTag(criterios.tag, tmp);
} else {
// console.log("no hay tags")
};
if (criterios.noParameter){
return lista
};
if (criterios.noFormat){
return noFormat(tmp)
};
// Si se ejecuta "noParameter" devuelve undefined,
// ya que "tmp" guarda un console.table() de la lista
return tmp;
};
[
{
"title": "Momia 1",
"rating": 6,
"tags": ["acción", "favorita", "nueva"]
},
{
"title": "Matrix",
"rating": 9,
"tags": ["favorita", "nueva", "futurista", "distopico"]
},
{
"title": "Suits",
"rating": 8,
"tags": ["series", "ley","favorita", "nueva"]
},
{
"title": "The walking Dead ",
"rating": 8,
"tags": ["accion", "drama", "series"]
},
{
"title": "Rapido y Furiosos red",
"rating": 7.5,
"tags": ["accion", "autos", "carreras"]
},
{
"title": "Gladiador red",
"rating": 10,
"tags": ["accion", "historico", "belico"]
},
{
"title": "El crochet para bebes",
"rating": 5,
"tags": ["manualidad", "tejidos"]
}
]
@alexismunoz1
Copy link

Hola @seba-campo felicitaciones por tu trabajo.
El desafío cumple con las condiciones de ser aprobado, éxitos en lo que sigue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment