Skip to content

Instantly share code, notes, and snippets.

View johnloy's full-sized avatar

John Loy johnloy

View GitHub Profile
function get_avatar_from_service(service, userid, size) {
// this return the url that redirects to the according user image/avatar/profile picture
// implemented services: google profiles, facebook, gravatar, twitter, tumblr, default fallback
// for google use get_avatar_from_service('google', profile-name or user-id , size-in-px )
// for facebook use get_avatar_from_service('facebook', vanity url or user-id , size-in-px or size-as-word )
// for gravatar use get_avatar_from_service('gravatar', md5 hash email@adress, size-in-px )
// for twitter use get_avatar_from_service('twitter', username, size-in-px or size-as-word )
// for tumblr use get_avatar_from_service('tumblr', blog-url, size-in-px )
// everything else will go to the fallback
// google and gravatar scale the avatar to any site, others will guided to the next best version
@johnloy
johnloy / python-simple-http-server.sh
Last active August 27, 2021 14:36
Start a quick http server from any directory
python -m SimpleHTTPServer
@johnloy
johnloy / is-git-branch-clean.sh
Last active August 27, 2021 19:32
Check whether a git branch is clean
git_is_dirty() {
if [[ $(git status --porcelain 2> /dev/null | tail -n1) != "" ]]
then
echo 'ERROR: branch dirty'
exit 1
fi
}
var pubsub = {};
(function (q) {
var topics = {},
subUid = -1;
q.subscribe = function (topic, func) {
if (!topics[topic]) {
topics[topic] = [];
}
var token = (++subUid).toString();
topics[topic].push({
@johnloy
johnloy / README.md
Created August 23, 2016 12:59
Reusable parsing algorithm using pure functions

There's no need to fully parse the xml of entire clearleap api responses to JSON. And there doesn't seem to be a compelling reason to use xml2json, unless api responses need to be rendered on the back end in an isomorphic app. Usually, we just want to pluck certain values out, massage them a bit, and add them as properties of an object/structure better suited to presentation.

At its simplest, this can be done with a function that takes an instance of Node, performs DOM querying logic to retrieve and aggregate values from elements and attributes, and then returns a single value.

This gist shows a way to reuse and compose pure functions of this variety to converge parsing operations for invidual values into a final object. Because only pure functions are involved, presumably parsing could be parallelized with web workers (https://adambom.github.io/parallel.js/). Each pure function involved is also automatically memoized and passed wi

@johnloy
johnloy / set-vim-manpager.sh
Last active August 27, 2021 19:46
set vim as MANPAGER
# From http://vim.wikia.com/wiki/Using_vim_as_a_man-page_viewer_under_Unix
export MANPAGER='col -bx | vim -c ":set ft=man nonu nolist" -R -'
@johnloy
johnloy / open-man-page-in-browser.sh
Last active August 27, 2021 19:46
Open a man page as html in default browser
groff -man -T html $(man -w man) > $TMPDIR/man.html | open -f -a 'Google Chrome' $TMPDIR/man.html
@johnloy
johnloy / jump-to-section-of-man-page.sh
Last active August 27, 2021 19:46
jump to a section of a man page
man -P 'less -p ^READLINE' bash
@johnloy
johnloy / timeoutify-a-callback.js
Last active August 27, 2021 18:54
Timeoutify a callback. From "You Don't Know JS: Async and Performance"
function timeoutify(fn,delay) {
var intv = setTimeout( function(){
intv = null;
fn( new Error( "Timeout!" ) );
}, delay )
;
return function() {
// timeout hasn't happened yet?
if (intv) {