Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MJ - Day2 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/data/zadanie01/sum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
108
10 changes: 5 additions & 5 deletions app/data/zadanieDnia/test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
You Don't Know JS: ES6 & Beyond
Foreword
YoU DoN'T KnOw JS: ES6 & BEyOnD
FoReWoRd

Kyle Simpson is a thorough pragmatist.
KyLe SImPsOn iS A ThOrOuGh pRaGmAtIsT.

I can't think of higher praise than this. To me, these are two of the most important qualities that a software developer must have. That's right: must, not should. Kyle's keen ability to tease apart layers of the JavaScript programming language and present them in understandable and meaningful portions is second to none.
[https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/foreword.md]
I CaN'T ThInK Of hIgHeR PrAiSe tHaN ThIs. To mE, tHeSe aRe tWo oF ThE MoSt iMpOrTaNt qUaLiTiEs tHaT A SoFtWaRe dEvElOpEr mUsT HaVe. ThAt's rIgHt: MuSt, NoT ShOuLd. KyLe's kEeN AbIlItY To tEaSe aPaRt lAyErS Of tHe JAvASCrIpT PrOgRaMmInG LaNgUaGe aNd pReSeNt tHeM In uNdErStAnDaBlE AnD MeAnInGfUl pOrTiOnS Is sEcOnD To nOnE.
[HtTpS://GiThUb.cOm/gEtIfY/YoU-DoNt-KNoW-JS/bLoB/MaStEr/eS6%20%26%20bEyOnD/FoReWoRd.mD]
7 changes: 7 additions & 0 deletions app/data/zadanieDnia/test_temp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
You Don't Know JS: ES6 & Beyond
Foreword

Kyle Simpson is a thorough pragmatist.

I can't think of higher praise than this. To me, these are two of the most important qualities that a software developer must have. That's right: must, not should. Kyle's keen ability to tease apart layers of the JavaScript programming language and present them in understandable and meaningful portions is second to none.
[https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/foreword.md]
13 changes: 12 additions & 1 deletion app/zadanie01.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
//Twój kod
const fs = require('fs');

fs.readFile('./data/zadanie01/input.json', 'utf-8', (err, data) => {
if (err) return console.log('Błąd odczytu pliku!');

let sum = JSON.parse(data).reduce((x, y) => x + y);

fs.writeFile('./data/zadanie01/sum.txt', sum, err => {
let msg = err ? 'Błąd zapisu pliku' : 'Plik poprawnie zapisany';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Dodatkowo, można było użyć const.

return console.log(msg)
});
});
15 changes: 14 additions & 1 deletion app/zadanie02.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
//Twój kod
const fs = require('fs');

let path = './data/zadanie02/';

fs.readdir(path, (err, files) => {
if (err) return console.log('Błąd odczytu katalogu');

files.forEach(file => {
console.log(`Nazwa pliku: ${file}`);
let data = fs.readFileSync(`${path}${file}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Staramy się zawsze używać funkcji asynchronicznych - dlatego tylko takie poznaliśmy. Dzięki temu kiedy Node.js "nudzi się" odczytem pliku - mogą się dziać inne rzeczy.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tak racja. Chciałem tym uzyskać odczyt pliku pod nazwą pliku bez rejestrowania eventu :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jasna sprawa :) Wygląda "prościej", ale właśnie nie korzysta z tej przewagi Node.
Natomiast taka ciekawostka, że używając Promise + async/await da się pisać kod który wygląda jak synchroniczny, a jest asynchroniczny ^^

console.log(data.toString());
console.log(`-----------------------------`);
});
});
23 changes: 22 additions & 1 deletion app/zadanieDnia.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
//Twój kod
const fs = require('fs');

const path = process.argv.slice(2).toString();

let trawka = (path) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) return console.log('Błąd odczytu pliku');

let newData = [...data].map((elem, i) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Najlepszy sposób.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dziękuję :)

return i % 2 === 0 ? elem.toUpperCase() : elem
})

console.log(newData.join(''))

fs.writeFile(path, newData.join(''), err => {
let msg = err ? 'Błąd zapisu pliku' : 'Plik poprawnie zapisany';
return console.log(msg);
})
});
};

trawka(path)