From 9befd818d5292c4659dc8621cdf3b8919f134ba2 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Mon, 5 Aug 2013 16:03:30 +0000 Subject: [PATCH] more efficient regex code --- modules/regex/regex.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/modules/regex/regex.js b/modules/regex/regex.js index ba11435..4f3ac8c 100644 --- a/modules/regex/regex.js +++ b/modules/regex/regex.js @@ -9,21 +9,22 @@ var regex = function(dbot) { toMatch = new RegExp(q[2], flags), replaceWith = q[3], last, - replacement; + replacement, + user; if(!replaceWith) replaceWith = ""; - - if(q[1] != null) { - var user = q[1]; - last = this.last[event.channel.name][user]; - replacement = last.replace(toMatch, replaceWith); - replacement = replacement.replace(/^.ACTION/, user).replace("\x01", ''); - 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); - replacement = replacement.replace(/^.ACTION/, event.user).replace("\x01", ''); - if(replacement != last) event.reply(event.user + " meant: " + replacement); + + user = q[1] || event.user; + last = this.last[event.channel.name][user]; + replacement = last.replace(toMatch, replaceWith); + replacement = replacement.replace(/^.ACTION/, user).replace("\x01", ''); + + if(replacement != last) { + if(q[1]) { + event.reply(event.user + " thinks " + user + " meant: " + replacement); + } else { + event.reply(event.user + " meant: " + replacement); + } } } else { if(_.has(this.last, event.channel.name)) {