Skip to content

Instantly share code, notes, and snippets.

View andrewluetgers's full-sized avatar

Github Notification andrewluetgers

View GitHub Profile
@andrewluetgers
andrewluetgers / ioUtils.js
Created October 12, 2012 04:44
update things in node when files change
var request = require('request');
var fs = require('fs');
// file watches seem fuxed so I have some hacky workarounds in here
/**
* @param url (string) url to get like: "http://www.google.com"
* @param path (string) path to file like: "/tmp/test/myfile.js"
* @param callback(err, stuff)
*/
@andrewluetgers
andrewluetgers / callback-organization-redux.js
Created August 17, 2013 17:17
In response to http://andrewkelley.me/post/js-callback-organization.html I believe that the convention of next(err, val) is that if err is truthy it will get handled if not then you expect data in the second arg. I often see code that adds extra checks for if (err != null) but is not necessary, you can just pass in the error with your data if th…
var async = require('async');
function getUserFriends(userName, next) {
db.users.findOne({name:userName}, foundUser);
function foundUser(err, user) {
err ? next(err) : getFriendsById(user.id, gotFriends);
function gotFriends(err, friends) {
if (err || user.type != 'power user') {
@andrewluetgers
andrewluetgers / seal.js
Last active August 29, 2015 13:58
seal and sealh: prototype chain used to create a projection of current object as original + changes, sealh stores a history of all changes from call to call
// prototype chain used to create a projection of current object as original + changes
// the return object is always an instance of the provided object, changes form a change-set
// todo deal with deeply nested prototype chains
function seal(d) {
var x = _seal(d);
if (_.isObject(d)) {
_.each(d, function(v, k) {
if (d.hasOwnProperty(k) && _.isObject(v)) {
x[k] = seal(v);
}
@andrewluetgers
andrewluetgers / gist:9bf09e447993e7786ce5
Last active August 29, 2015 14:13
DynaCache: Speed up angular $http requests with Localforage response cache
angular.module("dynacache", [])
.service("dynacache", function($http) {
// drop in replacement for $http
// currently supports "get", "post", "put", "jsonp" methods
// caches response body in localforage (http://mozilla.github.io/localForage/)
// subsequent requests immediately serve up local data
// whilst fetching new data from the server
// once ajax returns, new data replaces old by firing success handler once again
// expects lodash _ global to be present
function jaccardIndex(s1, s2) {
// Jaccard index: http://en.wikipedia.org/wiki/Jaccard_index
// size of the intersection divided by the size of the union of the sample sets
s1 = _.isArray(s1) ? s1 : _.keys(s1);
s2 = _.isArray(s2) ? s2 : _.keys(s2);
return _.intersection(s1, s2).length / _.union(s1, s2).length
}
@andrewluetgers
andrewluetgers / gulptfile.js
Created January 29, 2015 06:38
Gulp build for riot.js with sourcemaps
var es = require('event-stream'),
gulp = require('gulp'),
riot = require('gulp-riot'),
rimraf = require('gulp-rimraf'),
jshint = require('gulp-jshint'),
stylus = require('gulp-stylus'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
notify = require("gulp-notify"),
plumber = require('gulp-plumber'),
@andrewluetgers
andrewluetgers / gist:927fb8a8651d7a713365
Created February 5, 2015 16:38
Webpack + React + Stylus, Dev: Hotloat, Build: File Output
var webpack = require('webpack'),
path = require('path'),
ExtractTextPlugin = require("extract-text-webpack-plugin"),
isDev = process.env.NODE_ENV;
var config = {
cache: true,
resolve: {
extensions: ["", ".js", ".css", ".styl"]
@andrewluetgers
andrewluetgers / forceUpdateOnState.js
Last active August 29, 2015 14:17
Omniscient / Immstruct - forceUpdateOnState
/**
* forceUpdateOnState
*
* @param state immstruct structure
* @returns component mixins {componentWillMount, componentWillUpdate, componentWillUnmount}
*
* the parent component should provide to the target component
* a map of prop names as keys for cursors and period delimited path strings
* on as 'cursors' property, e.g. <div cursors={{foo: 'bar.foo'}}></div>
* in the above example: props.foo = state.reference(['bar', 'foo']).cursor()
@andrewluetgers
andrewluetgers / Map.js
Last active August 29, 2015 14:18
Map data structure with handy deepGet and deepSet methods https://jsfiddle.net/aq93v4g7/7/
function Map(keys, values) {return new _Map(keys, values)}
function _Map(keys, values) {this.clear(keys, values);}
_Map.prototype = {
set: function(key, value) {
var i = this.indexOf(key);
this._keys[i] = key;
this._values[i] = value;
return i !== this._keys.length;
},
@andrewluetgers
andrewluetgers / normalizer.js
Last active March 1, 2016 23:22
Convert a number in domain A to domain B
// a normalize function constructor for mapping from one range of numbers to another
// actually it is 4 ranges, one from mid to max another from min to mid for each domain
// so given 3 input (domainA) numbers for min, mid, max
// and given 3 output (domainB) numbers or arrays of any length of numbers also for min, mid, max
// the constructor function will return a normalize function that wil convert any number in domainA to domainB
//
// example:
// converting a value from -1 to 1 to a color ramp red to green
// with a white neutral in the middle and alpha evenly interpolated
//