-
Notifications
You must be signed in to change notification settings - Fork 84
/
index.js
44 lines (39 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var flyd = require('../../lib');
function isPlainObject(obj) {
return obj !== null && typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype;
}
var streamProps = function(from) {
var to = {};
for (var key in from) {
if (from.hasOwnProperty(key)) {
to[key] = isPlainObject(from[key]) ? streamProps(from[key]) : flyd.stream(from[key]);
}
}
return to;
};
var extractProps = function(obj) {
var newObj = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = isPlainObject(obj[key]) ? extractProps(obj[key])
: flyd.isStream(obj[key]) ? obj[key]()
: obj[key];
}
}
return newObj;
};
var stream = function(obj) {
var streams = Object.keys(obj).map(function(key) {
return isPlainObject(obj[key]) ? stream(obj[key])
: flyd.isStream(obj[key]) ? obj[key]
: flyd.stream(obj[key]);
});
return flyd.combine(function() {
return extractProps(obj);
}, streams);
};
module.exports = {
streamProps: streamProps,
extractProps: extractProps,
stream: stream
};