Permalink
Browse files

Fix bug on message split and added test case

  • Loading branch information...
1 parent 3eda9d3 commit 0a8e14b5ba1e80d281c87ee3566b5697cd1a745e @Soarez committed Jan 9, 2013
Showing with 20 additions and 4 deletions.
  1. +8 −3 lib/m2e.js
  2. +12 −1 test/m2e.js
View
@@ -10,9 +10,14 @@ function event2message(evtName, args) {
function message2event(message) {
- var bits = message.split(SEPARATOR)
- var evtName = bits[0];
- var args = JSON.parse(bits[1]);
+ var indexOfSeparator = message.indexOf(SEPARATOR);
+
+ if (!~indexOfSeparator)
+ throw new Error('Invalid M2E message');
+
+ var evtName = message.substring(0, indexOfSeparator);
+ var argsString = message.substring(indexOfSeparator + 1);
+ var args = JSON.parse(argsString);
return {
name: evtName,
View
@@ -14,4 +14,15 @@ test('event2message', function() {
var message = event2message(evtName, args);
ok(message === 'evt2:' + JSON.stringify(args));
-})
+})
+
+test('both', function() {
+ var evtName = 'evt2';
+ var args = [{a:1}, {b:2}];
+
+ var message = event2message(evtName, args);
+ var evt = message2event(message);
+
+ ok(evt.name === evtName);
+ ok(JSON.stringify(args) === JSON.stringify(evt.args));
+});

0 comments on commit 0a8e14b

Please sign in to comment.