Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var isArray = require('isarray');
* Module exports.
*/

module.exports = hasBinary;
module.exports = function hasBinaryCircular (obj) {
return hasBinary(obj, []);
};

/**
* Checks for binary data.
Expand All @@ -21,14 +23,20 @@ module.exports = hasBinary;
* @api public
*/

function hasBinary (obj) {
function hasBinary (obj, known) {
if (!obj || typeof obj !== 'object') {
return false;
}

if (known.indexOf(obj) >= 0) {
return false;
}

known.push(obj);

if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) {
if (hasBinary(obj[i])) {
if (hasBinary(obj[i], known)) {
return true;
}
}
Expand All @@ -45,11 +53,11 @@ function hasBinary (obj) {

// see: https://github.com/Automattic/has-binary/pull/4
if (obj.toJSON && typeof obj.toJSON === 'function') {
return hasBinary(obj.toJSON());
return hasBinary(obj.toJSON(), known);
}

for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key], known)) {
return true;
}
}
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@ describe('has-binarydata', function () {
assert(!hasBinary(global.Blob));
});
}

it('should work with recursive structures and no blobs', function () {
var child = {
foo: 'bar'
};
var parent = {
zoo: 'gar',
child: child
};
child.parent = parent;
assert(!hasBinary(child));
});

it('should work with recursive structures and blobs', function () {
var child = {
foo: 'bar'
};
var parent = {
zoo: 'gar',
child: child,
blob: new Buffer('xxx')
};
child.parent = parent;
assert(hasBinary(child));
});
});