-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines.js
More file actions
63 lines (46 loc) · 1.3 KB
/
lines.js
File metadata and controls
63 lines (46 loc) · 1.3 KB
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
/**
Instead of transforming every line as in the previous "INPUT OUTPUT" example,
for this challenge, convert even-numbered lines to upper-case and odd-numbered
lines to lower-case. Consider the first line to be odd-numbered. For example
given this input:
One
Two
Three
Four
Your program should output:
one
TWO
three
FOUR
You can use the `split` module to split input by newlines. For example:
var split = require('split');
process.stdin
.pipe(split())
.pipe(through(function (line) {
console.dir(line.toString());
}))
;
Will buffer and split chunks on newlines before you get them. For example, for
the `split.js` we just wrote we will get separate events for each line even
though the data probably all arrives on the same chunk:
$ echo -e 'one\ntwo\nthree' | node split.js
'one'
'two'
'three'
Your own program should use `split` in this way, but you should transform the
input and pipe the output through to `process.stdout`.
*/
var through = require('through');
var split = require('split');
var write = function(data) {
cnt++;
if (cnt % 2 == 0) {
this.queue(data.toString().toUpperCase()+"\n");
}
else {
this.queue(data.toString().toLowerCase()+"\n");
}
}
var tr = through(write);
var cnt = 0;
process.stdin.pipe(split()).pipe(tr).pipe(process.stdout);