Merge pull request #1071 from husio/toJSON

JSON.stringify(m.prop(x)) use x.toJSON method when possible
This commit is contained in:
Leo Horie 2016-08-12 00:45:27 -04:00 committed by GitHub
commit 6fe960ee8a
2 changed files with 17 additions and 0 deletions

View file

@ -1352,6 +1352,7 @@
}
prop.toJSON = function () {
if (store && isFunction(store.toJSON)) return store.toJSON()
return store
}

View file

@ -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)