Permalink
Browse files

small amount of binary cleanup, all tests pass

  • Loading branch information...
1 parent 4aaf296 commit 3204a4a65e723aa9125575ca3ec348bb954fb7c0 @substack committed Aug 11, 2010
Showing with 31 additions and 46 deletions.
  1. +26 −36 lib/binary.js
  2. +1 −1 package.json
  3. +4 −9 test/binary_event.js
View
@@ -118,11 +118,11 @@ function Binary(buffer) {
this.when = function (v1, v2, f) {
var f1 = typeof(v1) == 'string'
- ? function (vars) { return lookup.call(this,v1) }
+ ? function (vars) { return lookup(this,v1) }
: function (vars) { return v1 }
;
var f2 = typeof(v2) == 'string'
- ? function (vars) { return lookup.call(this,v2) }
+ ? function (vars) { return lookup(this,v2) }
: function (vars) { return v2 }
;
return this.tap(function () {
@@ -134,11 +134,11 @@ function Binary(buffer) {
this.unless = function (v1, v2, f) {
var f1 = typeof(v1) == 'string'
- ? function (vars) { return lookup.call(this,v1) }
+ ? function (vars) { return lookup(this,v1) }
: function (vars) { return v1 }
;
var f2 = typeof(v2) == 'string'
- ? function (vars) { return lookup.call(this,v2) }
+ ? function (vars) { return lookup(this,v2) }
: function (vars) { return v2 }
;
return this.tap(function () {
@@ -150,7 +150,7 @@ function Binary(buffer) {
this.repeat = function (n, f) {
var nf = typeof(n) == 'string'
- ? function (vars) { return lookup.call(this,n) }
+ ? function (vars) { return lookup(this,n) }
: function (vars) { return n }
;
this.pushAction({
@@ -180,41 +180,31 @@ function Binary(buffer) {
};
// assign immediately
- function assign (self) {
- var args = [].slice.call(arguments,1);
-
- // flatten :into so .getX(['foo','bar','baz'])
- // and .getX('foo','bar','baz') both work
- // also .getX('foo.bar.baz') does the same thing
- var keys = args.slice(0,-1).reduce(function f (acc,x) {
- return acc.concat(
- x instanceof Array ? x.reduce(f) : x.split('.')
- );
- }, []);
- var value = args.slice(-1)[0];
-
- // assign into key hierarchy with the into array
- var obj = self.vars;
- keys.slice(0,-1).forEach(function (k) {
- if (!obj[k]) obj[k] = {};
- obj = obj[k];
- });
- obj[ keys.slice(-1)[0] ] = value;
+ function assign (self, key, value) {
+ visit(
+ self, key instanceof Array ? key : [key],
+ function (v,k) { v[k] = value }
+ );
}
- function lookup() {
- var keys = [].reduce.call(arguments, function f (acc,x) {
- return acc.concat(
- x instanceof Array ? x.reduce(f) : x.split('.')
- );
- }, []);
- // assign into key hierarchy with the into array
- var obj = this.vars;
+ function lookup (self) {
+ var args = [].slice.call(arguments, 1);
+ return visit(self, args, function (v,k) { return v[k] });
+ }
+
+ function visit(self, args, f) {
+ var keys = args.reduce(function (acc,x) {
+ return acc.concat(x.split('.'))
+ },[])
+ ;
+
+ var obj = self.vars;
keys.slice(0,-1).forEach(function (k) {
if (!obj[k]) obj[k] = {};
obj = obj[k];
});
- return obj[ keys.slice(-1)[0] ];
+
+ return f(obj, keys.slice(-1)[0]);
}
// Assign into a variable. All but the last argument make up the key, which
@@ -242,7 +232,7 @@ function Binary(buffer) {
});
}
else if (typeof fv == 'string') {
- assign(this, keys, lookup.call(this,fv));
+ assign(this, keys, lookup(this,fv));
}
else if (typeof fv == 'number') {
assign(this, keys, fv);
@@ -379,7 +369,7 @@ function Binary(buffer) {
if (typeof(length) == 'string') {
var s = length;
- lengthF = function (vars) { return lookup.call(this,s) };
+ lengthF = function (vars) { return lookup(this,s) };
}
else if (typeof(length) == 'number') {
var s = length;
View
@@ -1,6 +1,6 @@
{
"name": "bufferlist",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Create linked lists of Buffer objects",
"directories": { "lib": "./lib" },
"main": "./bufferlist",
View
@@ -5,7 +5,7 @@ var Binary = require('bufferlist/binary');
function parser (sock) {
var bufferList = new BufferList;
- sock.addListener('data', function (data) {
+ sock.on('data', function (data) {
bufferList.push(data);
});
@@ -43,29 +43,24 @@ exports['binary event'] = function (assert) {
// connect to the server and parse its output
var client = new net.Stream;
- parser(client).addListener('end', function (vars) {
+ parser(client).on('end', function (vars) {
assert.equal(
moo, vars.moo,
'moo != ' + sys.inspect(moo) + ', moo == ' + sys.inspect(vars.moo)
);
- parsed ++;
client.end();
});
client.connect(port);
}
-
- var parsed = 0;
-
+
serverSession(20801,
['\x00','\x04m','eow'],
'xs:4:meow'
);
+
serverSession(20802,
['\x00\x00','\x12\x00\x00\x00happy pur','ring c','ats'],
'msg:18:happy purring cats'
);
- setTimeout(function () {
- assert.equal(parsed, 2, 'parsed != 2, parsed == ' + parsed);
- }, 500);
};

0 comments on commit 3204a4a

Please sign in to comment.