-
Notifications
You must be signed in to change notification settings - Fork 0
/
learnyounode.js
181 lines (153 loc) · 4.7 KB
/
learnyounode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// console.log("HELLO WORLD");
// STEP 2: Command line parameters can be accessed through the global process object. The process object has a argv property that is an array containing all the command-line command
/* var length = process.argv.length;
var result = 0;
for(var i=2;i<length;i++){
result = result+Number(process.argv[i]);
}
console.log(result);
*/
//STEP 3 - Synchronus file IO. This snippet will count the number of newlines in a given file
// The readFileSync object returns a buffer object. The buffer object is node's way of efficiently representing arbitrary array of data whether the data is binary, ascii, etc.
// If you pass UTF-8 as the second argument to readfilesync, you'll get a buffer instead of a string
// Async functions in fs take callback as the last argument. Usually, callbacks are passed as anonymuos functions function(err,data)
/*
var fs = require('fs');
var filename = process.argv[2];
var filecontents = fs.readFileSync(filename)
console.log(filecontents.toString().split('\n').length-1);
*/
//STEP 4 - Asynchronus file IO. This snippet will count the number of newlines in a given file
/*var fs = require('fs');
var filename = process.argv[2];
var filecontents = fs.readFile(filename, function(err,filecontents){
if(err)
console.log('error occured' + err);
console.log(filecontents.toString().split('\n').length-1);
});*/
// STEP 5: Print only those filenames that have extension given in args[2]
/*var fs = require('fs');
var dirname = process.argv[2];
fs.readdir(dirname, function(err, data){
for(var i=0;i<data.length;i++){
extension = data[i].split('.')[1];
if(extension == process.argv[3])
console.log(data[i]);
}
});*/
// STEP 5: Approach 2:
/*var fs = require('fs');
var path = require('path');
fs.readdir(process.argv[2], function(err, list){
list.forEach(function(file){
if(path.extname(file) === '.' + process.argv[3]){
console.log(file)
}
});
});*/
// STEP 6: Exporting modules
/*var path = require('path');
var mymodule = require('./mymodule');
function printResults(err, data){
data.forEach(function(file){
console.log(file);
});
}
mymodule(process.argv[2], process.argv[3], printResults);*/
// STEP 6: Approach 2 (Solution)
/*var filterFn = require('./solution_filter.js')
var dir = process.argv[2]
var filterStr = process.argv[3]
filterFn(dir, filterStr, function (err, list) {
if (err)
return console.error('There was an error:', err)
list.forEach(function (file) {
console.log(file)
})
})
*/
// STEP 7: Log data of an http get request
/*http = require('http');
http.get(process.argv[2], function(response){
response.setEncoding('utf-8')
response.on('data', function(data){
console.log(data.toString());
});
});
*/
// STEP 7: Approach 2 (Solution)
/*var http = require('http')
http.get(process.argv[2], function (response) {
response.setEncoding('utf8')
response.on('data', console.log)
response.on('error', console.error)
})
*/
// STEP 8 :Capture the entire get stream
/*var http = require('http');
var string="";
http.get(process.argv[2], function(response){
response.on('data', function(data){
string+=data.toString();
});
response.on('error', console.error);
response.on('end', function(){
console.log(string.length);
console.log(string);
});
});
*/
// STEP 8: Approach 2(Solution)
/*var http = require('http')
var bl = require('bl')
http.get(process.argv[2], function (response) {
response.pipe(bl(function (err, data) {
if (err)
return console.error(err)
data = data.toString()
console.log(data.length)
console.log(data)
}));
});*/
// STEP 9: Juggling Async
/*var http = require('http');
var string1 = "", string2 = "", string3 = "";
var count = 0;
for (var i=2, i<5;i++){
http.get(proces.argv[i],function(response){
response.on('data', function(data){
string1+=data.toString();
});
response.on('end', callback)
});
}
callback = function(resp_string){
count++;
if(count == 3){
console.log(string1 + '\n' + string2 + '\n'+ string3);
}
}*/
// STEP 9: Approach 2 (Solution)
/*var http = require('http')
var bl = require('bl')
var results = []
var count = 0
function printResults () {
for (var i = 0; i < 3; i++)
console.log(results[i])
}
function httpGet (index) {
http.get(process.argv[2 + index], function (response) {
response.pipe(bl(function (err, data) {
if (err)
return console.error(err)
results[index] = data.toString()
count++
if (count == 3) // yay! we are the last one!
printResults()
}))
})
}
for (var i = 0; i < 3; i++)
httpGet(i)
*/