Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

feature: make a best guess at prettifying object that have no prototype #284

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions specs/validate-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ describe("validate", function() {
expect(validate.prettify(object)).toEqual("Custom string");
});

it("tries to stringify objects that don't implement toString", function() {
var object = Object.create(null);

object.name = "Foo Bar";

expect(validate.prettify(object)).toEqual("{\"name\":\"Foo Bar\"}");
});

it("throws an error when trying to parse cyclical structures", function() {
var object = Object.create(null);

object.name = "Foo Bar";
object.cycle = object;

expect(function () {
return validate.prettify(object);
}).toThrow();
});

it("doesn't allow too many decimals", function() {
expect(validate.prettify(4711)).toEqual("4711");
expect(validate.prettify(4711.2)).toEqual("4711.2");
Expand Down
4 changes: 4 additions & 0 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@
}

if (v.isObject(str)) {
if (!v.isDefined(str.toString)) {
return JSON.stringify(str);
}

return str.toString();
}

Expand Down