Skip to content

Instantly share code, notes, and snippets.

View erberg-snippets's full-sized avatar

erberg-snippets

View GitHub Profile
/**
* Build a SOLR query using kay-value pairs from queryObject.
* @param {Object} queryObject Object conaining the name and value for each search parameter.
* @return {String} Query string to be appended to URL
*/
function buildSOLRQuery(queryObject) {
var queryString = '';
for (key in queryObject) {
if (queryObject[key] !== '') {
queryString += (queryString ? ' AND ' : '') + //If queryString is not empty, add '&' at the beginning
/**
* Creates a simple date string from a Date object.
* @param {Date} dateVariable Any date object.
* @return {String} String representation of date in format mm-dd-yyyy
*/
function convertDateToString(date) {
var day = date.getDate();
var month = date.getMonth() + 1; //Months are zero based
var year = date.getFullYear();
return month + "-" + day + "-" + year ;
/**
* Prevents the default action of submit when hitting enter on form input.
* @param {jQuery Object} domElement DOM form element on which to block the enter event.
* @return {jQuery Object} Return input object for chaining.
*/
function preventFormSubmitOnEnter(domElement){
domElement.keydown(function(event){
if ( event.keyCode == 13 ){ //Keycode 13 is the 'enter' key.
event.preventDefault(); //Ignore it when pressed...
}
/**
* UI to sort rows of data by clicking on their parent title.
* titleDomObject should have children - one of which has the 'descending' or 'ascending' class.
* @param {jQuery Object} titleDomObject Contains one or more children with the 'descending' or 'ascending' class, and the text of the html to be sorted.
* @param {Function} sortCallback Callback should re-render the contents of the titles based on the child that was clicked.
*/
function setupSortingHandlers(titleDomObject,sortCallback) {
var sortAscending = true;
titleElements = titleDomObject.children();
/**
* Returns value of specified URL parameter.
* @param {String} name Query parameter to return value of.
* @return {String} String of query parameter or null / 0.
*/
getUrlParameter = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#?]*)').exec(window.location.href);
if (results==null){ return null; }
else { return results[1] || 0; }
}
var monthFullNameArray = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
/**
* Returns the suffix related to the number for ex. (1st, 2nd, 3rd, 4th)
* @param {Number} number Number to find the suffix for.
* @return {String} Suffix string to append to be appended to number.
*/
function getNumberSuffix(number) {
var numberConvertedToString = (number + '');
var lastDigit = numberConvertedToString.charAt(numberConvertedToString.length - 1);
if (lastDigit === '1') {
/**
* Changes the first character of each word to upper case, and any other characters to lowercase.
* @return {String} String formatted as a title.
*/
String.prototype.toTitleCase = function() {
var title = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
/**
* Takes a string and returns true if its a valid email address.
* @param {String} email Email address to be tested for validity.
* @return {Boolean} True if valid email, false if not.
*/
function isValidEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}