Skip to content

Instantly share code, notes, and snippets.

View countable's full-sized avatar

Clark Van Oyen countable

View GitHub Profile
@countable
countable / quick-push-pull.bash
Created June 27, 2014 18:38
Quick git push and pull of current branch.
# Does "git push origin <current branch>"
function push {
git push origin $(git branch | grep "\*" | sed "s/\*\s//g")
}
# Does "git pull origin <current branch>"
function pull {
git pull origin $(git branch | grep "\*" | sed "s/\*\s//g")
}
@countable
countable / format-date.coffee
Last active December 23, 2015 07:39
Fast and concise date formatting routine, using python style format strings.
zpad = (s='', n=0)->
s += ''
while s.length < n
s = '0' + s
s
#Javascript date formatting. Supports a subset of Python's formats for datetime.strptime
#http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
months = 'January,February,March,April,May,June,July,August,September,October,November,December'.split(',')
months_abbr = (m.substr(0,3) for m in months)
@countable
countable / importAllFrom.coffee
Last active December 22, 2015 14:49
Python style "import" for coffeescript (this does not load resources, it only manipulates namespaces)
###
Python style "import" for coffeescript (this does not load resources, it only manipulates namespaces):
# Where you would do this:
from module.package import *
p = PackageClass()
# Do this, where module is the already global name, such as APP.MODULE
eval importAllFrom 'module.package'
p = PackageClass()
@countable
countable / zpad.coffee
Last active December 22, 2015 04:39
zpad(s, n) pads a string <s> with zeros in order to make it at least length <n>
# zpad(s, n) pads a string <s> with zeros in order to make it at least length <n>
zpad = (s='', n=0)->
s += '' # cast to string
while s.length < n
s = '0' + s
s
@countable
countable / serializeObject.jquery.coffee
Created August 12, 2013 22:58
Convert HTML form data to JSON.
$.fn.serializeObject = ->
o = {}
a = @serializeArray()
$.each a, ->
if o[@name]
o[@name] = [o[@name]] unless o[@name].push
o[@name].push @value or ""
else
o[@name] = @value or ""
@countable
countable / beget.coffee
Last active December 20, 2015 21:39
Crockford Object.beget with some extensions. Pass extra attributes to bind directly to the object. The optional `init` attribute is used as a constructor.
# Object.beget - returns a new object based on a prototype and extra attributes
# @param {object} o - the object to use as a prototype
# @param {object} moar - extra attributes to apply to the object.
# @param {function} o.init - optional constructor function.
Object.beget = (o, moar = {})->
F = moar.init ? new Function() # now F can be used as a constructor.
F.prototype = o # All new objects F creates will be based on o.
child = new F()
for own k,v of moar
child[k] = v
@countable
countable / fix-invalid-csv.py
Last active October 14, 2019 14:15
This script attempts to repair invalid or corrupt CSV that fails to be parsed by RFC 4180 compliant parsers including node-csv and the python csv parser. It will attempt to fix CSV files that break rule 5 of RFC 4180, by quoting substrings of items between delimiters. Usage: python fix-csv.py <filename>
import sys
import re
def num_endings(istr, ending):
n = 0
for i in reversed(istr):
if i == ending:
n += 1
else:
@countable
countable / facebook-like.html
Created July 17, 2013 01:15
Facebook Like Button
<!--Place the code for your plugin wherever you want the plugin to appear on your page. -->
<div class="fb-like" data-href="http://developers.facebook.com/docs/reference/plugins/like" data-send="true" data-width="450" data-show-faces="true"></div>
<!-- Include the JavaScript SDK on your page once, ideally right after the opening <body> tag. -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
@countable
countable / twitter-share.html
Created July 17, 2013 01:12
Twitter Social Share
<!-- Place this tag where you want the share button to render. -->
<a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
<!-- Place this tag after the last share tag -->
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
@countable
countable / format.coffee
Created June 19, 2013 18:43
string.format in coffeescript
# string.format
# Simply map named variables in a string to attributes of an object.
# ie)
#
# ''' I am a {fruit} '''.format {fruit: 'banana'}
# >>> I am a banana.
String::format = (str, dict)->
str.replace /\{([^\}]+)\}/g, (match, $1)->
dict[$1] or ''