Permalink
Browse files

Replace global string replace for message type revisions with custom …

…swapCommand function.

Also, put in a module pattern for augmentation of global module, so as to not pollute
global namespace.
  • Loading branch information...
1 parent bd1700c commit 3f26923dbef4ea367bc0db3b2bc2c13ff9ffc6b6 @akavlie committed Nov 6, 2011
Showing with 15 additions and 6 deletions.
  1. +2 −2 app.js
  2. +13 −4 js/util.js
View
4 app.js
@@ -1,6 +1,6 @@
$(function() {
// Our global object
- window.irc = {};
+ window.irc = window.irc || {};
// socket.io init
var socket = io.connect('http://localhost');
@@ -332,7 +332,7 @@ $(function() {
revised = command;
break;
}
- return replaceString(command, revised, text);
+ return irc.utils.swapCommand(command, revised, text);
},
sendInput: function(e) {
View
@@ -69,7 +69,16 @@ if ( !Array.prototype.forEach ) {
// UTILITY FUNCTIONS
// =================
-// Replaces oldString with newString in the string haystack
-function replaceString(oldString, newString, haystack) {
- return haystack.split(oldString).join(newString);
-}
+window.irc = (function(module) {
+ module.utils = {
+ // Replaces oldString with newString at beginning of text
+ swapCommand: function(oldString, newString, text) {
+ if (text.substring(0, oldString.length) === oldString)
+ return newString + text.substring(oldString.length, text.length);
+ else
+ throw 'String "' + oldString + '" not found at beginning of text';
+ }
+ }
+
+ return module
+})(window.irc || {});

0 comments on commit 3f26923

Please sign in to comment.