Created
December 30, 2013 18:50
-
-
Save freakent/8186161 to your computer and use it in GitHub Desktop.
Node-Red function for extracting colours from a Cheerlights Twitter message.
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
// Extract colours from Twitter String | |
var input = msg.payload; | |
var colours = input.toUpperCase().match(/\b(red|blue|green|purple)\b/ig); | |
if (colours == null) { return } | |
//console.log("colours:" + colours); | |
var pattern = []; | |
colours.forEach(function(colour) { | |
var cc = (colour == "RED") ? "P" : colour.charAt(0); | |
console.log("Colour:" + colour + ":" + cc); | |
if (pattern[pattern.length-1] == cc) { | |
pattern.push(" "); | |
} | |
pattern.push(cc); | |
}); | |
if (pattern.length > 0) { | |
msg.payload = "CHEER " + pattern.join(""); | |
msg.input = input; | |
return msg; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment