Files: aacf3bddbfecb924c25697a24bafc3bb0b1b86ad / when.js
711 bytesRaw
1 | var computed = require('./computed') |
2 | var isObservable = require('./is-observable') |
3 | |
4 | module.exports = When |
5 | |
6 | function When (obs, ifTrue, ifFalse) { |
7 | ifTrue = handleInnerValues(ifTrue) |
8 | ifFalse = handleInnerValues(ifFalse) |
9 | return computed([obs, ifTrue, ifFalse], lambda) |
10 | } |
11 | |
12 | function lambda (value, ifTrue, ifFalse) { |
13 | return value ? ifTrue : ifFalse |
14 | } |
15 | |
16 | function 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