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

Task 3 #3

Open
wants to merge 11 commits into
base: main
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
.vscode/
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# node-mentoring-introduction
Task 1
Task 3
https://d17btkcdsmqrmh.cloudfront.net/node-gmp/docs/event-emitter-vs-buffers-vs-streams/Homework

## Subtask 1
Write your own EventEmitter class.
It must contain
- <code>on / addEventListener</code> method,
- <code>off / removeEventListener</code> method,
- <code>once</code> method,
- <code>emit</code> method,
- <code>rawListeners</code> method,
- <code>listenerCount</code> method.
This basic features are sufficient to implement a full system using the eventing model.

Test result:

<img width="683" alt="2023-09-14_22h43_54" src="https://github.com/kandalova/node-mentoring-introduction/assets/26093763/709017e9-e37b-4e71-9c24-22843d8c2c45">

## Subtask 2
Write a class <code>WithTime</code> which should extend your own class EventEmitter.

<code>WithTime</code> must execute some Async function with arguments and compute the time taken by this function.

Execution must emit event start and end of execute and console.log of result data. This data must be gotten from Async function.

Test result:

![2023-09-15_20h27_13](https://github.com/kandalova/node-mentoring-introduction/assets/26093763/953a3aae-04fb-4f63-a231-b045a5febb96)

## Subtask 3

Write a program which should do the following:
- Read the content of csvfile from./csvdirectory
- Use the csvtojson package to convert csvfile to json object
- Write the csvfile content to a new txtfile.
- Do not load all the content of the csvfile into RAM via stream (read/write file content line by line).
- In case of read/write errors, log them in the console.
- The program should be started via npm script using nodemon(i.e. npm run task2).

Test result

Parsed csv strings

<img width="402" alt="2023-09-15_22h50_54" src="https://github.com/kandalova/node-mentoring-introduction/assets/26093763/2f691522-66b0-48df-a240-474d546e53f7">

Generated file

<img width="769" alt="2023-09-15_22h52_06" src="https://github.com/kandalova/node-mentoring-introduction/assets/26093763/addeb9fd-7123-4a3d-98da-2380a2679284">
6 changes: 6 additions & 0 deletions csvdirectory/nodejs-hw1-ex1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Book,Author,Amount,Price
The Compound Effect,Darren Hardy,5,9.48
The 7 Habits of Highly Effective People,Stephen R. Covey,4,23.48
The Miracle Morning,Hal Elrod,10,21.34
Influence: The Psychology of Persuasion,Robert B. Cialdini,4,12.99
The ONE Thing,Gary Keller,1,11.18
49 changes: 49 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import EventEmitter from "./utils/EventEmitter.js";

const myEmitter = new EventEmitter();

function c1() {
console.log('an event occurred!');
}

function c2() {
console.log('yet another event occurred!');
}

myEmitter.on('eventOne', c1); // Register for eventOne
myEmitter.on('eventOne', c2); // Register for eventOne

// Register eventOnce for one time execution
myEmitter.once('eventOnce', () => console.log('eventOnce once fired'));
myEmitter.once('init', () => console.log('init once fired'));

// Register for 'status' event with parameters
myEmitter.on('status', (code, msg)=> console.log(`Got ${code} and ${msg}`));


myEmitter.emit('eventOne');

// Emit 'eventOnce' -> After this the eventOnce will be
// removed/unregistered automatically
myEmitter.emit('eventOnce');


myEmitter.emit('eventOne');
myEmitter.emit('init');
myEmitter.emit('init'); // Will not be fired
myEmitter.emit('eventOne');
myEmitter.emit('status', 200, 'ok');

// Get listener's count
console.log(myEmitter.listenerCount('eventOne'));

// Get array of rawListeners//
// Event registered with 'once()' will not be available here after the
// emit has been called
console.log(myEmitter.rawListeners('eventOne'));

// Get listener's count after remove one or all listeners of 'eventOne'
myEmitter.off('eventOne', c1);
console.log(myEmitter.listenerCount('eventOne'));
myEmitter.off('eventOne', c2);
console.log(myEmitter.listenerCount('eventOne'));
Loading