From 7c146d428ff7a7e96f14cc364c7424488d3d19a8 Mon Sep 17 00:00:00 2001 From: Federico Krenn Date: Wed, 13 Dec 2023 11:34:51 -0300 Subject: [PATCH] Add solutions for news exercices --- .../01-node-callbacks-promises/input.txt | 1 - .../01-node-callbacks-promises/input2.txt | 1 - .../01-node-callbacks-promises/output.txt | 1 - .../01-node-callbacks-promises/output2.txt | 1 - .../01-node-callbacks-promises/package.json | 4 +- .../solutions/index.js | 38 ++++-- .../tests/dotenv.test.js | 114 +++++++++--------- .../tests/index.test.js | 37 ++++-- 8 files changed, 114 insertions(+), 83 deletions(-) delete mode 100644 live-coding/01-node-callbacks-promises/input.txt delete mode 100644 live-coding/01-node-callbacks-promises/input2.txt delete mode 100644 live-coding/01-node-callbacks-promises/output.txt delete mode 100644 live-coding/01-node-callbacks-promises/output2.txt diff --git a/live-coding/01-node-callbacks-promises/input.txt b/live-coding/01-node-callbacks-promises/input.txt deleted file mode 100644 index 23c86b80a..000000000 --- a/live-coding/01-node-callbacks-promises/input.txt +++ /dev/null @@ -1 +0,0 @@ -federico \ No newline at end of file diff --git a/live-coding/01-node-callbacks-promises/input2.txt b/live-coding/01-node-callbacks-promises/input2.txt deleted file mode 100644 index b8b4a4e2a..000000000 --- a/live-coding/01-node-callbacks-promises/input2.txt +++ /dev/null @@ -1 +0,0 @@ -hola \ No newline at end of file diff --git a/live-coding/01-node-callbacks-promises/output.txt b/live-coding/01-node-callbacks-promises/output.txt deleted file mode 100644 index fe0bf9608..000000000 --- a/live-coding/01-node-callbacks-promises/output.txt +++ /dev/null @@ -1 +0,0 @@ -FEDERICO \ No newline at end of file diff --git a/live-coding/01-node-callbacks-promises/output2.txt b/live-coding/01-node-callbacks-promises/output2.txt deleted file mode 100644 index 34af425be..000000000 --- a/live-coding/01-node-callbacks-promises/output2.txt +++ /dev/null @@ -1 +0,0 @@ -HOLA \ No newline at end of file diff --git a/live-coding/01-node-callbacks-promises/package.json b/live-coding/01-node-callbacks-promises/package.json index 86c267622..a9fba3b57 100644 --- a/live-coding/01-node-callbacks-promises/package.json +++ b/live-coding/01-node-callbacks-promises/package.json @@ -8,7 +8,9 @@ "test": "tests" }, "scripts": { - "test": "node --test" + "test": "echo \"Please use pnpm test1 or pnpm test2\"", + "test1": "node tests/index.test.js", + "test2": "node tests/dotenv.test.js" }, "keywords": [], "author": "Federico Krenn", diff --git a/live-coding/01-node-callbacks-promises/solutions/index.js b/live-coding/01-node-callbacks-promises/solutions/index.js index 22a205630..f5286f1ae 100644 --- a/live-coding/01-node-callbacks-promises/solutions/index.js +++ b/live-coding/01-node-callbacks-promises/solutions/index.js @@ -2,12 +2,12 @@ import net from 'node:net' import fs from 'node:fs' import { readFile, writeFile } from 'node:fs/promises' -export const ping = (ip, callbk) => { +export const ping = (ip, callbk) => { // Se agrega el cllbk como parámetro const startTime = process.hrtime() const client = net.connect({ port: 80, host: ip }, () => { client.end() - callbk(null, { time: process.hrtime(startTime), ip }) + callbk(null, { time: process.hrtime(startTime), ip }) // se cambia el return por el callbk }) client.on('error', (err) => { @@ -43,25 +43,25 @@ obtenerDatosPromise({ time: 1 }) */ export function procesarArchivo (callbk) { - const handleWrite = error => { + const handleRead = (error, contenido) => { if (error) { - console.error('Error guardando archivo:', error.message) + console.error('Error leyendo archivo:', error.message) callbk(error) } - console.log('Archivo procesado y guardado con éxito') - callbk(null) + const textoProcesado = contenido.toUpperCase() + + fs.writeFile('output.txt', textoProcesado, handleWrite) } - const handleRead = (error, contenido) => { + const handleWrite = error => { if (error) { - console.error('Error leyendo archivo:', error.message) + console.error('Error guardando archivo:', error.message) callbk(error) } - const textoProcesado = contenido.toUpperCase() - - fs.writeFile('output.txt', textoProcesado, handleWrite) + console.log('Archivo procesado y guardado con éxito') + callbk(null) } fs.readFile('input.txt', 'utf8', handleRead) @@ -72,3 +72,19 @@ export async function procesarArchivoPromise () { const textoProcesado = contenido.toUpperCase() await writeFile('output2.txt', textoProcesado) } + +export async function leerArchivos () { + const data = await Promise.all([ // Se podría usar all o allSettled, pero allSettled es más seguro porque no falla si uno de los archivos no existe + readFile('archivo1.txt', 'utf8'), + readFile('archivo2.txt', 'utf8'), + readFile('archivo3.txt', 'utf8') + ]) + + return data.join(' ') +} + +export async function delay (time) { + return new Promise(resolve => { + setTimeout(resolve, time) + }) +} diff --git a/live-coding/01-node-callbacks-promises/tests/dotenv.test.js b/live-coding/01-node-callbacks-promises/tests/dotenv.test.js index 9c3c389b1..c5cef3491 100644 --- a/live-coding/01-node-callbacks-promises/tests/dotenv.test.js +++ b/live-coding/01-node-callbacks-promises/tests/dotenv.test.js @@ -1,57 +1,57 @@ -import { describe, it, beforeEach, afterEach } from 'node:test' -import { equal } from 'node:assert/strict' -import { unlinkSync, writeFileSync } from 'node:fs' -import { createRequire } from 'node:module' -import { config } from '../solutions/dotenv.js' - -describe('1. dotenv', () => { - beforeEach(() => { - // clean process.env - for (const key of Object.keys(process.env)) { - delete process.env[key] - } - }) - - afterEach(() => { - try { - unlinkSync('.env') - } catch {} - - try { - unlinkSync('./test/.env.local') - } catch {} - }) - - it('1.1. load .env file', () => { - // create .env file in root directory - writeFileSync('.env', 'PORT=3000\nTOKEN="123abc"') - config() - - equal(process.env.PORT, '3000') - equal(process.env.TOKEN, '123abc') - }) - - it('1.2. load .env file from custom path', () => { - // create .env file in root directory - writeFileSync('./test/.env.local', 'PORT=3000\nTOKEN="123abc"') - config({ path: './test/.env.local' }) - - equal(process.env.PORT, '3000') - equal(process.env.TOKEN, '123abc') - }) - - it('1.3 it works even without .env file', () => { - config() - equal(process.env.TOKEN, undefined) - }) - - it('1.4 dont use dotenv dependency', () => { - // check that dotenv dependency is not installed - try { - const require = createRequire(import.meta.url) - require('dotenv') - } catch (error) { - equal(error.code, 'MODULE_NOT_FOUND') - } - }) -}) +// import { describe, it, beforeEach, afterEach } from 'node:test' +// import { equal } from 'node:assert/strict' +// import { unlinkSync, writeFileSync } from 'node:fs' +// import { createRequire } from 'node:module' +// import { config } from '../solutions/dotenv.js' + +// describe('1. dotenv', () => { +// beforeEach(() => { +// // clean process.env +// for (const key of Object.keys(process.env)) { +// delete process.env[key] +// } +// }) + +// afterEach(() => { +// try { +// unlinkSync('.env') +// } catch {} + +// try { +// unlinkSync('./test/.env.local') +// } catch {} +// }) + +// it('1.1. load .env file', () => { +// // create .env file in root directory +// writeFileSync('.env', 'PORT=3000\nTOKEN="123abc"') +// config() + +// equal(process.env.PORT, '3000') +// equal(process.env.TOKEN, '123abc') +// }) + +// it('1.2. load .env file from custom path', () => { +// // create .env file in root directory +// writeFileSync('./test/.env.local', 'PORT=3000\nTOKEN="123abc"') +// config({ path: './test/.env.local' }) + +// equal(process.env.PORT, '3000') +// equal(process.env.TOKEN, '123abc') +// }) + +// it('1.3 it works even without .env file', () => { +// config() +// equal(process.env.TOKEN, undefined) +// }) + +// it('1.4 dont use dotenv dependency', () => { +// // check that dotenv dependency is not installed +// try { +// const require = createRequire(import.meta.url) +// require('dotenv') +// } catch (error) { +// equal(error.code, 'MODULE_NOT_FOUND') +// } +// }) +// }) diff --git a/live-coding/01-node-callbacks-promises/tests/index.test.js b/live-coding/01-node-callbacks-promises/tests/index.test.js index 9a19dad7a..8abce43b8 100644 --- a/live-coding/01-node-callbacks-promises/tests/index.test.js +++ b/live-coding/01-node-callbacks-promises/tests/index.test.js @@ -1,9 +1,8 @@ -import { ping, obtenerDatosPromise, procesarArchivo, procesarArchivoPromise } from '../solutions/index.js' -import { describe, it, beforeEach, afterEach } from 'node:test' +import { ping, obtenerDatosPromise, procesarArchivo, procesarArchivoPromise, leerArchivos, delay } from '../solutions/index.js' +import { describe, it, afterEach } from 'node:test' import { equal, ifError } from 'node:assert/strict' import { unlinkSync, writeFileSync } from 'node:fs' import { readFile } from 'node:fs/promises' -import { createRequire } from 'node:module' describe('1. ping', () => { it('1.1. ping midu.dev', (_, done) => { @@ -26,6 +25,9 @@ describe('3. procesarArchivoPromise', () => { afterEach(() => { try { unlinkSync('output.txt') + unlinkSync('output2.txt') + unlinkSync('input.txt') + unlinkSync('input2.txt') } catch {} }) @@ -48,16 +50,31 @@ describe('3. procesarArchivoPromise', () => { equal(contenido, 'HOLA') }) }) -/* -describe('4. leerArchivos', () => { - // it('4.1. leerArchivos', () => { - // const mensaje = leerArchivos() - // equal(mensaje, 'hola qué tal') - // }) +describe('4. leerArchivos', () => { + afterEach(() => { + try { + unlinkSync('archivo1.txt') + unlinkSync('archivo2.txt') + unlinkSync('archivo3.txt') + } catch {} + }) it('4.1. leerArchivos', async () => { + writeFileSync('archivo1.txt', 'hola') + writeFileSync('archivo2.txt', 'qué') + writeFileSync('archivo3.txt', 'tal') const mensaje = await leerArchivos() equal(mensaje, 'hola qué tal') }) }) -*/ + +describe('5. delay', () => { + it('5.1. delay', async () => { + const timeToDelay = 1000 + const start = Date.now() + await delay(timeToDelay) + const end = Date.now() + const diff = end - start + equal(diff >= timeToDelay, true, 'La función delay debería retrasar la ejecución al menos 1000ms') + }) +})