Skip to content

Instantly share code, notes, and snippets.

@loickreitmann
loickreitmann / String.toDashed.js
Created April 14, 2011 23:11
Converts a string to dashed.
/**
* toDashed: converts string to dashed
*/
String.prototype.toDashed = function () {
return this.replace(/^\s+|\s+$/g, "")
.toLowerCase()
.replace(/-/g, '')
.replace(/(\s[a-z0-9])/g, function ($1) {
return $1.replace(/\s/, '-');
})
@loickreitmann
loickreitmann / String.trim.js
Created April 14, 2011 22:54
String method to remove leading and trailing white space.
/**
* trim: removes leading and trailing white space
*/
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, "");
};
@loickreitmann
loickreitmann / String.toCamelCase.js
Created April 14, 2011 22:51
Simple String method to convert a string to camel case (i.e, "This is my string!? Dashes - removed !@#$%^&*()(.+=;':[]}{<>|,./\"" converts to "thisIsMyStringDashesRemoved".
/**
* toCamelCase: converts string to camel case
*/
String.prototype.toCameCase = function () {
return this.replace(/^\s+|\s+$/g, "")
.toLowerCase()
.replace(/(\s[a-z0-9])/g, function ($1) {
return $1.replace(/\s/, '').toUpperCase();
})
.replace(/\W/g, '');
@loickreitmann
loickreitmann / doCookie.js
Created December 6, 2010 22:21
JS function to get or set a cookie
/**
* doCookie: function to get or set a cookie
* @param name (string): cookie name
* @param value (string - optional): cookie value to set
* @param options (object - optional): possible options are
* - expires: (number of days | proper datetime string)
* - path: (string)
* - domain: (string)
*/
function doCookie(name, value, options) {
@loickreitmann
loickreitmann / getQuerystringParamByName.js
Created December 6, 2010 22:13
JS function to grab a QS param
function getQuerystringParamByName(name) {
var regexS, regex, results;
regexS = "[\\?&]" + name + "=([^&#]*)";
regex = new RegExp(regexS);
results = regex.exec(location.href);
if (results === null) {
return null;
} else {
return (results[1] !== '') ? results[1] : null;
}