// Scholar for Firefox Utilities ///////////////////////////////////////////////////////////////// // // Scholar.Utilities // ///////////////////////////////////////////////////////////////// Scholar.Utilities = function () {} Scholar.Utilities.prototype.debug = function(msg) { Scholar.debug(msg, 4); } /* * See Scholar.Date */ Scholar.Utilities.prototype.formatDate = function(date) { return Scholar.Date.formatDate(date); } Scholar.Utilities.prototype.strToDate = function(date) { return Scholar.Date.strToDate(date); } /* * Cleans extraneous punctuation off an author name */ Scholar.Utilities.prototype.cleanAuthor = function(author, type, useComma) { if(typeof(author) != "string") { throw "cleanAuthor: author must be a string"; } author = author.replace(/^[\s\.\,\/\[\]\:]+/, ''); author = author.replace(/[\s\,\/\[\]\:\.]+$/, ''); author = author.replace(/ +/, ' '); if(useComma) { // Add period for initials if(author.substr(author.length-2, 1) == " " || author.substr(author.length-2, 1) == ".") { author += "."; } var splitNames = author.split(/, ?/); if(splitNames.length > 1) { var lastName = splitNames[0]; var firstName = splitNames[1]; } else { var lastName = author; } } else { var spaceIndex = author.lastIndexOf(" "); var lastName = author.substring(spaceIndex+1); var firstName = author.substring(0, spaceIndex); } // TODO: take type into account return {firstName:firstName, lastName:lastName, creatorType:type}; } /* * Cleans whitespace off a string and replaces multiple spaces with one */ Scholar.Utilities.prototype.cleanString = function(s) { if(typeof(s) != "string") { throw "cleanString: argument must be a string"; } s = s.replace(/[\xA0\r\n\s]+/g, " "); s = s.replace(/^\s+/, ""); return s.replace(/\s+$/, ""); } /* * Cleans any non-word non-parenthesis characters off the ends of a string */ Scholar.Utilities.prototype.superCleanString = function(x) { if(typeof(x) != "string") { throw "superCleanString: argument must be a string"; } var x = x.replace(/^[^\w(]+/, ""); return x.replace(/[^\w)]+$/, ""); } /* * Eliminates HTML tags, replacing
s with /ns */ Scholar.Utilities.prototype.cleanTags = function(x) { if(typeof(x) != "string") { throw "cleanTags: argument must be a string"; } x = x.replace(/]*>/gi, "\n"); return x.replace(/<[^>]+>/g, ""); } /* * Test if a string is an integer */ Scholar.Utilities.prototype.isInt = function(x) { if(parseInt(x) == x) { return true; } return false; } /* * Get current scholar version */ Scholar.Utilities.prototype.getVersion = function() { return Scholar.version; } /* * Get a page range, given a user-entered set of pages */ Scholar.Utilities.prototype._pageRangeRegexp = /^\s*([0-9]+)-([0-9]+)\s*$/; Scholar.Utilities.prototype.getPageRange = function(pages) { var pageNumbers; var m = this._pageRangeRegexp.exec(pages); if(m) { // A page range pageNumbers = [m[1], m[2]]; } else { // Assume start and end are the same pageNumbers = [pages, pages]; } return pageNumbers; } /* * provide inArray function */ Scholar.Utilities.prototype.inArray = Scholar.inArray; /* * pads a number or other string with a given string on the left */ Scholar.Utilities.prototype.lpad = function(string, pad, length) { while(string.length < length) { string = pad + string; } return string; } /* * returns true if an item type exists, false if it does not */ Scholar.Utilities.prototype.itemTypeExists = function(type) { if(Scholar.ItemTypes.getID(type)) { return true; } else { return false; } } /* * Cleans a title, capitalizing the proper words and replacing " :" with ":" */ Scholar.Utilities.capitalizeSkipWords = ["but", "or", "yet", "so", "for", "and", "nor", "a", "an", "the", "at", "by", "from", "in", "into", "of", "on", "to", "with", "up", "down"]; Scholar.Utilities.prototype.capitalizeTitle = function(title) { title = this.cleanString(title); title = title.replace(/ : /g, ": "); var words = title.split(" "); // always capitalize first words[0] = words[0][0].toUpperCase() + words[0].substr(1); if(words.length > 1) { var lastWordIndex = words.length-1; // always capitalize last words[lastWordIndex] = words[lastWordIndex][0].toUpperCase() + words[lastWordIndex].substr(1); if(words.length > 2) { for(var i=1; i