Skip to content

Instantly share code, notes, and snippets.

View greim's full-sized avatar

Greg Reimer greim

View GitHub Profile
@greim
greim / tree.js
Created May 17, 2011 06:08
Nice ascii-art DOM trees
/**
tree.js - nice ascii-art DOM trees
usage: tree(element[, options]);
example:
var s = tree(document.body,{
serialize: function(element){...}, // return a string representation such as "div#foo.bar"
filter: function(element){...} // return false for any nodes to not include in tree
});
console.log(s);
*/
@greim
greim / soft-model.js
Created April 3, 2012 19:09
Backbone.SoftModel
/**
Extend Backbone.SoftModel and then bind to the 'soft:change' event.
It will only fire once given a sequence of rapid-fire changes.
This avoids for example having multiple redundant renders,
which can be a performance killer. That way you don't have to
splatter your code with speculative {silent:true}s.
Warning!
This code is untested. This is just exploring an idea.
*/
// created by greg reimer ("gregreimer" at gmail) http://obadger.com/
function createHistogram(rFunc) {
var arr = [];
var size = 30;
for (var i=0; i<size; i++){
arr[i] = 0;
}
for (var i=0; i<1000000; i++){
arr[Math.floor(rFunc() * size)]++;
@greim
greim / gist:8221971
Created January 2, 2014 16:36
I'm building a decorative wooden header for some shelves with arches cut out at the top. The board was 5" wide and I wanted the arch to be half that, so I wrote this program to calculate the radius.
/*
For an arch with a rise of X, what's the radius.
Start with this right triangle:
b
|
a----------------c
a = left edge of arch
@greim
greim / gist:2dcedcf3c42ad6b5258f
Created February 9, 2015 16:36
Binary walk function
/*
* Home in on a number in binary search fashion.
* binWalk(2048) === 1024
* binWalk(2048, -1) === 512
* binWalk(2048, -1, -1) === 256
* binWalk(2048, -1, -1, -1) === 128
* binWalk(2048, -1, 1) === 1536
*/
function binWalk(n){
@greim
greim / asynchronous-quick-sort.js
Created June 10, 2015 06:04
Implementing async quicksort with generators
'use strict'
/*
* QuickSort algorithm adapted from here:
* http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
*/
var co = require('co')
function swap(items, firstIndex, secondIndex){
@greim
greim / https-mitm-proxy-poc.js
Last active September 18, 2022 06:44
HTTPS MITM Proxy Proof of Concept
import https from 'https'
import http from 'http'
import url from 'url'
import adapt from 'ugly-adapter' // callback => promise adapter; need to npm install this
import tls from 'tls'
import net from 'net'
import fs from 'fs'
import os from 'os'
import path from 'path'
import childProcess from 'child_process'
@greim
greim / reverse-proxy.md
Last active August 23, 2022 10:12
Using a Reverse Proxy for Rapid Prototyping

Using a Reverse Proxy for Rapid Prototyping

Note: This will be a contrived example, but hopefully illustrates some real-world trade-offs.

Example scenario: Suppose you're an independent web developer, and a client asks you to prototype a redesign of their website header. You'll be paid for your time, and if the client likes it, you'll be hired to do the full implementation. Your incentive is to show the client a quick, functional demo of the updated header. The problem is that quick and functional tend to be mutually-exclusive.

At One Extreme: Do It Fast

How you can help reduce node_modules bloat

This recent reddit thread reveals discontent among the web development community about the sheer volume of stuff in a typical node_modules dir. 140MB in this case!

Is it a design flaw in npm?

Opinions in the thread varied from "I'm surprised npm even works" to "everything is fine". I'm not going to offer an opinion, just these two observations:

  1. node_modules dirs typically do contain lots of stuff that doesn't need to be there.
  2. The latest version mitigates overall size by flattening the dependency tree, but some of the bloat is beyond npm's control.

Private members in ES6 classes

"Let's create an ES6 class!" you say. "Let's give it a private variable x."

class Foo {
  constructor(x) {
    this.x = x;
  }
  getX() {