Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / fps-reduce.js
Last active November 6, 2022 07:34
FPS Reduce -- a stream of times to use as an alternative to event loops.
var reducible = require("reducible/reducible");
var isReduced = require("reducible/is-reduced");
function fps(desiredFps) {
// Create a stream of times to use as an event loop with
// https://github.com/Gozala/coreduction/blob/master/coreduction.js
// Number -> Reducible[Float time, Float time, ...]
// Convert seconds to milliseconds.
var msPerFrame = 1000 / desiredFps
@gordonbrander
gordonbrander / example.js
Created March 1, 2013 17:51
updateState - a utility for representing/updating program state as an object.
var STATE_FILTERS = [
function clampIndex(state, old, update) {
return extend(state, {
// Validate and sanitize properties.
index: clamp(state.index, 0, state.units - 1)
});
},
function calculateOffset(state, old, update) {
return extend(state, {
// Create calculated properties.
@gordonbrander
gordonbrander / idOf.js
Last active December 14, 2015 06:29 — forked from anonymous/idOf.js
function uid() {
// Generate a unique and non-colliding id.
return Math.random().toString(36).slice(2);
}
// Create unique, non-colliding ID namespace.
var __id__ = uid();
function idOf(thing) {
return (thing && typeof(thing) === ('object' || 'function')) ?
@gordonbrander
gordonbrander / derp-modules.js
Created October 9, 2012 22:06
Simple, tiny, dumb JavaScript Modules
// Simple, tiny, dumb module definitions for Browser JavaScript.
//
// What it does:
//
// * Tiny enough to include anywhere. Intended as a shim for delivering
// browser builds of your library to folks who don't want to use script loaders.
// * Exports modules to `__modules__`, a namespace on the global object.
// This is an improvement over typical browser code, which pollutes the
// global object.
// * Prettier and more robust than the
@gordonbrander
gordonbrander / user-testing-script.markdown
Created September 12, 2012 20:53
User Testing Think Aloud Script

Preparation

  • Print a copy of wireframes to use as paper prototypes
  • Sharpie marker

Tip: You can use Photo Booth or iMovie to record video/audio from your Mac.

What is the job-to-be-done for this script?

@gordonbrander
gordonbrander / grid.less
Created August 31, 2012 23:21
Flexible Grid factory in Less
/* Creates a grid with a margins on either side of all units. To do the left-margin style of grid, change the .unit definition, subtracting 1 from the number of columns. */
@context: 740px;
@col: 40px;
@gutter: 20px;
.unit(@cols) {
@target: (@cols * @col) + (@cols * @gutter);
width: 100% * (@target / @context);
}
@gordonbrander
gordonbrander / tomethod-toLambda.js
Created July 12, 2012 17:05
toMethod and toLambda - higher order helpers for codebases that use both functional and OOP styles
// Convert a function that does not use `this` context into a function that
// does. The category of information that was previously passed as the first
// parameter of the function is now its `this` context.
function toMethod(lambda) {
return function () {
var args = [this];
var concat = args.concat;
// Spread arguments over arity of concat, since arguments is not a true array.
var combined = concat.apply(args, arguments);
return lambda.apply(null, combined);
@gordonbrander
gordonbrander / tiny-state-machine.js
Created May 25, 2012 17:01
World's Tiniest JavaScript State Machine
var StateMachine = {
// Register valid states for this object.
__states__: {
'success': ['failure'],
'failure': ['success']
},
// Set initial state.
__state__: 'success',
@gordonbrander
gordonbrander / statelessview.js
Created May 9, 2012 16:33
Potential Kicks Controller/View API
var postModels = new Models();
postModels.fetch();
postModels.on('change', function (postModels) {
$('.post').models(postModels)
.render(function (model) {
$(this)
.find('.meta .author').html(model.prop('meta.author')).end()
@gordonbrander
gordonbrander / chain.js
Created April 17, 2012 16:54
JavaScript Method Composition
// In JavaScript, we can treat `this` as an implied first argument that can be
// set via `Function.prototype.bind`, `Function.prototype.call` or
// `Function.prototype.apply`. If we treat `this` as an argument containing a
// value that should be operated on and returned, a number of benefits emerge:
//
// * All methods of an object can be easily chained, jQuery style.
// * It reduces the confusing nature of side-effects in JavaScript OOP by taking the
// implicit `this` object and treating it as an explicit data object to be
// operated on.
// * Any method of an object that returns