Skip to content

Commit

Permalink
Merge pull request #2110 from joshlory/stringify-fix
Browse files Browse the repository at this point in the history
Fix #2109: stringify should handle objects with non-coercible length
  • Loading branch information
danielstjules committed Mar 20, 2016
2 parents 9ae6a85 + 3b92fcc commit 61fbb7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/acceptance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ describe('lib/utils', function () {
stringify({symbol: symbol}).should.equal('{\n "symbol": Symbol(value)\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 () {
Expand Down

0 comments on commit 61fbb7f

Please sign in to comment.