From e594b94b54e08168763823b4cc8398c77e5dd2d3 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 3 Apr 2017 22:30:22 +0200 Subject: [PATCH] do not call toJSON more than once In order to match JSON.stringify's behaviour: > var o = { toJSON: function() { return this; } }; > JSON.stringify(o) '{}' > hasBinary(o) false (previously "RangeError: Maximum call stack size exceeded") --- index.js | 4 ++-- test.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 395036e..8b655a6 100644 --- a/index.js +++ b/index.js @@ -44,8 +44,8 @@ function hasBinary (obj) { } // see: https://github.com/Automattic/has-binary/pull/4 - if (obj.toJSON && typeof obj.toJSON === 'function') { - return hasBinary(obj.toJSON()); + if (obj.toJSON && typeof obj.toJSON === 'function' && arguments.length === 1) { + return hasBinary(obj.toJSON(), true); } for (var key in obj) { diff --git a/test.js b/test.js index 8e65ff3..e39dbe0 100644 --- a/test.js +++ b/test.js @@ -34,6 +34,11 @@ describe('has-binarydata', function () { assert(hasBinary(ob)); }); + it('should work with an object by calling toJSON() once', function () { + var ob = { toJSON: function () { return this; } }; + assert(!hasBinary(ob)); + }); + it('should work with null', function () { assert(!hasBinary(null)); });