Skip to content

Instantly share code, notes, and snippets.

View Fauntleroy's full-sized avatar

Timothy Kempf Fauntleroy

View GitHub Profile
// assume this is node.js/browserify land so this is actually private
var next_server_number = function( servers ){
var current_int = 1;
servers = servers.sort( function( a, b ){
return a - b;
});
for( var i = 0; i < servers.length; i++ ){
if( current_int === servers[i] ){
current_int++;
}
@Fauntleroy
Fauntleroy / client.js
Last active August 29, 2015 13:56
RPC + data stream
var rpc = require('rpc-stream');
var engine = require('engine.io-stream');
var rpc_client = rpc();
var engine_stream = engine('/test');
remote = rpc_client.wrap(['addMessage']);
engine_stream
.pipe( rpc_client )
@Fauntleroy
Fauntleroy / Modular.js
Last active August 29, 2015 13:56
"Modular" self-rendering module example
var NO_OP = function(){};
var domready = require('domready');
var sizzle = require('sizzle');
var elClass = require('element-class');
var jsonp = require('jsonp');
var xhr = require('xhr');
var Modular = function( config ){
this.url = config.url;
@Fauntleroy
Fauntleroy / gulpfile.js
Created February 21, 2014 01:24
I'm terrible at uglifyify
var gulp = require('gulp');
var vinylSource = require('vinyl-source-stream');
var watchify = require('watchify');
gulp.task( 'browserify', function(){
// use watchify instead of gulp-browserify and gulp.watch
var bundler = watchify('./src/universe-experiment.js');
bundler.transform('hbsfy');
bundler.transform('uglifyify');
var rebundle = function(){
@Fauntleroy
Fauntleroy / index.js
Created February 20, 2014 20:23
Use browserify + hbsfy to render solidus widgets client side
var $ = require('jquery');
// Set up hbsfy for Solidus feature parity
var Handlebars = require('hbsfy/runtime');
// Pull in Handlebars helpers
var HandlebarsHelper = require('handlebars-helper');
HandlebarsHelper.help( Handlebars );
// Set partials
Handlebars.registerPartial( 'favorite', require('../../views/favorite.hbs') );
var getElementsByClass = function( classname ){
var els_with_class = [];
var els = document.getElementsByTagName('*');
for( var i = 0; i < els.length; i++ ){
var el = els[i];
if( el.className.split(' ').indexOf( classname ) > -1 ) els_with_class.push( el );
}
return els_with_class;
};
var shoe = require('shoe');
var through = require('through');
var result = document.getElementById('result');
var stream = shoe('/chat');
stream.on( 'data', function( msg ){
console.log( 'data', msg );
});
// Thing constructor
var Thing = function(){
this.pies = ['cherry','apple'];
};
Thing.prototype = {};
Object.defineProperties( Thing.prototype, {
piescount: {
get: function(){ return this.pies.length; }
@Fauntleroy
Fauntleroy / convertOffset.js
Created January 25, 2014 02:20
Convert a time offset string -HHMM into offset minutes.
var convertOffset = function( offset ){
if( typeof offset !== 'string' ) return;
var hours = parseInt( offset.substr( -4, 2 ), 10 );
var minutes = parseInt( offset.substr( -2, 2 ), 10 );
var negative = ( offset.substr( -5, 1 ) === '-' );
var offset_in_minutes = hours * 60 + minutes;
if( negative ) offset_in_minutes = offset_in_minutes * -1;
return offset_in_minutes;
};
@Fauntleroy
Fauntleroy / (designated) auth.json
Last active December 29, 2015 01:29
resources + auth ideas for solidus
{
"storyteller": {
"headers": {
"Api-Key": "e801cf0a-ee44-42d3-a29f-aff82fd0521b"
}
}
}