Skip to content
Closed
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
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = hasBinary;

function hasBinary(data) {

function _hasBinary(obj) {
function _hasBinary(obj, afterToJSON) {
if (!obj) return false;

if ( (global.Buffer && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) ||
Expand All @@ -41,8 +41,9 @@ function hasBinary(data) {
}
} else if (obj && 'object' == typeof obj) {
// see: https://github.com/Automattic/has-binary/pull/4
if (obj.toJSON && 'function' == typeof obj.toJSON) {
obj = obj.toJSON();
// also: toJSON() can return any type. Restart the check, but don't call toJSON twice
if (!afterToJSON && obj.toJSON && 'function' == typeof obj.toJSON) {
return _hasBinary(obj.toJSON(), true);
}

for (var key in obj) {
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('has-binarydata', function(){
assert(hasBinary(ob));
});

it('should work with an object whose toJSON() returns a buffer', function() {
var ob = {a: 'a', b: [], c: 1234, toJSON: function() { return new Buffer('abc') }};
assert(hasBinary(ob));
});

it('should work with null', function() {
assert(!hasBinary(null));
});
Expand Down