Skip to content

Instantly share code, notes, and snippets.

@akras14
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akras14/11480891 to your computer and use it in GitHub Desktop.
Save akras14/11480891 to your computer and use it in GitHub Desktop.
http://nodeschool.io/ Stream Adventure - HTML Stream Solution

The Problem

Your program will get some html written to stdin. Convert all the inner html to upper-case for elements with a class name of "loud".

You can use trumpet and through to solve this adventure.

With trumpet you can create a transform stream from a css selector:

var trumpet = require('trumpet');
var fs = require('fs');
var tr = trumpet();
fs.createReadStream('input.html').pipe(tr);

var stream = tr.select('.beep').createStream();

Now stream outputs all the inner html content at '.beep' and the data you write to stream will appear as the new inner html content.

Make sure to npm install trumpet through in the directory where your solution file lives.

To verify your program, run: stream-adventure verify program.js.

Runing the App

Run node solution.js < test_input.html

Sample Output

Note that everything inside loud class tag is capitalized

<p> some text that <span class="loud">GOVERNMENT LOVE THE PEOPLE, BESIDE THE PEOPLE," FOUR OF THE PEOPLE, SHALL NOT PERISH FROM THIS EARTH.</span> and some more text here</p>
<p>Another line of text</p>
<p>Yet another line</p>
var trumpet = require('trumpet');
var through = require('through');
var tr = trumpet();
var loud = tr.select('.loud').createStream();
loud.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(loud);
process.stdin.pipe(tr).pipe(process.stdout);
<p> some text that <span class="loud">government love the people, beside the people," four of the people, shall not perish from this earth.</span> and some more text here</p>
<p>Another line of text</p>
<p>Yet another line</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment