From c71e8f4fcb5a58653442bb17a02ba64ec5e4c07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Husiaty=C5=84ski?= Date: Wed, 25 May 2016 10:18:32 +0200 Subject: [PATCH] JSON.stringify(m.prop(x)) use x.toJSON method when possible When JSON serializing m.prop, check if wrapped object does define toJSON method and if so, return it's result. --- mithril.js | 1 + test/mithril.prop.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/mithril.js b/mithril.js index 9478add4..e8305a87 100644 --- a/mithril.js +++ b/mithril.js @@ -1331,6 +1331,7 @@ } prop.toJSON = function () { + if (store && isFunction(store.toJSON)) return store.toJSON() return store } diff --git a/test/mithril.prop.js b/test/mithril.prop.js index 4be4b1b9..cc6ef6f1 100644 --- a/test/mithril.prop.js +++ b/test/mithril.prop.js @@ -42,6 +42,22 @@ describe("m.prop()", function () { expect(JSON.stringify(obj)).to.equal('{"prop":"test"}') }) + it("correctly stringifies Date", function () { + var prop = m.prop(new Date(999)) + expect(JSON.stringify(prop)).to.equal('"1970-01-01T00:00:00.999Z"') + }) + + it("correctly stringifies object with toJSON method", function () { + function Thing(name) { + this.name = name + } + Thing.prototype.toJSON = function() { + return {kind: 'Thing', name: this.name} + } + var banana = m.prop(new Thing("bannana")) + expect(JSON.stringify(banana)).to.equal('{"kind":"Thing","name":"bannana"}') + }) + it("correctly wraps Mithril promises", function () { var defer = m.deferred() var prop = m.prop(defer.promise)