Created
February 20, 2015 21:40
-
-
Save rhodey/2c320f67080c7e59d7df to your computer and use it in GitHub Desktop.
mokey tiger haiku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var split = require('split'); | |
var http = require('http'); | |
function pullHaikuFromLine(line, offset) { | |
return line.replace(/\s{2,}/g, ' ') | |
.replace(/<div class="line">/g, "\n") | |
.replace(/<(?:.|\n)*?>/gm, ' ') | |
.substring(3); | |
} | |
function handleGetHaiku(cb) { | |
http.get('http://www.randomhaiku.com/', function(res) { | |
res.pipe(split()).on('data', function (line) { | |
if (line.indexOf('div class="poem"') >= 0) | |
cb(pullHaikuFromLine(line, 0)); | |
}); | |
}).on('error', function(e) { | |
process.stderr.write('error in stream D: ' + e.message); | |
}); | |
} | |
function hasTiger(haiku) { | |
var lHaiku = haiku.toLowerCase(); | |
return lHaiku.indexOf('t') >= 0 && | |
lHaiku.indexOf('i') >= 0 && | |
lHaiku.indexOf('g') >= 0 && | |
lHaiku.indexOf('e') >= 0 && | |
lHaiku.indexOf('r') >= 0; | |
} | |
function hasMonkey(haiku) { | |
var lHaiku = haiku.toLowerCase(); | |
return lHaiku.indexOf('m') >= 0 && | |
lHaiku.indexOf('o') >= 0 && | |
lHaiku.indexOf('n') >= 0 && | |
lHaiku.indexOf('k') >= 0 && | |
lHaiku.indexOf('e') >= 0 && | |
lHaiku.indexOf('y') >= 0; | |
} | |
function handleSearchHaiku(haiku) { | |
console.log(haiku); | |
var tiger = hasTiger(haiku); | |
var monkey = hasMonkey(haiku); | |
if (tiger && monkey) | |
console.log('TIGER MOKEY!!! :D'); | |
else if (tiger) | |
console.log('TIGER!'); | |
else if (monkey) | |
console.log('MONEKY!'); | |
else | |
console.log('NO TIGER, NO MONKEY :['); | |
} | |
handleGetHaiku(handleSearchHaiku); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment