Skip to content

Instantly share code, notes, and snippets.

View willread's full-sized avatar

William Read willread

View GitHub Profile
@willread
willread / add1.js
Created August 2, 2012 21:34
Array iteration problem
/*
Solution to the Array Iteration Problem @ http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html
---
add1 - increments items in an array matching specified value
param: arr - array of integers to manipulate
param: val - integer, value to increment
@willread
willread / classNameManipulation.js
Created August 3, 2012 04:40
Utility methods for adding, removing, checking for and toggling classes in classNames.
var hasClass = function(el, c){
return new RegExp("(\\s|^)" + c + "(\\s|$)", "g").test(el.className);
}
var addClass = function(el, c){
if(!this.hasClass(el, c)) el.className += " " + c;
}
var removeClass = function(el, c){
el.className = el.className.replace(new RegExp("(\\s|^)" + c + "(\\s|$)", "g"), " ").trim();
@willread
willread / convertFont.sh
Created August 20, 2012 19:10
Convert SVG Font To TTF Using cURL
curl -X POST -F outputFormat=ttf -F fontFile=@./icons.svg www.freefontconverter.com > icons.ttf
@willread
willread / padNumber.js
Created August 21, 2012 15:29
Pad a number with leading zeros to a specified length
var padNumber = function(n, d){
if(!d || d <= (n+"").length)
return n+"";
return (0).toPrecision(d - (n+"").length).replace(".", "")+n;
}
padNumber(1.234, 6); // Returns "000001.234"
padNumber(1.234, 10); // Returns "01.234"
@willread
willread / minimalAjax.js
Created August 21, 2012 20:20
Minimal cross browser AJAX
var minimalAjax = function(url, callback){
var xmlHTTP = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MicrosoftXMLHTTP");
xmlHTTP.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200)
callback(this.responseText);
}
xmlHTTP.open("GET", url, true);
xmlHTTP.send();
@willread
willread / reverseWords.js
Created August 24, 2012 01:57
Reverse the words in a string
// Reverse words in a string
var reverseWords = function(sentence){
var words = sentence.split(" ").reverse(); // Split the sentence into an array of words and reverse it
var string = "";
for(word in words)
string += (word > 0 ? " " : "") + words[word]; // Concatenate each word to the output and add spaces where required
return string;
@willread
willread / gist:3875540
Created October 11, 2012 21:16
Remotely add a vhost to a couchdb instance
curl -XPUT -HContent-Type:application/json https://user:password@host.com/_config/vhosts/subdomain.host.com -d '"/database/_design/app/_rewrite"'
@willread
willread / gist:3876557
Created October 12, 2012 00:13
String representation of an array
'"' + words.join('","') + '"'
@willread
willread / gist:4128941
Created November 22, 2012 01:36
Serve the current directory via SimpleHTTPServer in OSX
python -m SimpleHTTPServer
@willread
willread / gist:4133768
Created November 23, 2012 02:30
Count the occurences of a phrase in a file
grep -oh something ./some.file | wc -w