From 24bb198c5dc065c8007a59d1f8c84b6c305cd8aa Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 21 Jan 2013 20:14:51 +0000 Subject: [PATCH] Bit more efficient. dont bother saying the replacement if replacement not made [#154] --- modules/regex/regex.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/regex/regex.js b/modules/regex/regex.js index d693183..50dd5b6 100644 --- a/modules/regex/regex.js +++ b/modules/regex/regex.js @@ -3,19 +3,23 @@ var _ = require('underscore')._; var regex = function(dbot) { this.last = {}; this.listener = function(event) { - var q = event.message.valMatch(/^s\/(.+)\/(.+)\/$/, 3); - var otherQ = event.message.valMatch(/^([\d\w\s]*): s\/(.+)\/(.+)\/$/, 4); + var q = event.message.valMatch(/^([\d\w\s]*)?:? ?s\/(.+)\/(.+)\/$/, 4); if(q) { - var toMatch = new RegExp(q[1]); - var replaceWith = q[2]; - var last = this.last[event.channel.name][event.user]; - event.reply(event.user + " meant: " + last.replace(toMatch, replaceWith)); - } else if(otherQ) { - var user = otherQ[1]; - var toMatch = new RegExp(otherQ[2]); - var replaceWith = otherQ[3]; - var last = this.last[event.channel.name][user]; - event.reply(event.user + " thinks " + user + " meant: " + last.replace(toMatch, replaceWith)); + var toMatch = new RegExp(q[2]), + replaceWith = q[3], + last, + replacement; + + if(q[1] != null) { + var user = q[1]; + last = this.last[event.channel.name][user]; + replacement = last.replace(toMatch, replaceWith); + if(replacement != last) event.reply(event.user + " thinks " + user + " meant: " + replacement); + } else { + last = this.last[event.channel.name][event.user]; + replacement = last.replace(toMatch, replaceWith); + if(replacement != last) event.reply(event.user + " meant: " + replacement); + } } else { if(_.has(this.last, event.channel.name)) { this.last[event.channel.name][event.user] = event.message;