diff --git a/README.md b/README.md index 7c3b4d80..06bd294b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Examples run out of the box. Just open the HTML files. The code is fairly stable and I'm using it in production, but there may be bugs still lurking. -Some examples of usage can be found in the [examples](examples) folder. [ThreadItJS](http://cdn.rawgit.com/lhorie/mithril.js/rewrite/examples/threaditjs-bundle/index.html) has the largest API surface coverage. +Some examples of usage can be found in the [examples](examples) folder. [ThreadItJS](http://cdn.rawgit.com/lhorie/mithril.js/rewrite/examples/threaditjs/index.html) has the largest API surface coverage. Partial documentation can be found in the [`/docs`](docs) directory diff --git a/mithril.js b/mithril.js index 34f77cb8..9de1bf21 100644 --- a/mithril.js +++ b/mithril.js @@ -73,6 +73,7 @@ function hyperscript(selector) { return Vnode(selector, attrs && attrs.key, attrs || {}, Vnode.normalizeChildren(children), undefined, undefined) } hyperscript.trust = function(html) { + if (html == null) html = "" return Vnode("<", undefined, undefined, html, undefined, undefined) } hyperscript.fragment = function(attrs1, children) { diff --git a/mithril.min.js b/mithril.min.js index 955fc913..c101f8cd 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,10 +1,10 @@ new function(){function y(b,f,p,g,k,h){return{tag:b,key:f,attrs:p,children:g,text:k,dom:h,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function A(b){if(null==b||"string"!==typeof b&&null==b.view)throw Error("The selector must be either a string or a component.");if("string"===typeof b&&void 0===J[b]){for(var f,p,g=[],k={};f=Q.exec(b);){var h=f[1],w=f[2];""===h&&""!==w?p=w:"#"===h?k.id=w:"."===h?g.push(w):"["===f[3][0]&&((h=f[6])&&(h=h.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")), k[f[4]]=h||!0)}0d.filter(G).length)throw Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream"); return v(f(),d,function(){var e=d.filter(B);if(0 -1) { + seed = tuples[idx][1](seed, stream._state.value) + } + }) + + return seed + }, streams) + + return newStream +} diff --git a/stream/scan.js b/stream/scan.js new file mode 100644 index 00000000..4f65736e --- /dev/null +++ b/stream/scan.js @@ -0,0 +1,14 @@ +//! adapted for mithril from flyd https://github.com/paldepind/flyd +'use strict' + +var combine = require('../stream').combine + +module.exports = function (reducer, seed, stream) { + var newStream = combine(function (s) { + return seed = reducer(seed, s._state.value) + }, [stream]) + + if (newStream._state.state === 0) newStream(seed) + + return newStream +} diff --git a/stream/tests/index.html b/stream/tests/index.html new file mode 100644 index 00000000..69cf025e --- /dev/null +++ b/stream/tests/index.html @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/stream/tests/test-stream.js b/stream/tests/test-stream.js new file mode 100644 index 00000000..bdaee92b --- /dev/null +++ b/stream/tests/test-stream.js @@ -0,0 +1,90 @@ +'use strict' + +var o = require('../../ospec/ospec') + +o.spec('stream', function () { + var prop + + o.beforeEach(function () { + prop = require('../../stream') + }) + + o.spec('scan', function () { + var scan + + o.beforeEach(function () { + scan = require('../scan') + }) + + o('defaults to seed', function () { + var parent = prop() + var child = scan(function (out, p) { + return out - p + }, 123, parent) + o(child()).equals(123) + }) + + o('accumulates values as expected', function () { + var parent = prop() + var child = scan(function (arr, p) { + return arr.concat(p) + }, [], parent) + + parent(7) + parent('11') + parent(undefined) + parent({ a: 1 }) + var result = child() + + // deepEquals fails on arrays? + o(result[0]).equals(7) + o(result[1]).equals('11') + o(result[2]).equals(undefined) + o(result[3]).deepEquals({ a: 1 }) + }) + }) + + o.spec('scanMerge', function () { + var scanMerge + + o.beforeEach(function () { + scanMerge = require('../scan-merge') + }) + + o('defaults to seed', function () { + var parent1 = prop() + var parent2 = prop() + + var child = scanMerge([ + [parent1, function (out, p1) { + return out + p1 + }], + [parent2, function (out, p2) { + return out + p2 + }] + ], -10) + + o(child()).equals(-10) + }) + + o('accumulates as expected', function () { + var parent1 = prop() + var parent2 = prop() + + var child = scanMerge([ + [parent1, function (out, p1) { + return out + p1 + }], + [parent2, function (out, p2) { + return out + p2 + p2 + }] + ], 'a') + + parent1('b') + parent2('c') + parent1('b') + + o(child()).equals('abccb') + }) + }) +})