Skip to content

Instantly share code, notes, and snippets.

View forivall's full-sized avatar
🕴️
levitating

Emily Marigold Klassen forivall

🕴️
levitating
View GitHub Profile
@forivall
forivall / generator_attributes.py
Last active December 19, 2015 06:49
Generator Attributes
import functools
# forked from http://code.activestate.com/recipes/577057-generator-attributes/
# doesn't rely on exec or locals, works in python 2 and 3
def generator_with_attributes(fn):
@functools.wraps(fn)
def fnwrapper(*args, **kwargs):
wrapper = type(
'GeneratorWithAttributes', (object,),
{'__iter__': lambda self: gen})()
gen = fn(wrapper, *args, **kwargs)
@forivall
forivall / pre-commit
Created July 15, 2013 20:31
Don't allow git --all when files are currently staged. ~ An interesting lesson in git's use of environment vars (GIT_INDEX_FILE)
# don't allow --all when we have staged files. It's usually a mistake
# depends on procfs and won't work through gitaliases
while read -d $'\0' arg ; do
if [[ "$arg" == -a* || "$arg" == -[b-zB-Z]*a* || "$arg" == '--all' ]] ; then
if (( $((unset GIT_INDEX_FILE; git diff --cached --name-only)|wc -l) > 0 )) ; then
echo 'using --all while files are cached. don'\''t do that.'
exit 1
fi
fi
done < /proc/$PPID/cmdline
@forivall
forivall / gist:6601639
Created September 17, 2013 22:36
Requirejs bootstrap for userscripts
;(function (body) {
var el = document.createElement('script');
el.textContent = ';(' + body + ')();';
document.body.appendChild(el);
return el;
})(function() {
(function(body) {
if (typeof 'require' !== 'undefined' && require !== null) {
var el = document.createElement('script');
el.addEventListener('load', body);
@forivall
forivall / gist:6772931
Created October 1, 2013 01:57
jQuery.serializeObject
jQuery.fn.serializeObject = function() {
var target = {};
var source = this.serializeArray();
jQuery.each(source, function(i, item) {
if (target[item.name]) {
// multiple values of same name: coerce as array
if (!target[item.name].push) {
target[item.name] = [target[item.name]];
}
target[item.name].push(item.value || '');
@forivall
forivall / signedFloor.js
Created October 3, 2013 17:58
signedFloor
var signedFloor = (function() {
var mfloor = Math.floor, mceil = Math.ceil
return function(n) {
return n > 0 ? mfloor(n) : mceil(n);
};
})();
@forivall
forivall / vcs_info_git_tag_hook.zsh
Last active December 26, 2015 03:59
Show git tag rather than just the abbreviated sha1 for vcs_info
# (( ${+functions[vcs_info_hookadd]} )) || autoload -Uz vcs_info_hookadd
# vcs_info_hookadd set-message git-tag-instead-of-sha1
# vcs_info_hookadd doesn't support dynamic hooks. :(
zstyle ':vcs_info:git*+set-message:*' hooks git-tag-instead-of-sha1
+vi-git-tag-instead-of-sha1() {
if [[ ${hook_com[branch]} =~ ^[0-9a-f]+\.{3}$ ]] ; then
local tag=$(git name-rev --name-only --no-undefined --always HEAD)
if (( $? == 0 )) ; then
hook_com[branch]=$tag
@forivall
forivall / index.html
Last active December 26, 2015 13:39 — forked from max-mapper/index.html
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
/*
Shameless port of a shameless port
@defunkt => @janl => @aq
See http://github.com/defunkt/mustache for more info.
@forivall
forivall / gist:7407357
Created November 11, 2013 03:30
Notes for fixing the annoying '/' = quick find in firefox breaking github's quick find
@forivall
forivall / new.js
Last active December 30, 2015 13:09
The `new` function from "Crockford on JavaScript - Chapter 2" -- https://www.youtube.com/watch?v=RO1Wnu-xKoY&list=PL7664379246A246CB#t=30m12s
// > JavaScript itself was confused about its prototypal nature. Originally it
// > didn't have `Object.create`; instead it had the new operator which modified
// > the way invocation worked. If you were to express the new operator as a new
// > function, this is what it actually does: it calls Object.create on the
// > prototype property of your function, makes a new object that inherits from
// > that, and then it calls your function, binding the new object to that and
// > passing along whatever arguments you gave it. Then it takes the result, and
// > if the result is an object then it returns that object, and if it's not an
// > object, then it returns the object that you made. It was trying to look
// > classical so that the Java kids would loook at it and go yeah, OK, this
@forivall
forivall / simpleCoffeescriptNaturalSort
Created March 5, 2014 19:59
Simple (non-robust) Natural Sort in coffeescript
natcompare_re = /^(\D*)(\d*)([\s\S]*)$/
naturalCompare = (a, b) ->
return 0 if a is b
[__, a_word, a_num, a_rest] = natcompare_re.exec(a)
[__, b_word, b_num, b_rest] = natcompare_re.exec(b)
return (if a < b then -1 else 1) if a_word isnt b_word
return (+a_num) - (+b_num) if a_num isnt b_num
return naturalCompare a_rest, b_rest