diff --git a/lib/utils.js b/lib/utils.js index 7a65f92ec3..92a7e31a36 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -454,7 +454,7 @@ function jsonStringify(object, spaces, depth) { var space = spaces * depth; var str = isArray(object) ? '[' : '{'; var end = isArray(object) ? ']' : '}'; - var length = object.length || exports.keys(object).length; + var length = typeof object.length === 'number' ? object.length : exports.keys(object).length; // `.repeat()` polyfill function repeat(s, n) { return new Array(n).join(s); diff --git a/test/acceptance/utils.js b/test/acceptance/utils.js index 053e7ca1f1..95424bcb56 100644 --- a/test/acceptance/utils.js +++ b/test/acceptance/utils.js @@ -331,6 +331,10 @@ describe('lib/utils', function () { stringify(a).should.equal('{\n "foo": 1\n}'); }); + + it('should handle length properties that cannot be coerced to a number', function () { + stringify({length: {toString: 0}}).should.equal('{\n "length": {\n "toString": 0\n }\n}'); + }); }); describe('type', function () {