git ssb

1+

Matt McKegg / mutant



Tree: aa5cf8e200da764dc0c7adab9d38551e5d9abd13

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