Created
May 31, 2011 17:21
-
-
Save jeffmarshall/1000912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var datatypes = { | |
action: /^[-\w]$/, | |
channel: /^[-_a-z0-9]*$/, | |
nick: /^[-\w]$/, | |
message: /^.{1,255}$/, | |
timestamp: /^\d$/ | |
} | |
var messagetype = { | |
join: [ | |
'channel', | |
], | |
say: [ | |
'message' | |
] | |
} | |
function valid(datatype, data){ | |
if(isNAN(data) && typeof data != 'string'){ | |
console.error('Datatype Validation Failed: unacceptable data '+ data); | |
return false; | |
} | |
if (datatypes[datatype] == "undefined"){ | |
console.error('Datatype Validation Failed: invalid datatype '+ datatype); | |
return false; | |
} | |
if(typeof data != "string" || data.match(datatypes[datatype]) === null){ | |
console.log('Datatype Validation Failed: '+ datatypes[datatype] +" did not find a match in "+ data); | |
return false; | |
} | |
return true; | |
} | |
function Message(action){ | |
this.action = valid(action) ? action; | |
this.timestamp = new Date().getTime(); | |
this.setMessageAttribute = function(datatype, data){ | |
if(valid(datatype, data)){ | |
this[datatype] = data; | |
} | |
} | |
} | |
function JoinMessage(channel){ | |
Message.call(this, 'join'); | |
this.channel = channel; | |
} | |
function SayMessage(message){ | |
Message.call(this, 'say'); | |
this.message = message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment