/
rx.contribute.js
123 lines (100 loc) · 3.89 KB
/
rx.contribute.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* RxJS Contribute
*
* Copyright (c) 2012 Christoph Burgdorf (http://twitter.com/cburgdorf)
* Licensed under the MIT (https://github.com/cburgdorf/rxjs-contribute/blob/master/license.txt) license.
*/
(function (global) {
var root;
if (typeof global.module !== 'undefined' && typeof global.module.exports !== 'undefined') {
root = global.require('./rx-vsdoc.js');
} else {
root = global.Rx;
}
var Observable = root.Observable,
observableProto = Observable.prototype,
isFunction = function(fun) { return typeof(fun) === "function"; };
observableProto.whereTrue = function (propertyName) {
return this.where(function (x) {
return propertyName === undefined ? x === true : x[propertyName] === true;
});
};
observableProto.whereFalse = function (propertyName) {
return this.where(function (x) {
return propertyName === undefined ? x === false : x[propertyName] === false;
});
};
observableProto.wrapAs = function (propertyName) {
return this.select(function (x) {
var temp = {};
temp[propertyName] = x;
return temp;
});
};
observableProto.appendAs = function (propertyName, data) {
return this.select(function (x) {
if (x !== null && typeof (x) == 'object') {
x[propertyName] = isFunction(data) ? data() : data;
return x;
}
else {
var temp = {};
temp[propertyName] = isFunction(data) ? data() : data;
return temp;
}
});
};
observableProto.convertProperty = function (propertyFrom, propertyTo, transistorFunc) {
return this.select(function (x) {
if (x.hasOwnProperty(propertyFrom) && isFunction(transistorFunc)) {
x[propertyTo] = transistorFunc(x[propertyFrom]);
}
return x;
});
};
observableProto.selectProperty = function (propertyName) {
return this.select(function (x) {
if (x.hasOwnProperty(propertyName)) {
return x[propertyName];
}
return x;
});
};
observableProto.selectAs = function (as) {
return this.select(function (_) {
return as;
});
};
Observable.forkJoin = function (sources) {
var tempSources = arguments.length > 1 ? arguments : sources;
return Rx.Observable
.fromArray(tempSources)
.selectMany(function (o, i) {
return o.takeLast(1).select(function (value) { return { i: i, value: value }; });
})
.aggregate({ array: [], count: 0 }, function (results, result) {
results.array[result.i] = result.value;
return {
array: results.array,
count: results.count + 1
};
})
.where(function (results) { return results.count === tempSources.length; })
.select(function (results) { return results.array; });
};
observableProto.combineLatestOnLeft = function (rightSource, selector) {
return this.timestamp()
.combineLatest(rightSource.timestamp(), function (l, r) {
return {
Left: l,
Right: r
};
})
.where(function (x) {
return x.Left.timestamp >= x.Right.timestamp;
})
.select(function (x) {
return selector(x.Left.value, x.Right.value);
});
};
})(this);