node-red-capitalize-first-word-in-sentence
Version:
Node to capitalixe first word in the sentence
21 lines • 855 B
JavaScript
module.exports = function(RED) {
function UpperCaseNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
msg.payload = titleCase(msg.payload);
node.send(msg);
});
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
}
RED.nodes.registerType("capitalize-case",UpperCaseNode);
}