Skip to content

Instantly share code, notes, and snippets.

View gist:eb1da11044643b5cb7aee2f802f049b0
function getRandomIndex(len) {
return Math.floor(Math.random() * Math.floor(len));
}
function cycleArray(arr, i) {
return arr.slice(i).concat(arr.slice(0, i))
}
function cycleArrayByRandomIndex() {
return cycleArray(arr, getRandomIndex(arr.length))
View reactComponentUtils
import React from 'react'
export function isComponentWithName (el, name) {
if (typeof name === 'string') {
return React.isValidElement(el) && name === getDisplayName(el)
}
const validRegEx = name instanceof RegExp ? name : new RegExp(name)
return React.isValidElement(el) && validRegEx.test(getDisplayName(el))
}
@geraldfullam
geraldfullam / Math.isPrime
Last active Jan 17, 2018
Extends the Math object with isPrime, optimized to check only odd numbers greater than two and less than square root.
View Math.isPrime
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
@geraldfullam
geraldfullam / toDate.js
Created Jun 13, 2016
DIY Date manipulations in JavaScript: how to get days, weeks, and months from now with chainable utility methods.
View toDate.js
/* 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());
View A-jQuery-plugin-deepest().markdown

jQuery plugin: .deepest()

Get the deepest descendants matching an optional selector of each element in the set of matched elements.

A Pen by Gerald on CodePen.

License.

Installation

View A-jQuery-plugin:-$().removeClassExcept().markdown

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.

View Use-of-:not()-in-delegated-event.markdown

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 / index.html
Last active Aug 29, 2015
jQuery tooltip: accessibility friendly
View index.html
<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>
View Layered-Event-Listeners.markdown