Skip to content

Commit 9a38d6c

Browse files
committed
#27 - JavaScript
1 parent 679d856 commit 9a38d6c

File tree

2 files changed

+85
-55
lines changed

2 files changed

+85
-55
lines changed

Roadmap/25 - LOGS/javascript/RicJDev.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const logger = winston.createLogger({
1111
format: winston.format.combine(
1212
winston.format.timestamp({ format: 'DD-MM-YYYY HH:mm:ss' }),
1313
winston.format.printf(({ timestamp, level, message }) => {
14-
return `${timestamp} - [${level.toUpperCase()}]: ${message}`;
14+
return `[${level.toUpperCase()}] - ${timestamp} : ${message}`;
1515
})
1616
),
1717
transports: [new winston.transports.Console()],
@@ -50,58 +50,86 @@ customLevelsLogger.log('Alerta amarilla', 'se muestra en amarillo');
5050
customLevelsLogger.log('Alerta verde', 'se muestra en verde');
5151

5252
//EXTRA
53-
const taskManagerLogger = winston.createLogger({
53+
const taskManagerLogs = winston.createLogger({
5454
level: 'debug',
5555
format: winston.format.combine(
5656
winston.format.timestamp({ format: 'DD-MM-YYYY HH:mm:ss' }),
5757
winston.format.printf(({ timestamp, level, message }) => {
58-
return `${timestamp} - [${level.toUpperCase()}]: ${message}`;
58+
return `[${level.toUpperCase()}] - ${timestamp} : ${message}`;
5959
})
6060
),
61-
transports: [new winston.transports.File({ filename: 'RicJDev.txt' })],
61+
transports: [new winston.transports.Console()],
6262
});
6363

64-
function addToTaskManagerLogger(fun, level, message) {
65-
taskManagerLogger.log(level, message);
64+
function timeLog(fun, log) {
6665
return function (...arg) {
67-
return fun.apply(this, arg);
66+
const start = new Date();
67+
const execute = fun.apply(this, arg);
68+
const end = new Date();
69+
70+
log.debug(
71+
`tiempo de ejecución de ${fun.name}: ${end.getTime() - start.getTime()} milisegundos`
72+
);
73+
74+
return execute;
6875
};
6976
}
70-
7177
class TaskManager {
72-
taskList = new Map();
78+
#taskList = new Map();
7379

7480
addTask(task, description) {
75-
this.taskList.set(task, description);
81+
if (this.#taskList.has(task)) {
82+
taskManagerLogs.warn(`se esta intentando agregar una tarea existente: ${task}`);
83+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
84+
} else {
85+
this.#taskList.set(task, description);
86+
87+
taskManagerLogs.info(`se ha agregado una tarea a la lista: ${task}`);
88+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
89+
}
7690
}
7791

7892
deleteTask(task) {
79-
if (this.taskList.has(task)) {
80-
this.taskList.delete(task);
93+
if (this.#taskList.has(task)) {
94+
this.#taskList.delete(task);
95+
96+
taskManagerLogs.info(`se ha eliminado una tarea de la lista: ${task}`);
97+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
98+
} else {
99+
taskManagerLogs.error(`se esta intentando eliminar una tarea que no existe: ${task}`);
100+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
81101
}
82102
}
83103

84104
listTasks() {
85-
if (this.taskList.size > 0) {
86-
this.taskList.forEach((description, task) => {
105+
if (this.#taskList.size > 0) {
106+
console.log('\n---- LISTA DE TAREAS ----');
107+
this.#taskList.forEach((description, task) => {
87108
console.log(`- ${task}: ${description}`);
88109
});
110+
console.log('-------------------------\n');
111+
112+
taskManagerLogs.info('se ha mostrado la lista de tareas al usuario');
113+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
89114
} else {
90-
console.log('No hay tareas registradas');
115+
taskManagerLogs.info('no hay tareas para mostrar');
116+
taskManagerLogs.debug(`cantidad de tareas ${this.#taskList.size}`);
91117
}
92118
}
93119
}
94120

95-
TaskManager.prototype.addTask = addToTaskManagerLogger(
96-
TaskManager.prototype.addTask,
97-
'info',
98-
'se ha añadido una tarea a la lista'
99-
);
121+
TaskManager.prototype.addTask = timeLog(TaskManager.prototype.addTask, taskManagerLogs);
122+
TaskManager.prototype.deleteTask = timeLog(TaskManager.prototype.deleteTask, taskManagerLogs);
123+
TaskManager.prototype.listTasks = timeLog(TaskManager.prototype.listTasks, taskManagerLogs);
100124

101-
let myTaskManager = new TaskManager();
125+
const myTaskManager = new TaskManager();
102126

103127
myTaskManager.addTask('Juan', 'felicitar a Juan por su cumpleaños');
104128
myTaskManager.addTask('Pan', 'ir a comprar pan');
129+
myTaskManager.addTask('Pan', 'ir a comprar pan');
130+
myTaskManager.listTasks();
131+
myTaskManager.deleteTask('Juan');
105132
myTaskManager.listTasks();
106133
myTaskManager.deleteTask('Juan');
134+
myTaskManager.deleteTask('Pan');
107135
myTaskManager.listTasks();

Roadmap/27 - SOLID OCP/javascript/RicJDev.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Circle extends GeometricShape {
9191

9292
class AreaCalculator {
9393
calculateArea(shape) {
94-
return shape.getArea(); //puede usarse shape.area también
94+
return shape.getArea();
9595
}
9696
}
9797

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

105105
displayAreas() {
106-
console.log('Rectangulo:', this.areaCalc.calculateArea(this.rectangle));
106+
console.log('Rectángulo:', this.areaCalc.calculateArea(this.rectangle));
107107
console.log('Cuadrado:', this.areaCalc.calculateArea(this.square));
108-
console.log('Circulo:', this.areaCalc.calculateArea(this.circle));
108+
console.log('Círculo:', this.areaCalc.calculateArea(this.circle));
109109
}
110110
}
111111

@@ -116,12 +116,8 @@ app.displayAreas();
116116
//EXTRA
117117
console.log('\nCalculadora');
118118
class Operation {
119-
constructor(name) {
120-
this.name = name;
121-
}
122-
123119
calculate(a, b) {
124-
return 'Operación no soportada';
120+
throw new Error('operación no soportada');
125121
}
126122
}
127123

@@ -131,7 +127,7 @@ class Add extends Operation {
131127
}
132128
}
133129

134-
class Susbtrac extends Operation {
130+
class Susbtract extends Operation {
135131
calculate(a, b) {
136132
return a - b;
137133
}
@@ -150,43 +146,49 @@ class Divide extends Operation {
150146
}
151147

152148
class Calculator {
153-
#operations = [];
154-
155-
addOperation(operation) {
156-
this.#operations.push(operation);
157-
}
149+
operations = {
150+
add: new Add(),
151+
substract: new Susbtract(),
152+
multiply: new Multiply(),
153+
divide: new Divide(),
154+
};
158155

159-
calculate(operation, a, b) {
160-
let fun = new Operation().calculate;
156+
calculate(operationType, a, b) {
157+
let operation = this.operations[operationType] || new Operation();
161158

162-
this.#operations.forEach((op) => {
163-
if (op.name === operation) {
164-
fun = op.calculate;
165-
}
166-
});
167-
168-
return fun(a, b);
159+
try {
160+
return operation.calculate(a, b);
161+
} catch (error) {
162+
console.log(`Se produjo un error: ${error.message}`);
163+
return NaN;
164+
}
169165
}
170166
}
171167

172168
const calculator = new Calculator();
173169

174-
calculator.addOperation(new Add('Suma'));
175-
calculator.addOperation(new Susbtrac('Resta'));
176-
calculator.addOperation(new Multiply('Multiplicación'));
177-
calculator.addOperation(new Divide('División'));
170+
let result = calculator.calculate('add', 12, 10);
171+
console.log('Suma:', result);
172+
173+
result = calculator.calculate('substract', 10, 2);
174+
console.log('Resta:', result);
175+
176+
result = calculator.calculate('multiply', 10, 2);
177+
console.log('Multiplicación:', result);
178178

179-
console.log('Suma:', calculator.calculate('Suma', 10, 1));
180-
console.log('Resta:', calculator.calculate('Resta', 10, 1));
181-
console.log('Multiplicación:', calculator.calculate('Multiplicación', 4, 2));
182-
console.log('División:', calculator.calculate('División', 10, 2));
183-
console.log('Potencia:', calculator.calculate('Potencia', 3, 2));
179+
result = calculator.calculate('divide', 10, 2);
180+
console.log('División:', result);
181+
182+
result = calculator.calculate('exponent', 10, 2);
183+
console.log('Potencia:', result);
184184

185185
class Exponent extends Operation {
186186
calculate(a, b) {
187187
return a ** b;
188188
}
189189
}
190190

191-
calculator.addOperation(new Exponent('Potencia'));
192-
console.log('Potencia:', calculator.calculate('Potencia', 3, 2));
191+
calculator.operations.exponent = new Exponent();
192+
193+
result = calculator.calculate('exponent', 10, 2);
194+
console.log('Potencia:', result);

0 commit comments

Comments
 (0)