sushu 3.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JSON.js +11 -0
- package/baby.js +10 -0
- package/callbackhell.js +56 -0
- package/callbacks.js +42 -0
- package/copyingData.js +11 -0
- package/eventEmitter.js +42 -0
- package/fsmoduleQuestion.js +31 -0
- package/functions.js +8 -0
- package/index.js +5 -3
- package/input.txt +1 -0
- package/libraryManagementSystem.js +45 -0
- package/math.js +11 -3
- package/output.txt +1 -0
- package/package.json +1 -1
- package/promises.js +19 -0
- package/streams.js +70 -0
- package/test.json +7 -0
package/JSON.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const fs=require('fs');
|
|
2
|
+
fs.readFile("test.json",(err,data)=>{
|
|
3
|
+
if(err){
|
|
4
|
+
console.log(err);
|
|
5
|
+
}
|
|
6
|
+
else{
|
|
7
|
+
const test=JSON.parse(data); /**it converts string to object */
|
|
8
|
+
const sushu=JSON.stringify(test); /**it convers object to string */
|
|
9
|
+
console.log(sushu);
|
|
10
|
+
}
|
|
11
|
+
})
|
package/baby.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// init is used for initialize
|
|
2
|
+
// node filename- to execute
|
|
3
|
+
// npm login
|
|
4
|
+
// npm publish
|
|
5
|
+
// package.json contains all the metadata file
|
|
6
|
+
// package.json is what we want and package-Lock.json is what we get
|
|
7
|
+
// JavaScript executes based on TIME, not order of code, for async tasks.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// https://fsad-cse.vercel.app/ - web for full stank
|
package/callbackhell.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
|
|
2
|
+
// callback hell ???? and how we can solve it????
|
|
3
|
+
// callback is a function where it is passed as argument to another function and
|
|
4
|
+
// 1. callback hell hapens when we use nested callback functions.
|
|
5
|
+
|
|
6
|
+
// 2. Why node js uses callback
|
|
7
|
+
// It does not wait for slow tasks (file read, DB, API)
|
|
8
|
+
// It continues execution
|
|
9
|
+
// When task finishes → callback runs
|
|
10
|
+
|
|
11
|
+
// 3.Callback hell appears when:
|
|
12
|
+
// Task 2 depends on Task 1
|
|
13
|
+
// Task 3 depends on Task 2
|
|
14
|
+
// And so on…
|
|
15
|
+
|
|
16
|
+
// function step1(cb){
|
|
17
|
+
// setTimeout(() => {
|
|
18
|
+
// console.log("step 1 is done");
|
|
19
|
+
// cb();
|
|
20
|
+
// }, 1000);
|
|
21
|
+
// }
|
|
22
|
+
// function step2(cb){
|
|
23
|
+
// setTimeout(() => {
|
|
24
|
+
// console.log("step 2 is done");
|
|
25
|
+
// cb();
|
|
26
|
+
// }, 2000);
|
|
27
|
+
// }
|
|
28
|
+
// function step3(cb){
|
|
29
|
+
// setTimeout(() => {
|
|
30
|
+
// console.log("step 3 is done");
|
|
31
|
+
// cb();
|
|
32
|
+
// }, 3000);
|
|
33
|
+
// }
|
|
34
|
+
// step1(() => {
|
|
35
|
+
// step2(() => {
|
|
36
|
+
// step3(() => {
|
|
37
|
+
// console.log("all steps have finished thier execution");
|
|
38
|
+
// })
|
|
39
|
+
// })
|
|
40
|
+
// })
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// ................
|
|
44
|
+
function getUsers(callback){
|
|
45
|
+
callback({name:"sushu", id:"18"});
|
|
46
|
+
}
|
|
47
|
+
function getPosts(userId, callback){
|
|
48
|
+
callback([ "Post1","Post2","Post3"]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getUsers((user)=>{
|
|
52
|
+
console.log(user); /*user prints both name and id, but user.name prints only name , user.id printls only id**/
|
|
53
|
+
getPosts(user.id,(posts)=>{
|
|
54
|
+
console.log(posts);
|
|
55
|
+
});
|
|
56
|
+
});
|
package/callbacks.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// behaviour of js is asyncronous nature
|
|
2
|
+
function greet(){
|
|
3
|
+
console.log("Hello world");
|
|
4
|
+
}
|
|
5
|
+
setTimeout(greet, 5000)
|
|
6
|
+
// function will run after 5 milli sec i.e 5000 sec
|
|
7
|
+
console.log("This line is after settimeout.");
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// ARROW function is used beacuse function can run multiple time no need to write it again and again
|
|
11
|
+
setTimeout(()=>{
|
|
12
|
+
console.log("hello baby");
|
|
13
|
+
},5000);
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// to stop interval we need to preess cntl+c
|
|
17
|
+
// setInterval(()=>{
|
|
18
|
+
// console.log("hello baby");
|
|
19
|
+
// },1000);
|
|
20
|
+
|
|
21
|
+
// .......clear the value succesfuuly executed but no output
|
|
22
|
+
const value=setInterval(()=>{
|
|
23
|
+
console.log("sushu");
|
|
24
|
+
},2000);
|
|
25
|
+
setTimeout(()=>{
|
|
26
|
+
clearInterval(value);
|
|
27
|
+
},5000);
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function parent(name,callback){
|
|
31
|
+
console.log("Entering parent: " + name);
|
|
32
|
+
setTimeout(()=>{
|
|
33
|
+
callback();
|
|
34
|
+
},5000);
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
function child(){
|
|
38
|
+
console.log("Inside child");
|
|
39
|
+
}
|
|
40
|
+
parent("One",child); /*here name is one and child as callback**/
|
|
41
|
+
|
|
42
|
+
|
package/copyingData.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// copying data from to file to another
|
|
2
|
+
const fs=require('fs');
|
|
3
|
+
const readStream=fs.createReadStream("input.txt",{encoding:"utf8"});
|
|
4
|
+
const writeStream=fs.createWriteStream("output.txt");
|
|
5
|
+
readStream.on("data",(chunk)=>{
|
|
6
|
+
writeStream.write(chunk);
|
|
7
|
+
});
|
|
8
|
+
readStream.on("end",()=>{
|
|
9
|
+
writeStream.end("completed copying the data");
|
|
10
|
+
console.log("Succcesfully done!");
|
|
11
|
+
})
|
package/eventEmitter.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// const EventEmitter=require('events'); /*class is EventEmitter and obj is events**/
|
|
2
|
+
// const emitter=new EventEmitter();
|
|
3
|
+
// emitter.on('greet',(name)=>{
|
|
4
|
+
// console.log('hello, ${name}');
|
|
5
|
+
// });
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
// const EventEmitter=require('events');
|
|
9
|
+
// const emitter=new EventEmitter();
|
|
10
|
+
// function greetUser(name){
|
|
11
|
+
// console.log("hello", name);
|
|
12
|
+
// }
|
|
13
|
+
// emitter.on('greet',greetUser); /*listener**/
|
|
14
|
+
// // emitter.once('greet',greetUser); /*it will print only once**/
|
|
15
|
+
// emitter.emit('greet', 'sushu'); /*emitter**/
|
|
16
|
+
// emitter.emit('greet', 'baby');
|
|
17
|
+
// // emitter.removeListener('greet',greetUser);
|
|
18
|
+
// emitter.emit('greet','amma');
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const EventEmitter=require('events');
|
|
23
|
+
const emitter=new EventEmitter();
|
|
24
|
+
let count=0;
|
|
25
|
+
function greetUser(name){
|
|
26
|
+
if(count<4){
|
|
27
|
+
count++;
|
|
28
|
+
console.log("hello",name,count);
|
|
29
|
+
}
|
|
30
|
+
else{
|
|
31
|
+
emitter.removeListener('greet',greetUser);
|
|
32
|
+
console.log("stop listening");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
emitter.on('greet',greetUser);
|
|
36
|
+
emitter.emit('greet','sushu');
|
|
37
|
+
emitter.emit('greet','sushu');
|
|
38
|
+
emitter.emit('greet','sushu');
|
|
39
|
+
emitter.emit('greet','sushu');
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Find the function for append and delete in FS and perform them
|
|
2
|
+
// appending the data
|
|
3
|
+
const fs=require('fs');
|
|
4
|
+
fs.appendFileSync("input.txt","message appended in the file\n");
|
|
5
|
+
console.log("data to append syncronously!");
|
|
6
|
+
|
|
7
|
+
const fs=require('fs');
|
|
8
|
+
const data="suhsuuuu"
|
|
9
|
+
fs.appendFile("input.txt",data,(err)=>{
|
|
10
|
+
if(err){
|
|
11
|
+
console.log(err);
|
|
12
|
+
}
|
|
13
|
+
else{
|
|
14
|
+
console.log("data appended successfully!");
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
// deleting the data
|
|
19
|
+
const fs=require('fs');
|
|
20
|
+
fs.unlinkSync("input.txt");
|
|
21
|
+
console.log("data in file deleted successfully!!");
|
|
22
|
+
|
|
23
|
+
const fs=require('fs');
|
|
24
|
+
fs.unlink("input.txt",(err)=>{
|
|
25
|
+
if(err){
|
|
26
|
+
console.log(err);
|
|
27
|
+
}
|
|
28
|
+
else{
|
|
29
|
+
console.log("data deleted successfully!");
|
|
30
|
+
}
|
|
31
|
+
});
|
package/functions.js
ADDED
package/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const express=require('express');
|
|
2
|
-
const a=require('./math');
|
|
2
|
+
// const a=require('./math');
|
|
3
|
+
const s=require('./math');
|
|
3
4
|
const fs=require('fs');
|
|
4
5
|
console.log("hello!!");
|
|
5
|
-
console.log("sum is: ",a.add(3,2));
|
|
6
|
+
// console.log("sum is: ",a.add(3,2));
|
|
7
|
+
console.log("difference is: ",s.sub(9,5));
|
|
8
|
+
// here to run code - node filename and we learn to npm init, npm login and npm install express
|
|
6
9
|
|
|
7
|
-
// here to run code - node filename and we learn to init npm and npm install express
|
package/input.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
suhsuuuu
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// function getMem(callback) {
|
|
2
|
+
// callback({ name: "suhsu", id: 1 });
|
|
3
|
+
// }
|
|
4
|
+
|
|
5
|
+
// function issuedbook(memID, callback) {
|
|
6
|
+
// callback(["java"]);
|
|
7
|
+
// }
|
|
8
|
+
|
|
9
|
+
// function fine(books, callback) {
|
|
10
|
+
// callback(1000);
|
|
11
|
+
// }
|
|
12
|
+
|
|
13
|
+
// getMem((user) => {
|
|
14
|
+
// issuedbook(user.id, (books) => {
|
|
15
|
+
// fine(books, (details) => {
|
|
16
|
+
// console.log("Total Fine:", details);
|
|
17
|
+
// });
|
|
18
|
+
// });
|
|
19
|
+
// });
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
function getMem(memID) {
|
|
24
|
+
return new Promise((resolve,reject)=>{
|
|
25
|
+
resolve({id:12,name:"sushu"})
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function issuedbook(memID) {
|
|
30
|
+
return new Promise((resolve,reject)=>{
|
|
31
|
+
resolve(["java"]);
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fine(books) {
|
|
36
|
+
return new Promise((resolve,reject)=>{
|
|
37
|
+
resolve([1000]);
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getMem()
|
|
42
|
+
.then(member=>issuedbook(member.id))
|
|
43
|
+
.then(books=>fine(books))
|
|
44
|
+
.then(fine=>console.log(fine))
|
|
45
|
+
.catch(err=>console.log(err))
|
package/math.js
CHANGED
package/output.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hii hello my name is sushuu
|
package/package.json
CHANGED
package/promises.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function getUser(){
|
|
2
|
+
return new Promise((resolve, reject)=>{
|
|
3
|
+
resolve({name:"sushu",id:"26"});
|
|
4
|
+
})
|
|
5
|
+
}
|
|
6
|
+
function getPosts(userName){
|
|
7
|
+
return new Promise((resolve, reject)=>{
|
|
8
|
+
resolve(["post1" ,"post2"]);
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
getUser()
|
|
12
|
+
.then(username=>getPosts(username.name))
|
|
13
|
+
.then(posts=>console.log(posts))
|
|
14
|
+
.catch(error=>console.log(error))
|
|
15
|
+
// async function main(){
|
|
16
|
+
// const user=awaitgetUsers();
|
|
17
|
+
// const posts= await getPosts(user.name);
|
|
18
|
+
// }
|
|
19
|
+
// main();
|
package/streams.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// const fs=require('fs');
|
|
2
|
+
// utf-8 is used to convert binary code into human readable
|
|
3
|
+
// hightwaterwark is used to know how much data is read at once(in bytes)
|
|
4
|
+
// const readStream=fs.createReadStream("input.txt",{encoding:"utf-8",highWaterMark:10});
|
|
5
|
+
// readStream.on("data",(chunk)=>{
|
|
6
|
+
// console.log("new chunk recieved: ");
|
|
7
|
+
// console.log(chunk);
|
|
8
|
+
// });
|
|
9
|
+
// readStream.on("end",()=>{
|
|
10
|
+
// console.log("finished reading the file: ");
|
|
11
|
+
// })
|
|
12
|
+
|
|
13
|
+
// .........
|
|
14
|
+
// const fs=require('fs');
|
|
15
|
+
// const writeStream=fs.createWriteStream("output.txt"); /* output will be seen in output.txt file**/
|
|
16
|
+
// writeStream.write("hello from the stream!");
|
|
17
|
+
// writeStream.write("hello!! ,message from streams.js");
|
|
18
|
+
|
|
19
|
+
// writeStream.end("This is the end of the write stream.");
|
|
20
|
+
// writeStream.on("finish",()=>{
|
|
21
|
+
// console.log("finished writing to the file");
|
|
22
|
+
// })
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// const fs=require('fs');
|
|
26
|
+
// const readStream=fs.createReadStream("./input.txt");
|
|
27
|
+
// const writeStream=fs.createWriteStream("./output.txt");
|
|
28
|
+
// readStream.pipe(writeStream); /**output will be in output.txt that is whatever data is in input.txt */
|
|
29
|
+
|
|
30
|
+
// reading a file in syncronus way
|
|
31
|
+
// const fs=require('fs');
|
|
32
|
+
// const data=fs.readFileSync("input.txt","utf-8");
|
|
33
|
+
// console.log("data from file: ");
|
|
34
|
+
// console.log(data);
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// reading file in asyncronous way
|
|
38
|
+
// const fs=require('fs');
|
|
39
|
+
// fs.readFile("input.txt","utf-8",(err,data)=>{
|
|
40
|
+
// if(err){
|
|
41
|
+
// console.error("error reading file: ",err);
|
|
42
|
+
// }
|
|
43
|
+
// else{
|
|
44
|
+
// console.log("data from file: ");
|
|
45
|
+
// console.log(data);
|
|
46
|
+
// }
|
|
47
|
+
// });
|
|
48
|
+
// console.log("this line is after the readfile function call");
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// same code in syncronous
|
|
52
|
+
// const fs=require('fs');
|
|
53
|
+
// const data=fs.readFileSync("input.txt","utf-8");
|
|
54
|
+
// console.log("data from file: ");
|
|
55
|
+
// console.log(data);
|
|
56
|
+
// console.log("this line is after the readfile function call");
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// ....................
|
|
60
|
+
// const fs=require('fs');
|
|
61
|
+
// fs.writeFileSync("output.txt","hello world"); /**output will be in output.txt file */
|
|
62
|
+
|
|
63
|
+
// .....................
|
|
64
|
+
const fs=require('fs');
|
|
65
|
+
const data="hii hello my name is sushuu"; /**output will be in output.txt file */
|
|
66
|
+
fs.writeFile("output.txt",data,(err)=>{
|
|
67
|
+
if(err){
|
|
68
|
+
console.log(err);
|
|
69
|
+
}
|
|
70
|
+
});
|