git ssb

1+

Matt McKegg / mutant



Tree: aacf3bddbfecb924c25697a24bafc3bb0b1b86ad

Files: aacf3bddbfecb924c25697a24bafc3bb0b1b86ad / 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