git ssb

1+

Matt McKegg / mutant



Tree: ef9e5ab9d31b22070e49efa9d5db1f9b8f20b572

Files: ef9e5ab9d31b22070e49efa9d5db1f9b8f20b572 / when.js

711 bytesRaw
1var computed = require('./computed')
2var isObservable = require('./is-observable')
3
4module.exports = When
5
6function When (obs, ifTrue, ifFalse) {
7 ifTrue = handleInnerValues(ifTrue)
8 ifFalse = handleInnerValues(ifFalse)
9 return computed([obs, ifTrue, ifFalse], lambda)
10}
11
12function lambda (value, ifTrue, ifFalse) {
13 return value ? ifTrue : ifFalse
14}
15
16function handleInnerValues (obs) {
17 if (Array.isArray(obs) && obs.some(isObservable)) {
18 // HACK: resolve inner observs
19 var inner = []
20 return computed(obs, function () {
21 for (var i = 0; i < arguments.length; i++) {
22 inner[i] = arguments[i]
23 }
24 inner.length = arguments.length
25 return inner
26 })
27 }
28 return obs
29}
30

Built with git-ssb-web