Skip to content

Commit

Permalink
Replace global string replace for message type revisions with custom …
Browse files Browse the repository at this point in the history
…swapCommand function.

Also, put in a module pattern for augmentation of global module, so as to not pollute
global namespace.
  • Loading branch information
akavlie committed Nov 6, 2011
1 parent bd1700c commit 3f26923
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app.js
@@ -1,6 +1,6 @@
$(function() { $(function() {
// Our global object // Our global object
window.irc = {}; window.irc = window.irc || {};


// socket.io init // socket.io init
var socket = io.connect('http://localhost'); var socket = io.connect('http://localhost');
Expand Down Expand Up @@ -332,7 +332,7 @@ $(function() {
revised = command; revised = command;
break; break;
} }
return replaceString(command, revised, text); return irc.utils.swapCommand(command, revised, text);
}, },


sendInput: function(e) { sendInput: function(e) {
Expand Down
17 changes: 13 additions & 4 deletions js/util.js
Expand Up @@ -69,7 +69,16 @@ if ( !Array.prototype.forEach ) {
// UTILITY FUNCTIONS // UTILITY FUNCTIONS
// ================= // =================


// Replaces oldString with newString in the string haystack window.irc = (function(module) {
function replaceString(oldString, newString, haystack) { module.utils = {
return haystack.split(oldString).join(newString); // 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.