Created
July 17, 2012 15:39
-
-
Save jrusbatch/3130163 to your computer and use it in GitHub Desktop.
Count unique user names in Jabbr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
if (typeof Array.prototype.unique !== 'function'){ | |
Array.prototype.unique = function() { | |
var a = this.concat(); | |
for (var i = 0, l = a.length; i < l; ++i) { | |
for (var j = i + 1; j < l; ++j) { | |
if (a[i] === a[j]) { | |
a.splice(j, 1); | |
} | |
} | |
} | |
return a; | |
}; | |
} | |
var chat = $.connection.chat; | |
function WhenAll(requests) { | |
return $.when.apply($, requests); | |
} | |
function ToUserNames(user) { | |
return user.Name; | |
} | |
function OnRoomsReceived(rooms) { | |
var outstandingRequests = []; | |
for (var i = 0, l = rooms.length; i < l; i++) { | |
var room = rooms[i]; | |
if (room.Count > 0) { | |
var req = chat.getRoomInfo(room.Name); | |
outstandingRequests.push(req); | |
} | |
} | |
WhenAll(outstandingRequests) | |
.done(function() { | |
var roomDetails = arguments, | |
allUserNames = []; | |
for (var i = 0, l = roomDetails.length; i < l; i++) { | |
var users = roomDetails[i].Users, | |
names = users.map(ToUserNames); | |
allUserNames = allUserNames.concat(names).unique(); | |
} | |
console.log('Total unique users: ' + allUserNames.length); | |
console.log(allUserNames); | |
}) | |
.fail(function(reqs){ | |
console.log('failed'); | |
}); | |
} | |
chat.getRooms().done(OnRoomsReceived); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment