Skip to content

Instantly share code, notes, and snippets.

View mjackson's full-sized avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
@mjackson
mjackson / download-counts.mjs
Last active January 5, 2026 04:18
Download counts for react-router
const npmApiUrl = "https://api.npmjs.org/downloads/point";
async function fetchDownloadCounts(packageName, year) {
let range = `${year}-01-01:${year}-12-31`;
let response = await fetch(`${npmApiUrl}/${range}/${packageName}`);
return (await response.json()).downloads;
}
async function getDownloads(startYear, endYear = startYear) {
if (endYear < startYear) {
@mjackson
mjackson / resolvePromise.js
Last active January 5, 2026 03:52
An easy way to do async APIs in JavaScript that support both promises *and* callbacks!
// Here is a function that I use all the time when creating public
// async APIs in JavaScript:
const resolvePromise = (promise, callback) => {
if (callback)
promise.then(value => callback(null, value), callback)
return promise
}
// Sometimes I like to use callbacks, but other times a promise is
<!DOCTYPE html>
<html>
<head>
<title>XML.js Test</title>
<script type="text/javascript" src="xml.js"></script>
<script type="text/javascript">
var xml = new XML('root');
xml.attr('one', 1);
@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 / 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 / mockstream.js
Created September 7, 2011 17:31
A mock stream wrapper for node.js
var util = require("util"),
Stream = require("stream").Stream;
module.exports = MockStream;
/**
* A constructor that inherits from Stream and emits data from the given
* `source`. If it's a Stream it will be piped through to this stream.
* Otherwise, it should be a string or a Buffer which will be emitted by this
* stream as soon as possible.
@mjackson
mjackson / FirebaseStateMixin.js
Last active January 5, 2026 03:45
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 / Dispatcher.js
Created August 21, 2014 19:55
An async version of Facebook's flux dispatcher
var d = require('d');
var warning = require('react/lib/warning');
var Promise = require('es6-promise').Promise;
function isNotNull(object) {
return object != null;
}
function Dispatcher() {
this._currentActionName = null;
var promise = new AbortablePromise(function (resolve, reject, onAbort) {
// Use resolve & reject as you normally would.
var request = makeRequest( ... , function (error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
});