Skip to content

Instantly share code, notes, and snippets.

@geraldfullam
geraldfullam / RTE RegEx Replacements
Created October 31, 2014 18:24
Common RegEx Replacements for Rich Text Editors
content = content
// Replace hypen, if between number, with en-dash
.replace(/(\d+)-(\d+)/ig, '$1–$2')
// Replace hypen, if between spaces, with em-dash
.replace(/( +)-+( +)/ig, '$1—$2')
// Replace smart double quotes with straight double quotes
.replace(/“|”|„|‟|″|‶/ig, '"')
@geraldfullam
geraldfullam / window.location.query
Created January 6, 2015 21:41
JavaScript: Global access to query string variables as a map
// Source: http://stackoverflow.com/a/13455920/2502532
// -------------------------------------------------------------------
// Add prototype for 'window.location.query([source])' which contain an object
// of querystring keys and their values
// -------------------------------------------------------------------
if(!window.location.query) {
window.location.query = function(source){
var map = {};
source = source || this.search;
@geraldfullam
geraldfullam / JS Get Query Param Value by Name
Created January 7, 2015 04:07
Get query parameter values by key name from window.location.search string
/* Based on: http://stackoverflow.com/a/901144/2502532 */
var getParam = function(key) {
key = key.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var val = window.location.search.match(new RegExp('[\\?&]' + key + '=([^&#]*)'));
return val === null ? '' : decodeURIComponent(val[1].replace(/\+/g, ' '));
};
console.log(getParam('foo'));
@geraldfullam
geraldfullam / Layered-Event-Listeners.markdown
Last active August 29, 2015 14:14
Layered Event Listeners
@geraldfullam
geraldfullam / index.html
Last active August 29, 2015 14:15
jQuery tooltip: accessibility friendly
<div class="button-list namespace">
<h2>$('selector').tooltip()</h2>
<ul>
<li>
<span>Lorem Ipsum Dolar Sit</span>
<button type="button">+
<span class="tooltip" role="tooltip">Hello World again and again and again and again.</span>
</button>
</li>
<li>
@geraldfullam
geraldfullam / Use-of-:not()-in-delegated-event.markdown
Created March 18, 2015 14:40
Use of :not() in delegated event

Use of :not() in delegated event

Proof that using :not() in event delegation is honored, but doesn't prevent bubbling and thus potentially triggering the handler multiple times.

A Pen by Gerald on CodePen.

License.

@geraldfullam
geraldfullam / A-jQuery-plugin:-$().removeClassExcept().markdown
Last active August 29, 2015 14:17
jQuery plugin: $().removeClassExcept()

jQuery plugin: $().removeClassExcept()

Extend jQuery with $().removeClassExcept(); Removes all classes except those specified. Use as an alternative to .removeClass() when you only want to specify (or only know) which classes you want to keep.

A Pen by Gerald on CodePen.

License.

@geraldfullam
geraldfullam / toDate.js
Created June 13, 2016 14:11
DIY Date manipulations in JavaScript: how to get days, weeks, and months from now with chainable utility methods.
/* Define new prototype methods on Date object. */
// Returns Date as a String in YYYY-MM-DD format.
Date.prototype.toISODateString = function () {
return this.toISOString().substr(0,10);
};
// Returns new Date object offset `n` days from current Date object.
Date.prototype.toDateFromDays = function (n) {
n = parseInt(n) || 0;
var newDate = new Date(this.getTime());
@geraldfullam
geraldfullam / Math.isPrime
Last active January 17, 2018 21:06
Extends the Math object with isPrime, optimized to check only odd numbers greater than two and less than square root.
Math.isPrime = function (n) {
if (n === 2) { return true; }
if (n % 2 === 0) { return false; }
for(let i = 3, s = Math.sqrt(n); i <= s; i += 2) {
if (n % i === 0) { return false; }
}
return n !== 1;
};
// Based on this StackOverflow answer: https://stackoverflow.com/a/40200710/2502532