Skip to content

Instantly share code, notes, and snippets.

@wrboyce
wrboyce / gist:786460
Created January 19, 2011 17:12
pre-commit hook to automatically minify javascript/css
#!/usr/bin/zsh
COMPRESSOR=$(whence -p yui-compressor)
[ -z $COMPRESSOR ] && exit 0;
function _compress {
local fname=$1:t
local dest_path=$1:h
local min_fname="$dest_path/${fname:r}.min.${fname:e}"
$COMPRESSOR $1 > $min_fname
@Gozala
Gozala / example.js
Created January 29, 2012 03:46
Workaround for lack of "tail call optimization" in JS
// Lack of tail call optimization in JS
var sum = function(x, y) {
return y > 0 ? sum(x + 1, y - 1) :
y < 0 ? sum(x - 1, y + 1) :
x
}
sum(20, 100000) // => RangeError: Maximum call stack size exceeded
// Using workaround
@jonathanmoore
jonathanmoore / gist:2640302
Created May 8, 2012 23:17
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@getify
getify / test.js
Created August 16, 2012 21:25
object JSON serialization that's circular-ref safe
// all this `toJSON()` does is filter out any circular refs. all other values/refs,
// it passes through untouched, so it should be totally safe. see the test examples.
// only extend the prototype if `toJSON` isn't yet defined
if (!Object.prototype.toJSON) {
Object.prototype.toJSON = function() {
function findCircularRef(obj) {
for (var i=0; i<refs.length; i++) {
if (refs[i] === obj) return true;
}
@hapticdata
hapticdata / bubbled-console.js
Created September 6, 2012 02:32
bubble console methods from nested iframes into root window.console
//##console.log bubble for nested iframes
//##by [Kyle Phillips](http://haptic-data.com)
//find each iframe recursively and bubble its logs up to the current window.console
(function( d, w ){
function bubble( doc, win, msg ){
Array.prototype.forEach.call(doc.getElementsByTagName('iframe'), function( frame ){
if( !sameOrigin(frame.src) || !frame.contentWindow || !frame.contentWindow.console ){
w.console.log('console of iframe ', frame.src, ' is unreachable');
return;
}
@naholyr
naholyr / git-pr.md
Created November 22, 2012 18:11
Using "hub" to automatically push & PR

git-pr

Part of my personal shortcuts for the usual pull-request-based workflow.

Setup

Given you have an "upstream" repository you need to work on:

  1. Fork it
  2. Clone your fork locally (repo "origin")
anonymous
anonymous / dict-ie.js
Created December 28, 2012 07:12
old IE shim for creating empty dict objects
// pre-ES5 IE version
var dict = (function() {
var PROPERTY_KEYS = [
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable"
"valueOf",
"toString",
"toLocaleString",
"constructor"
@brucelawson
brucelawson / native over web reasons
Last active December 15, 2015 23:09
Native app developers: what are your reasons for preferring to develop native over Web apps?
[My initial list:]
DRM
speed for superhi-perf games
app store placement
source-code secrecy
[list compiled from other people's twitter answers; thanks all!]
pushing native notifications when they're not in the "app" (several people said this)
@naholyr
naholyr / 1-useless-stack.js
Last active June 8, 2021 08:29
Make stack traces useful again
var fs = require('fs');
// Here is a simple function that reads a file and makes something with its content
function readLog1 (cb) {
// Notice how I even gave a name to my callback, this is useful for stack traces, everyone says…
fs.readFile('file-not-found.log', function onRead1 (err, content) {
// Usual error handling
if (err) return cb(err);
@jlongster
jlongster / es6-destructure.js
Last active December 29, 2015 01:49
ES6 destructuring (most of it) as macros
// ES6 destructuring as sweet.js macros. See bottom for examples.
// TODO:
// elision: var [,,,four] = arr;
// rest: var [foo, bar, ...rst] = arr;
// function args: function(foo, bar, { baz, poop }) {}
macro destruct__objassign {
rule { $declare ($prop:ident : $pattern:expr = $default:expr) $obj:expr } => {
next $declare ($obj.$prop || $default) $pattern