Skip to content

Instantly share code, notes, and snippets.

@jabney
jabney / declare.js
Last active August 29, 2015 14:10
An experiment with "forward type checking" in JavaScript. Execute a method based upon an object's type.
// declare.js MIT License © 2014 James Abney http://github.com/jabney
// An experiment with "forward type checking". Register types
// using 'declare' along with corresponding methods. The methods
// get called depending upon the type of the passed object.
//
// // Create a new declare object.
// var logByType = fwdType.declare();
//
// // Register types and methods.
@jabney
jabney / encode.js
Last active August 29, 2015 14:10
Encode an object's type as a numeric value.
// encode.js MIT License © 2014 James Abney http://github.com/jabney
// Encode built-in type as a numeric value.
//
// typeOf(null); // => 'Null'
// typeOf(undefined); // => 'Undefined'
// typeOf([]); // => 'Array'
//
// encodeType(null); // => 1
// encodeType(undefined); // => 2
@jabney
jabney / shuffle.js
Last active August 29, 2015 14:11
Shuffle an array in place so that the positions of its values are random
// shuffle.js © 2014 James Abney http://github.com/jabney
(function(ex) {
'use strict';
// Export shuffle to 'random' namespace.
var random = ex.random || (ex.random = Object.create(null));
// Shuffle an array in place so that the positions of its values are random.
random.shuffle = function shuffle(a) {
var i, r, tmp, len = a.length;
@jabney
jabney / huffman.js
Last active August 29, 2015 14:11
A toy implementation of Huffman compression
// huffman.js © 2014 James Abney http://github.com/jabney
// Huffman coding was invented by David A. Huffman of MIT, 1952.
(function() {
'use strict';
// A toy implementation of Huffman compression. The result of
// encoding is a bit string. In this form it's not truly compressed,
// but the bit string represents the binary values after compression.
function huffman(str) {
var ft = frequencyTable(str), ht = huffTree(ft);
@jabney
jabney / graph.js
Last active August 29, 2015 14:12
Graph abstract data type and some associated algorithms
// graph.js © 2015 James Abney http://github.com/jabney
(function(ex, undefined) {
'use strict';
var gr = ex.graph || (ex.graph = Object.create(null));
// ---------------------------------------------------------------
// The graph abstract data type.
// ---------------------------------------------------------------
gr.graph = function graph(numVerts) {
@jabney
jabney / containers.js
Last active August 29, 2015 14:13
Containers deque, bag, stack, queue, priority queue, and set
/*
containers.js
MIT License © 2015 James Abney
http://github.com/jabney
Containers: bag, stack, queue, double-ended queue (deque),
priorityQueue, and set.
See documentation at http://github.com/jabney/containers.js
*/
@jabney
jabney / augment.js
Last active August 29, 2015 14:19
Javascript augmentation pattern for a factory method
// This augmentation style is appropriate for a factory-style function,
// which might be something that was imported as part of a library.
var factory = function factory() {
var data = [];
// Return an object with one or more methods.
return {
init: function() {
data.push.apply(data, arguments);
@jabney
jabney / gen_fontawesome.py
Last active August 29, 2015 14:21
Python utility for converting Font Awesome cheatsheet to a JSON file
"""
Generate fontawesome icon set in JSON format from website cheatsheet.
http://fortawesome.github.io/Font-Awesome/cheatsheet/
Usage: python gen_fontawesome.py
Output: font-awesome.json
{icon_name: entity_reference, ...}
Author: James Abney
Date: 2015-05-21
@jabney
jabney / factory-inheritance.js
Last active August 29, 2015 14:22
JavaScript Factory Inheritance
(function() {
'use strict';
// Create a factory function (a function that returns an object).
function aFactory() {
// Create a data store.
var _data = [];
// Add supplied arguments to the data store.
if (arguments.length)
@jabney
jabney / post-transform-coords.js
Last active August 29, 2015 14:23
Javascript function to return post-transform coordinates of an element.
// Return the post-transform coords of an element,
// assuming that proper pre-transform coords are supplied.
function getElementCoords(element, coords) {
var ctm = element.getCTM(),
xn = ctm.e + coords.x*ctm.a,
yn = ctm.f + coords.y*ctm.d;
return { x: xn, y: yn };
};
var circle = document.getElementById('svgCircle'),