Permalink
Browse files

Added type detection

will try to attempt to match types of incoming arguments
  • Loading branch information...
1 parent 1cb094a commit db35f13e7759d06ee1e5441f368e814c7c36b944 Patrick committed Dec 27, 2011
Showing with 46 additions and 6 deletions.
  1. +31 −6 ControlPort.js
  2. +15 −0 examples/vanilla.js
View
37 ControlPort.js
@@ -25,12 +25,7 @@ ControlPort.prototype.start = function (port) {
});
}
-function colorizePrompt(prompt,color) {
- if(Colors[color] === undefined) {
- color = 'yellow'
- }
- return Colors[color] + prompt;
-}
+
ControlPort.prototype.handler = function (data,socket) {
var parse = data.toString().substring(0, data.length-1).split(/\s+/g);
@@ -42,6 +37,9 @@ ControlPort.prototype.handler = function (data,socket) {
socket.write('\n'+colorizePrompt(this.prompt,this.color));
return;
}
+
+ detectTypes(parse);
+ console.log(parse);
var out = this.commands[command].apply(this, parse);
socket.write(out+'\n'+colorizePrompt(this.prompt,this.color));
}
@@ -68,6 +66,25 @@ ControlPort.prototype.register = function(name, callback, descrip) {
}
}
+
+//Helper functions
+
+
+function detectTypes (args) {
+
+ for(var i = 0; i < args.length; i++) {
+ if(typeof args[i] == 'object') continue;
+
+ //TODO: this won't detect cases of NumString. parseFloat() chops String and returns Num
+ if(typeof args[i] == 'string') {
+ if(!isNaN(parseFloat(args[i]))) {
+ args[i] = parseFloat(args[i]);
+ }
+ }
+ }
+}
+
+
function extractArguments (string) {
string = string.replace('function','');
var args = string.substring(0,string.search('{')).replace(/^\s*\(/g,'').replace(/\)\s*$/g, '').split(',');
@@ -77,6 +94,14 @@ function extractArguments (string) {
return args;
}
+function colorizePrompt(prompt,color) {
+ if(Colors[color] === undefined) {
+ color = 'yellow'
+ }
+ return Colors[color] + prompt;
+}
+
+
//Available Colors
var Colors = {
View
15 examples/vanilla.js
@@ -0,0 +1,15 @@
+var ControlPort = require('../ControlPort.js').ControlPort;
+
+var control = new ControlPort();
+
+control.start(6000);
+
+control.register('add', function (a,b) {
+ return (a+b);
+},'Adds two numbers');
+
+control.register('shutdown', function () {
+ process.exit(0);
+},'Shuts down process');
+
+

0 comments on commit db35f13

Please sign in to comment.