add stream folder with scan and scan-merge
This commit is contained in:
parent
db956d7194
commit
db445a899d
4 changed files with 147 additions and 0 deletions
26
stream/scan-merge.js
Normal file
26
stream/scan-merge.js
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
//! adapted for mithril from flyd https://github.com/paldepind/flyd
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
var combine = require('../stream').combine
|
||||||
|
|
||||||
|
module.exports = function (tuples, seed) {
|
||||||
|
var streams = tuples.map(function (tuple) {
|
||||||
|
var stream = tuple[0]
|
||||||
|
if (stream._state.state === 0) stream(undefined)
|
||||||
|
return stream
|
||||||
|
})
|
||||||
|
|
||||||
|
var newStream = combine(function () {
|
||||||
|
var changed = arguments[arguments.length - 1]
|
||||||
|
|
||||||
|
streams.forEach(function (stream, idx) {
|
||||||
|
if (changed.indexOf(stream) > -1) {
|
||||||
|
seed = tuples[idx][1](seed, stream._state.value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return seed
|
||||||
|
}, streams)
|
||||||
|
|
||||||
|
return newStream
|
||||||
|
}
|
||||||
14
stream/scan.js
Normal file
14
stream/scan.js
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
17
stream/tests/index.html
Normal file
17
stream/tests/index.html
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="../../module/module.js"></script>
|
||||||
|
<script src="../../ospec/ospec.js"></script>
|
||||||
|
<script src="../../util/stream.js"></script>
|
||||||
|
<script src="../../stream.js"></script>
|
||||||
|
<script src="../scan.js"></script>
|
||||||
|
<script src="../scan-merge.js"></script>
|
||||||
|
<script src="test-stream.js"></script>
|
||||||
|
|
||||||
|
<script>require("../../ospec/ospec").run()</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
90
stream/tests/test-stream.js
Normal file
90
stream/tests/test-stream.js
Normal file
|
|
@ -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')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue