Skip to content

Commit

Permalink
Removed trampolining and added new tests.
Browse files Browse the repository at this point in the history
lol this thing was not tail recursive in the first place, my bad.
New tests with very deep json to prove it.
  • Loading branch information
Kevin Roark committed May 31, 2014
1 parent e3c2f02 commit 16e83c3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
1 change: 1 addition & 0 deletions fixtures/big.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var ob = {};

var max = 5230; // just before call stack breaks
var count = 0;

function g(o) {
if (count++ < max) {
o.k = {};
g(o.k);
}
else {
o.k = 'cool';
}
}

g(ob);
console.log(JSON.stringify(ob));
43 changes: 14 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var isArray = require('isarray');
module.exports = hasBinary;

/**
* Checks for binary data (ArrayBuffer, Buffer, Blob, and File).
* Checks for binary data.
*
* Right now only Buffer and ArrayBuffer are supported..
*
* @param {Object} anything
* @api public
Expand All @@ -31,42 +33,25 @@ function hasBinary(data) {
}

if (isArray(obj)) {
return function() {
for (var i = 0; i < obj.length; i++) {
var result = _hasBinary(obj[i]);
while(isfun(result)) result = result();
if (result) return true;
}

return false;
for (var i = 0; i < obj.length; i++) {
if (_hasBinary(obj[i])) {
return true;
}
}
} else if (obj && 'object' == typeof obj) {
return function() {
if (obj.toJSON) {
obj = obj.toJSON();
}
if (obj.toJSON) {
obj = obj.toJSON();
}

for (var key in obj) {
var result = _hasBinary(obj[key]);
while (isfun(result)) result = result();
if (result) return true;
for (var key in obj) {
if (_hasBinary(obj[key])) {
return true;
}

return false;
}
}

return false;
}

// trampoline
var result = _hasBinary(data);
while (isfun(result)) {
result = result();
}
return result;
}

function isfun(x) {
return typeof x === 'function';
return _hasBinary(data);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "has-binary-data",
"version": "0.2.0",
"version": "0.1.2",
"description": "A function that takes anything in javascript and returns true if its argument contains binary data.",
"dependencies": {
"isarray": "0.0.1"
Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ describe('has-binarydata', function(){
assert(hasBinary(ob));
});

it('should handle a very large json object with no binary', function(done) {
this.timeout();
fs.readFile(__dirname + '/fixtures/big.json', function(err, data) {
if (err) {
console.log(err);
assert(false);
}
data = JSON.parse(data);
assert(!hasBinary(data));
done();
});
});

it('should handle a very large json object with binary', function(done) {
this.timeout();
fs.readFile(__dirname + '/fixtures/big.json', function(err, data) {
if (err) {
console.log(err);
assert(false);
}
var ob = JSON.parse(data);
ob.bin = {bin: {bin: {bin: new Buffer('abc')}}};
assert(hasBinary(ob));
done();
});
});

if (global.ArrayBuffer) {
it('should work with an ArrayBuffer', function() {
assert(hasBinary(new ArrayBuffer()));
Expand Down

0 comments on commit 16e83c3

Please sign in to comment.