Skip to content

Instantly share code, notes, and snippets.

View mhairston's full-sized avatar

Michael Hairston mhairston

View GitHub Profile
@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);
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 / 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;
}
@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 / 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 / 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 / react-markup.js
Created February 14, 2016 00:19
Sample Markup in React
Markup in React
Javascript:
return React.createElement("p", {className: "foo"}, "Hello, world!");
JSX:
return (<p className="foo">Hello, world!</p>);
@mhairston
mhairston / jquery-object-merge.js
Created January 28, 2016 21:57
Merging Objects with jQuery
// Merging objects via jQuery
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
// merged objects not modified -- creates new object `settings`
var settings = $.extend( {}, defaults, options );
@mhairston
mhairston / copy-and-zip.sh
Created December 21, 2015 20:26
Copy & Zip files
#!/bin/sh
ditto -c -k --rsrc --keepParent ~/Documents/ Docs.zip
@mhairston
mhairston / date-format.sh
Created December 21, 2015 19:58
Preferred date format
#!/bin/sh
DATE="`date +%Y%m%d"-"%H%M`"