/** * **/ define( [ 'exports' ], function( exports ) { /** * Header info comparator that orders messages in order of numerically * decreasing date and UIDs. So new messages come before old messages, * and messages with higher UIDs (newer-ish) before those with lower UIDs * (when the date is the same.) */ var cmpHeaderYoungToOld = exports.cmpHeaderYoungToOld = function cmpHeaderYoungToOld(a, b) { var delta = b.date - a.date; if (delta) return delta; // favor larger UIDs because they are newer-ish. return b.id - a.id; } /** * Perform a binary search on an array to find the correct insertion point * in the array for an item. From deuxdrop; tested in * deuxdrop's `unit-simple-algos.js` test. * * @return[Number]{ * The correct insertion point in the array, thereby falling in the inclusive * range [0, arr.length]. * } */ var bsearchForInsert = exports.bsearchForInsert = function bsearchForInsert(list, seekVal, cmpfunc) { if (!list.length) return 0; var low = 0, high = list.length - 1, mid, cmpval; while (low <= high) { mid = low + Math.floor((high - low) / 2); cmpval = cmpfunc(seekVal, list[mid]); if (cmpval < 0) high = mid - 1; else if (cmpval > 0) low = mid + 1; else break; } if (cmpval < 0) return mid; // insertion is displacing, so use mid outright. else if (cmpval > 0) return mid + 1; else return mid; }; var bsearchMaybeExists = exports.bsearchMaybeExists = function bsearchMaybeExists(list, seekVal, cmpfunc, aLow, aHigh) { var low = ((aLow === undefined) ? 0 : aLow), high = ((aHigh === undefined) ? (list.length - 1) : aHigh), mid, cmpval; while (low <= high) { mid = low + Math.floor((high - low) / 2); cmpval = cmpfunc(seekVal, list[mid]); if (cmpval < 0) high = mid - 1; else if (cmpval > 0) low = mid + 1; else return mid; } return null; }; /** * Partition a list of messages (identified by message namers, aka the suid and * date of the message) by the folder they belong to. * * @args[ * @param[messageNamers @listof[MessageNamer]] * ] * @return[@listof[@dict[ * @key[folderId FolderID] * @key[messages @listof[MessageNamer]] * ] */ exports.partitionMessagesByFolderId = function partitionMessagesByFolderId(messageNamers) { var results = [], foldersToMsgs = {}; for (var i = 0; i < messageNamers.length; i++) { var messageNamer = messageNamers[i], messageSuid = messageNamer.suid, idxLastSlash = messageSuid.lastIndexOf('/'), folderId = messageSuid.substring(0, idxLastSlash); if (!foldersToMsgs.hasOwnProperty(folderId)) { var messages = [messageNamer]; results.push({ folderId: folderId, messages: messages, }); foldersToMsgs[folderId] = messages; } else { foldersToMsgs[folderId].push(messageNamer); } } return results; }; exports.formatAddresses = function(nameAddrPairs) { var addrstrings = []; for (var i = 0; i < nameAddrPairs.length; i++) { var pair = nameAddrPairs[i]; // support lazy people providing only an e-mail... or very careful // people who are sure they formatted things correctly. if (typeof(pair) === 'string') { addrstrings.push(pair); } else if (!pair.name) { addrstrings.push(pair.address); } else { addrstrings.push( '"' + pair.name.replace(/["']/g, '') + '" <' + pair.address + '>'); } } return addrstrings.join(', '); }; }); // end define