Skip to content

Instantly share code, notes, and snippets.

View mhairston's full-sized avatar

Michael Hairston mhairston

View GitHub Profile
@mhairston
mhairston / jasmine-sample.js
Created March 11, 2016 14:20
Jasmine sample test code
/*
Jasmine sample code
This file is called a spec.
describe() groups related tests within a spec.
it() contains a single test.
The outer describe() matches the name of the module being tested.
Select elements only via test-foo classes!
*/
@mhairston
mhairston / no-double-submit.js
Last active September 23, 2016 16:05
Prevent Double Form Submit
$('form').on('submit', function(evt) {
$('a, button:not([type]), [type="submit"]', this)
.prop('disabled', true);
$('a').addClass('is-disabled');
});
/*
The `is-disabled` class is added for the rare case that a link might
be set up to submit a form, since some browsers won't disable a link.
@mhairston
mhairston / event-delegate.js
Created October 27, 2016 17:59
Event Delegation with Plain JavaScript
// Event Delegation with Plain Javascript
// from Adam Beres-Deak
// http://bdadam.com/blog/plain-javascript-event-delegation.html
function on(elSelector, eventName, selector, fn) {
var element = document.querySelector(elSelector);
element.addEventListener(eventName, function(event) {
var possibleTargets = element.querySelectorAll(selector);
var target = event.target;
@mhairston
mhairston / object-extend.js
Created April 22, 2019 13:14
Extend JS object
// from https://plainjs.com/javascript/utilities/merge-two-javascript-objects-19/
function extend(obj, src) {
Object.keys(src).forEach(function(key) { obj[key] = src[key]; });
return obj;
}
My Awesome Sketch
Normal State*
hover -> Hover State
click -> Selected State
Hover State
unhover -> Normal State
click -> HoverSelected State
Selected State
hover -> HoverSelected State
clickanyother -> Normal State
@mhairston
mhairston / quantize.js
Created May 3, 2022 20:18
Quantize value to a multiple
/**
* Return a number "rounded" to the nearest multiple of `amount`.
*
* @param {number} num - The number to quantize
* @param {number} amount - the quantization factor
* @return {number} The multiple of `amount` that is nearest to `number`.
*/
quantize(num, amount) {
const halfway = Math.round(amount / 2);