Skip to content

Instantly share code, notes, and snippets.

@nikcorg
nikcorg / gist:1875753
Created February 21, 2012 10:48
Solve all your commit message problems with one small alias
alias commit='git commit -a -m \"$(curl -s http://whatthecommit.com|xpath "//p[1]/text()" 2>/dev/null)\"'
@nikcorg
nikcorg / svn-up-preview.sh
Created March 9, 2012 09:50
Preview what will happen when you svn update
#!/bin/bash
WD="."
function extract_revision()
{
REV=$(svn info $1|grep "Revision:"|cut -d ":" -f 2-|sed -e 's/^[ ]*//')
}
function split_op_and_loc()
@nikcorg
nikcorg / gist:2043791
Created March 15, 2012 11:36
Fisher-Yates shuffle in JavaScript
var n = 100,
data = (function (l, r, i) { for (i = 0; i < l; r.push(i++)); return r;}(n, []));
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
function shuffle(arr) {
var i, n = arr.length, p;
for (i = n - 1; i > 0; i--) {
p = Math.floor(Math.random() * i);
arr.splice(i, 1, arr.splice(p, 1, arr[i]).pop());
}
@nikcorg
nikcorg / gist:2043935
Created March 15, 2012 12:19
QuickSort (recursive, wasteful) in JavaScript
var n = 100,
arr = (function (l, r, i) { for (i = 0; i < l; r.push(i++)); return r;}(n, []));
// http://en.wikipedia.org/wiki/Quicksort
function quicksort(arr) {
var pivot,
pi = Math.floor(Math.random() * arr.length), // Random pivot index
below = [],
above = [],
i, l;
@nikcorg
nikcorg / gist:2323429
Created April 6, 2012 22:08
Bresenham's line algorithm
/**
* My take on Bresenham's line algorithm
*
* http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
*
* Based off of the limited algorithm, amended by own research,
* trial and error, and plain stubborness.
*
* Demo: http://jsfiddle.net/VFybR/26/
*/
@nikcorg
nikcorg / lightbox.js
Created June 6, 2012 15:01
Lightbox script
/**
* Lightbox script I made back in the day when I wasn't happy with the ones I could find
* already out there. Also my first exercise using the Module-pattern, copied from jQuery,
* which is the reason for the overtly complex and stupid structure.
*
* Contains bugs and weird solutions. (Some of which I just had to fix, in case someone
* actually sees this code.)
*
* This source code is stored here more as a curiosity than for real world usage.
*
@nikcorg
nikcorg / cheapo_cache.php
Created October 5, 2012 15:00
Cheapo caching for PHP
<?php
// Set elsewhere before including:
//
// $template = contains file to be read when fresh content is served
// CACHE_TTL_SECONDS = how long the cache should live, defaults to 10 mins
//
// Note: this does not necessarily save your server from receiving the GET request,
// you simply don't serve any content over the wire if the caching headers match,
// and you might save some disk I/O.
@nikcorg
nikcorg / js_hint_conf.js
Created October 25, 2012 10:15 — forked from voidfiles/js_hint_conf.js
JSHint conf options
{
// Settings
"passfail" : false, // Stop on first error.
"maxerr" : 10000, // Maximum error before stopping.
// Predefined globals whom JSHint will ignore.
"browser" : true, // Standard browser globals e.g. `window`, `document`.
"node" : false,
@nikcorg
nikcorg / gist:4001207
Created November 2, 2012 12:52
Anonymous state retaining event listener
document.addEventListener(
"mousemove",
function (e) {
var sample = {x: parseInt(e.clientX, 10) || 0, y: parseInt(e.clientY, 10) || 0};
var diff = {x: sample.x - this.lastSample.x, y: sample.y - this.lastSample.y};
this.lastSample = sample;
console.dir(diff);
}.bind({lastSample: {x: 0, y: 0}}),
false
@nikcorg
nikcorg / build-versionjs.sh
Created November 26, 2012 09:40
Cache busting helper
#!/bin/bash
PROJECTHOME=$(dirname $(dirname $0))
JSSOURCEDIR="$PROJECTHOME/public/js"
VERSIONFILE="$JSSOURCEDIR/version.js"
VERSION=$(git describe --all --long | tr "-" " " | cut -d " " -f 3)
if [ ! -d "$PROJECTHOME" -o ! -d "$JSSOURCEDIR" ];
then
echo "Unable to resolve PROJECTHOME or JSSOURCEDIR"
exit 1