Skip to content

Instantly share code, notes, and snippets.

@nickjacob
nickjacob / safe_eval.js
Created November 13, 2013 00:45
an idea for a safe eval function
function safe_eval(code, ctx) {
return (new Function (["window", "undefined"], "try { " + code + " } catch (e){ return e; }"))(ctx);
}
function restrict (base, perm) {
var out = {}, k = null,
READ = 'read',
WRITE = 'write',
RW = 'readwrite';
@nickjacob
nickjacob / mini_tpl.js
Last active December 26, 2015 04:29
Newer version of my mini templating
;(function (window, document, undefined) {
var m = window.micro = {},
whiteSpaceRxp = /[\r\n\t\s]/g,
quotesRxp = /['"]/g,
attrRxp = /<%=\s*([\w_\$\.]+)\s*%>/ig;
// Convert template to use consistent quotes, remove unnecessary whitespace
// returns: the normalized template string
function normalizeTemplate(tpl) {
return tpl.replace(whiteSpaceRxp, ' ').replace(quotesRxp, "'");
@nickjacob
nickjacob / is_a.js
Created July 1, 2013 23:01
rly simple self-defining test function snippet
/* Just define a simple is_a fn and populate it with functions
* that correspond to the primary JS objects; interface is
*
* is_a.array([]) >> true
*/
;(function (window, undefined){
var upFirst = /^(\w)/,
toS = Object.prototype.toString,
builtIns = ['Array', 'Object', 'Number', 'String', 'Window', 'RegExp', 'Function'];
@nickjacob
nickjacob / framekiller.js
Created June 25, 2013 17:04
really basic framekiller
;(function (window) {
if (window.top !== window.self) {
killFrame();
}
var _frame_ = "<html><head><title>Not Allowed</title>\
</head><body><h1>We don't allow This</h1></body></html>";
function killFrame() {
document.open();
@nickjacob
nickjacob / anagram.js
Created June 24, 2013 23:27
just simple how 2 find anagrams
// space complexity: O(n) -- the hashmap
// time complexity: ~O(2n); iterate both strings once
// worse case: O(2n)
function areAnagrams(a, b) {
var len = a.length, chars = {}, i = 0;
if (len !== b.length) return false;
for(; i < len; i++) {
chars[a[i].toLowerCase()] = true;
}
@nickjacob
nickjacob / parallel.sh
Last active April 11, 2022 22:06
bash parallelization template
#!/bin/bash
# Parallelize the execution of a command
######
CHUNK_COUNT=0
parallel_configure () {
if [ $# -eq 0 ]; then
echo "usage: <tmp dir> <tmp prefix> <num chunks>"
fi
@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
@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 / µ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 / 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