Skip to content

Instantly share code, notes, and snippets.

@tylorr
Created April 18, 2015 03:30
Show Gist options
  • Save tylorr/b6c198065b556ab289f5 to your computer and use it in GitHub Desktop.
Save tylorr/b6c198065b556ab289f5 to your computer and use it in GitHub Desktop.
module.exports = function(maxSpoilsPerChannel) {
return function(data, spoils) {
var channelId = data.channel_id,
channelName = data.channel_name,
originalText = data.text.trim();
var result = {
username: data.user_name,
channel: '',
text: ''
};
if (channelName === 'directmessage') {
result.channel = '@' + result.username;
} else {
result.channel = '#' + channelName;
}
var textNotToSpoil = '';
var textToSpoil = originalText;
var matches = originalText.match(/^\[(.*?)\](.*)$/i);
if (matches) {
textNotToSpoil = matches[1];
textToSpoil = matches[2];
}
if (!spoils[channelId]) {
spoils[channelId] = [];
}
var spoilsForChannel = spoils[channelId];
spoilsForChannel.unshift({
username: result.username,
text: textNotToSpoil + textToSpoil
});
if (spoilsForChannel.length > maxSpoilsPerChannel) {
spoilsForChannel.shift();
}
result.text = textNotToSpoil + textToSpoil.replace(/[^\s]/gi, '■');
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment