Skip to content

Instantly share code, notes, and snippets.

View mjackson's full-sized avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
@mjackson
mjackson / strftime.js
Created January 4, 2011 18:53
A strftime implementation for JavaScript Date objects.
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var shortDays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function zeropad(n) {
return n > 9 ? String(n) : "0" + String(n);
}
function twelveHour(t) {
@mjackson
mjackson / Array#pick_random.rb
Created May 20, 2011 21:19
Pick n random items from an Array quickly
class Array
# Pick +n+ random items from this array.
def pick_random(n=1)
sort_by { rand }.slice(0...n)
end
end
@mjackson
mjackson / events.js
Created May 31, 2011 18:05
Generic add/remove event functions for JavaScript
// Event handling functions modified from originals by Dean Edwards.
// http://dean.edwards.name/my/events.js
var guid = 1;
// Adds an event handler to the given element. The handler will be called
// in the context of the element with the event object as its only argument.
function addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
@mjackson
mjackson / intersect.js
Created July 9, 2011 18:32
Get the intersection of two sorted arrays of numbers or strings.
// Returns the intersection of two sorted arrays of numbers or strings.
function intersectArrays(a, b) {
var array = [], ai = 0, alen = a.length, bi = 0, blen = b.length;
while (ai < alen && bi < blen) {
if (a[ai] < b[bi]) {
ai++;
} else if (a[ai] > b[bi]) {
bi++;
} else {
@mjackson
mjackson / blocks.rb
Created August 11, 2011 18:30
Demonstrates the difference between Ruby's two different block styles.
def a(*args, &block)
puts "a got a block" if block_given?
end
def b(*args, &block)
puts "b got a block" if block_given?
end
# In this call, `b' is called with the block and the
# return value is given to `a' as an argument.
var JSONStream = require('jsonstream')
var es = require('event-stream')
function handleRequest(data, callback) {
callback(null, JSON.stringify(data) + '\n')
}
process.stdin
.pipe(JSONStream.parse())
.pipe(es.map(handleRequest))
@mjackson
mjackson / FirebaseStateMixin.js
Last active February 5, 2016 11:34
A simple mixin for React components that need to bind state to a Firebase ref
var Firebase = require('firebase');
var baseRef = new Firebase('https://my-firebase.firebaseio.com');
function getSnapshotValue(snapshot) {
return snapshot.val();
}
/**
* A mixin for components that want to bind the value of a state variable
* to the value at a Firebase ref.
@mjackson
mjackson / babel+webpack.js
Last active March 2, 2016 16:22
Crazy babel+webpack bug.
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["HTTPClient"] = factory();
else
root["HTTPClient"] = factory();
})(this, function() {
<div className="pricing" style={{ opacity: purchasing ? 0.25 : '' }}>
{purchaseComplete ? (
<div className="purchase-complete">
<h2>Thanks!</h2>
<p>
Thank you for your purchase of {formatPrice(this.state.total)}.
We’ll send you a receipt shortly.
</p>
<p>
<button
@mjackson
mjackson / SVGIcons.js
Last active June 24, 2016 13:08
Building reusable SVG icons w React
import React from 'react'
// So, this is awesome. And it's the approach I took in my own site.
// Nothing wrong with it!
const IconUmbrella = React.createClass({
render() {
return (
<svg className="umbrella" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32" aria-labelledby="umbrella icon">
<title>Umbrella</title>
<path d="M27 14h5c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2v0zM27 14c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2v0 14c0 1.112-0.895 2-2 2-1.112 0-2-0.896-2-2.001v-1.494c0-0.291 0.224-0.505 0.5-0.505 0.268 0 0.5 0.226 0.5 0.505v1.505c0 0.547 0.444 0.991 1 0.991 0.552 0 1-0.451 1-0.991v-14.009c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2c0-1.105-1.119-2-2.5-2s-2.5 0.895-2.5 2c0-5.415 6.671-9.825 15-9.995v-1.506c0-0.283 0.224-0.499 0.5-0.499 0.268 0 0.5 0.224 0.5 0.499v1.506c8.329 0.17 15 4.58 15 9.995h-5z"/>