Skip to content

Commit

Permalink
#27 - JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
RicJDev committed Jul 4, 2024
1 parent 679d856 commit 9a38d6c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 55 deletions.
70 changes: 49 additions & 21 deletions Roadmap/25 - LOGS/javascript/RicJDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({ format: 'DD-MM-YYYY HH:mm:ss' }),
winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} - [${level.toUpperCase()}]: ${message}`;
return `[${level.toUpperCase()}] - ${timestamp} : ${message}`;
})
),
transports: [new winston.transports.Console()],
Expand Down Expand Up @@ -50,58 +50,86 @@ customLevelsLogger.log('Alerta amarilla', 'se muestra en amarillo');
customLevelsLogger.log('Alerta verde', 'se muestra en verde');

//EXTRA
const taskManagerLogger = winston.createLogger({
const taskManagerLogs = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp({ format: 'DD-MM-YYYY HH:mm:ss' }),
winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} - [${level.toUpperCase()}]: ${message}`;
return `[${level.toUpperCase()}] - ${timestamp} : ${message}`;
})
),
transports: [new winston.transports.File({ filename: 'RicJDev.txt' })],
transports: [new winston.transports.Console()],
});

function addToTaskManagerLogger(fun, level, message) {
taskManagerLogger.log(level, message);
function timeLog(fun, log) {
return function (...arg) {
return fun.apply(this, arg);
const start = new Date();
const execute = fun.apply(this, arg);
const end = new Date();

log.debug(
`tiempo de ejecución de ${fun.name}: ${end.getTime() - start.getTime()} milisegundos`
);

return execute;
};
}

class TaskManager {
taskList = new Map();
#taskList = new Map();

addTask(task, description) {
this.taskList.set(task, description);
if (this.#taskList.has(task)) {
taskManagerLogs.warn(`se esta intentando agregar una tarea existente: ${task}`);
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
} else {
this.#taskList.set(task, description);

taskManagerLogs.info(`se ha agregado una tarea a la lista: ${task}`);
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
}
}

deleteTask(task) {
if (this.taskList.has(task)) {
this.taskList.delete(task);
if (this.#taskList.has(task)) {
this.#taskList.delete(task);

taskManagerLogs.info(`se ha eliminado una tarea de la lista: ${task}`);
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
} else {
taskManagerLogs.error(`se esta intentando eliminar una tarea que no existe: ${task}`);
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
}
}

listTasks() {
if (this.taskList.size > 0) {
this.taskList.forEach((description, task) => {
if (this.#taskList.size > 0) {
console.log('\n---- LISTA DE TAREAS ----');
this.#taskList.forEach((description, task) => {
console.log(`- ${task}: ${description}`);
});
console.log('-------------------------\n');

taskManagerLogs.info('se ha mostrado la lista de tareas al usuario');
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
} else {
console.log('No hay tareas registradas');
taskManagerLogs.info('no hay tareas para mostrar');
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
}
}
}

TaskManager.prototype.addTask = addToTaskManagerLogger(
TaskManager.prototype.addTask,
'info',
'se ha añadido una tarea a la lista'
);
TaskManager.prototype.addTask = timeLog(TaskManager.prototype.addTask, taskManagerLogs);
TaskManager.prototype.deleteTask = timeLog(TaskManager.prototype.deleteTask, taskManagerLogs);
TaskManager.prototype.listTasks = timeLog(TaskManager.prototype.listTasks, taskManagerLogs);

let myTaskManager = new TaskManager();
const myTaskManager = new TaskManager();

myTaskManager.addTask('Juan', 'felicitar a Juan por su cumpleaños');
myTaskManager.addTask('Pan', 'ir a comprar pan');
myTaskManager.addTask('Pan', 'ir a comprar pan');
myTaskManager.listTasks();
myTaskManager.deleteTask('Juan');
myTaskManager.listTasks();
myTaskManager.deleteTask('Juan');
myTaskManager.deleteTask('Pan');
myTaskManager.listTasks();
70 changes: 36 additions & 34 deletions Roadmap/27 - SOLID OCP/javascript/RicJDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Circle extends GeometricShape {

class AreaCalculator {
calculateArea(shape) {
return shape.getArea(); //puede usarse shape.area también
return shape.getArea();
}
}

Expand All @@ -103,9 +103,9 @@ class App {
areaCalc = new AreaCalculator();

displayAreas() {
console.log('Rectangulo:', this.areaCalc.calculateArea(this.rectangle));
console.log('Rectángulo:', this.areaCalc.calculateArea(this.rectangle));
console.log('Cuadrado:', this.areaCalc.calculateArea(this.square));
console.log('Circulo:', this.areaCalc.calculateArea(this.circle));
console.log('Círculo:', this.areaCalc.calculateArea(this.circle));
}
}

Expand All @@ -116,12 +116,8 @@ app.displayAreas();
//EXTRA
console.log('\nCalculadora');
class Operation {
constructor(name) {
this.name = name;
}

calculate(a, b) {
return 'Operación no soportada';
throw new Error('operación no soportada');
}
}

Expand All @@ -131,7 +127,7 @@ class Add extends Operation {
}
}

class Susbtrac extends Operation {
class Susbtract extends Operation {
calculate(a, b) {
return a - b;
}
Expand All @@ -150,43 +146,49 @@ class Divide extends Operation {
}

class Calculator {
#operations = [];

addOperation(operation) {
this.#operations.push(operation);
}
operations = {
add: new Add(),
substract: new Susbtract(),
multiply: new Multiply(),
divide: new Divide(),
};

calculate(operation, a, b) {
let fun = new Operation().calculate;
calculate(operationType, a, b) {
let operation = this.operations[operationType] || new Operation();

this.#operations.forEach((op) => {
if (op.name === operation) {
fun = op.calculate;
}
});

return fun(a, b);
try {
return operation.calculate(a, b);
} catch (error) {
console.log(`Se produjo un error: ${error.message}`);
return NaN;
}
}
}

const calculator = new Calculator();

calculator.addOperation(new Add('Suma'));
calculator.addOperation(new Susbtrac('Resta'));
calculator.addOperation(new Multiply('Multiplicación'));
calculator.addOperation(new Divide('División'));
let result = calculator.calculate('add', 12, 10);
console.log('Suma:', result);

result = calculator.calculate('substract', 10, 2);
console.log('Resta:', result);

result = calculator.calculate('multiply', 10, 2);
console.log('Multiplicación:', result);

console.log('Suma:', calculator.calculate('Suma', 10, 1));
console.log('Resta:', calculator.calculate('Resta', 10, 1));
console.log('Multiplicación:', calculator.calculate('Multiplicación', 4, 2));
console.log('División:', calculator.calculate('División', 10, 2));
console.log('Potencia:', calculator.calculate('Potencia', 3, 2));
result = calculator.calculate('divide', 10, 2);
console.log('División:', result);

result = calculator.calculate('exponent', 10, 2);
console.log('Potencia:', result);

class Exponent extends Operation {
calculate(a, b) {
return a ** b;
}
}

calculator.addOperation(new Exponent('Potencia'));
console.log('Potencia:', calculator.calculate('Potencia', 3, 2));
calculator.operations.exponent = new Exponent();

result = calculator.calculate('exponent', 10, 2);
console.log('Potencia:', result);

0 comments on commit 9a38d6c

Please sign in to comment.