Created
August 1, 2014 18:05
-
-
Save qcom/e1e65990455e3e0ece2f to your computer and use it in GitHub Desktop.
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
var moment = require('moment'); | |
var queryBuilder = require('./queryBuilder.js').bind(null, 'https://someurl.com'); | |
module.exports = helpers = {}; | |
/* delete all key/value pairs on obj that are equal to the provided val */ | |
helpers.deleteAll = function(obj, val) { | |
for (var key in obj) | |
if ((typeof obj[key] === 'string' ? obj[key].trim() : obj[key]) === val) | |
delete obj[key]; | |
return obj; | |
}; | |
/* delete all key/value pairs on target that are not in reference */ | |
helpers.matchReference = function(target, reference) { | |
var stripped = {}; | |
for (var key in target) | |
if (key in reference) | |
stripped[key] = target[key]; | |
return stripped; | |
}; | |
/* return an anchor tag of the passed moment() date */ | |
helpers.getPrettyDate = function(date) { | |
var time = moment(date).calendar().split(' '); | |
function getAnchor(s) { return '<a href="' + queryBuilder({ date_added : moment(date).format('MM-DD-YYYY') }).toString() + '">' + s + '</a>'; } | |
if (time.length === 5) { | |
return getAnchor(time.slice(0, 2).join(' ')) + ' ' + time.slice(2, time.length).join(' '); | |
} else if (time.length === 4) { | |
return getAnchor(time[0]) + ' ' + time.slice(1, time.length).join(' '); | |
} else { | |
return getAnchor(time); | |
} | |
}; | |
/* remove the time specification from moment.js' calendar() method */ | |
helpers.calendarDate = function(date) { | |
var time = moment(date).calendar().split(' '); | |
if (time.length === 1) { | |
return time.join(' '); | |
} else if (time.length === 4) { | |
return time[0]; | |
} else { | |
return time.slice(0, 2).join(' '); | |
} | |
}; | |
/* convert any valid web address into a standardized, protocol-specific URL format */ | |
helpers.formatUrl = function(url) { | |
var remove = ['https://', 'http://', 'www.']; | |
remove.forEach(function(s) { url = url.replace(s, ''); }); | |
return 'https://www.' + url; | |
}; | |
/* determines whether it is appropriate to display form history given a redirection key */ | |
helpers.getPreservedData = function(req, key) { | |
if (req.session.preserved && | |
req.session.preserved[key] && | |
req.session.lastVisited === '/' + key) { | |
return req.session.preserved[key]; | |
} else { | |
if (req.session.preserved) | |
delete req.session.preserved[key]; | |
return null; | |
} | |
}; | |
/* preserves form data (from req.body) but stores a custom, supplied error on the session object */ | |
helpers.preserveAndRedirect = function(req, res, id, error) { | |
req.session.error = error; | |
req.session.preserved = req.session.preserved || {}; | |
req.session.preserved[id] = req.body; | |
res.redirect(req.headers.referer.split('.com')[1]); | |
}; | |
/* mixin-like function for adding all passed objects' key/value pairs onto a blank object and returning that object */ | |
helpers.augment = function() { | |
var objs = Array.prototype.slice.call(arguments); | |
var result = {}; | |
objs.forEach(function(obj) { | |
for (var key in obj) | |
result[key] = obj[key]; | |
}); | |
return result; | |
}; | |
/* convert a string into a proper title casing */ | |
helpers.toProperCase = function(s) { | |
return s.split(' ').map(function(word) { | |
return word[0].toUpperCase() + word.slice(1).toLowerCase() | |
}).join(' '); | |
}; | |
/* convert an underscore_styled string to camelCase */ | |
helpers._ToCamel = function(s) { | |
var result = ''; | |
s.toLowerCase().split('_').forEach(function(term, idx) { | |
result += ((idx === 0 ? term[0] : term[0].toUpperCase()) + term.slice(1)); | |
}); | |
return result; | |
}; | |
/* apply an object of functions to a data object and add those key/value pairs to a new object and return */ | |
helpers.reduceFnObj = function(fnObj, dataObj) { | |
var o = {}; | |
for (var key in fnObj) | |
o[key] = fnObj[key](dataObj); | |
return o; | |
}; | |
/* convert an object with underscore_styled keys to one with camelCase */ | |
helpers._ObjToCamel = function(obj) { | |
var result = {}; | |
for (var key in obj) | |
result[helpers._ToCamel(key)] = obj[key]; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment