Skip to content

Instantly share code, notes, and snippets.

@nickjacob
nickjacob / _url.js
Created November 1, 2012 16:05
url parsing "plugin" in response to this: https://github.com/websanova/js-url
/**
* URL Parsing "Library"/function
* https://github.com/websanova/js-url <-- this is unnecessarily complex
*/
;(function _url(window,undefined){
function Url(url){
this.elem = window.document.createElement('a');
this.elem.href = url || window.location;
@nickjacob
nickjacob / switchy.js
Created November 6, 2012 07:50
Javascript is the perfect language for dispatch tables
/* Why would you ever need a switch statement in javascript? */
var switchy = {
1: function(x){ return x*x; },
pizza: function(x){ return x*x*X }
};
var bar = switchy[foo](10);
// what if you could extend it with regex?
@nickjacob
nickjacob / api_wrapper.rb
Last active December 11, 2015 04:28
I know this has been done before, but here's my metaprogramming solution to wrapping a RESTful API.
###
# API Client for "MyApi"
###
require 'httparty'
require 'yaml'
module MyApi
class Config
attr_accessor :base_url, :page_size, :key
@nickjacob
nickjacob / picotemplate.js
Last active December 11, 2015 17:58
logic-less, compiled client-side templates in 5 lines of javascript
/**
* PicoTemplate
* syntax (there is barely any)
*
* <a href="{{ url }}">{{ movie.title }}</a>
* NO LOGIC!
**/
function __deep(obj, prop) {
var parts = prop.split('.'), i = -1, l = parts.length;
while(++i < l) {
@nickjacob
nickjacob / multipleOpen.py
Created February 14, 2013 20:43
open multiple files in with statement
from contextlib import contextmanager
@contextmanager
def files(*ff):
fi = [ open(*f) if len(f) == 2 else open(f) for f in ff ]
yield(fi)
map(close,fi)
javascript:(function(){
var body = window.document.body,
hh = body.scrollTop,
interval = null,
style_div = "font-family:helvetica;border-radius:5px;border:1px solid #222;border-bottom: 2px solid #111;padding:10px;position:fixed;top:20px;left:20px;background:#555",
style_link = "padding: 5px;background:#222;color:#eee;text-decoration:none;margin:5px;",
tpl = "<div id='stoppy' style='" + style_div + "'><a style='" + style_link + "' href='#' id='start-btn'>start</a> \
<a style='" + style_link + "' href='#' id='stop-btn'>stop</a>";
@nickjacob
nickjacob / persist_url.coffee
Last active December 14, 2015 06:39
just an idea I wanted to play with; for a searching app, shouldn't the user be able to refresh and get the same parameters in the field/query?
inputRe = /input|select|textarea/i # some browsers do uppercase tagNames; also faster than 3 strcmp
cache = {}
elByName = (el) ->
cache[el.name] ?= document.getElementsByName el.name
document.addEventListener 'load', (e) ->
document.addEventListener 'change', (e) ->
target = e.target
@nickjacob
nickjacob / µTpl.coffee
Last active December 14, 2015 06:39
impl of my picotemplate in coffeescript to see what it'd be like
__deep = (obj, prop) ->
for p in prop.split '.'
if not (p in obj and obj = obj[p])
return undefined
obj() if typeof obj is 'function' else obj
getEscape = (tpl) ->
qq = "\'" if '"' in tpl else "\""
(str) -> qq + str + qq
@nickjacob
nickjacob / script.rb
Created March 25, 2013 21:41
I've been writing a lot of ruby scripts that I want to make more usable/modular, realized I need a common template/api for these things
# Script Template
# @author nickjacob
require 'pp'
def print_json json
pp json
end
def perform argv
while line = gets.chomp
@nickjacob
nickjacob / appending.js
Last active December 16, 2015 12:59
overview of different ways to append to the DOM
// inefficient because you call append multiple times
// this causes a reflow on each call (https://developer.mozilla.org/en-US/docs/Notes_on_HTML_Reflow)
$('#my-el').append('<div>hi</div>').append('<div>two</div>').append('<div>three</div>');
// you could just make 1 string of HTML
var to_append = '<div>hi</div><div>two</div><div>three</div>';
$('#my-el').append(to_append);
// or you can use documentFragment;
// documentFragment is a container for DOM elements that, when you eventually attach it to the DOM, disappears so