-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
101 lines (94 loc) · 2.81 KB
/
helpers.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
var http = require('http');
/**
* requestData is a stub for PUBNUB.history
*
* @param {function} successCallback
* @param {function} errorCallback
* @return {undefined}
*/
function requestData(successCallback, errorCallback) {
return http.get({
port: 3000
}, function (res) {
var response = '';
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
successCallback(JSON.parse(response));
});
res.on('error', errorCallback);
});
}
exports.requestData = requestData;
// TODO: document
function getHashByPropertyValue (property, collection) {
var len = collection.length,
i = 0,
collector = {};
while(i < len){
var current;
if(collection[i].hasOwnProperty(property)){
current = collector[collection[i][property]] = {};
for(var prop in collection[i]){
if(prop !== property){
current[prop] = collection[i][prop];
}
}
}
i++;
}
return collector;
}
exports.getHashByPropertyValue = getHashByPropertyValue;
// TODO: document
function extractFromEach(key) {
return function (collection) {
var collector = [],
len = collection.length,
i = 0;
while(i < len){
if(collection[i].hasOwnProperty(key)){
collector = collector.concat(collection[i][key]);
}
i++;
}
return collector;
};
}
exports.extractFromEach = extractFromEach;
// TODO: document
function getTotalByProperty(key, collection) {
return collection.reduce(function (prev, curr) {
return prev + (curr[key] ? curr[key].length : 0);
},0);
}
exports.getTotalByProperty = getTotalByProperty;
// TODO: history threshold: if call returns fewer than N records, prevent
// additional calls
function iteratorFactory(success, error) {
var count = 0;
return function iterateConversations(conversations, history) {
var historyByChannel = getHashByPropertyValue('key', history),
hits = 0;
for(var i = 0; i < conversations.length; i++){
var conversation = conversations[i],
channel = conversation.channel;
if(historyByChannel.hasOwnProperty(channel)){
conversation.timestamp = historyByChannel[channel].timestamp;
count++;
hits++;
}
if(hits === history.length){
break;
}
}
if(count === conversations.length){
return success(conversations);
}
requestData(function (history) {
iterateConversations(conversations, history);
}, error);
};
}
exports.iteratorFactory = iteratorFactory;