object-from-streams.js
1.11 KB
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
45
46
47
48
49
50
51
var _ = require('lodash');
var asyncEach = require('async-each');
var stream = require('./stream').obj;
module.exports = function (spec) {
var objectStream = stream.create();
asyncEach(Object.keys(spec), function (key, done) {
if (stream.validate(spec[key])) {
spec[key].pipe(stream.concat(function (data) {
var obj = {};
obj[key] = data;
objectStream.push(obj);
done();
}));
}
else if (typeof spec[key] === 'object' && spec[key].onArray && stream.validate(spec[key].data)) {
spec[key].data.pipe(stream.concat(function (data) {
var obj = {};
obj[key] = spec[key].onArray(data);
objectStream.push(obj);
done();
}));
}
else {
var obj = {};
obj[key] = spec[key];
objectStream.push(obj);
done();
}
}, function () {
objectStream.end();
});
// TODO: right now this returns a stream containing the whole object.
// It might be could to split it by key?
return objectStream
.pipe(stream.reduce(_.extend));
};