Skip to content

Instantly share code, notes, and snippets.

View PatrickJS's full-sized avatar

PatrickJS PatrickJS

View GitHub Profile
@PatrickJS
PatrickJS / getElementsByClassName.js
Created April 18, 2013 20:31
getElementsByClassName
var getElementsByClassName = function(className) {
var element = arguments[1] || document.body,
results = [],
has_class_name = [].indexOf.call(element.classList, className) !== -1,
children_results = [].map.call(element.children, function(element) {
return getElementsByClassName(className,element)[0];
}).filter(Boolean);
if (has_class_name) {
results.push(element);
@PatrickJS
PatrickJS / Text Editor Browser
Created April 23, 2013 03:27
single line of code to turn your browser into a text editor
data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
@PatrickJS
PatrickJS / two_invocations.js
Last active September 19, 2023 07:00
Write a function that adds from two invocations in JavaScript
function addf(x) {
return function(y) {
return x + y;
}
}
addf(3)(4)
-------------------------
@PatrickJS
PatrickJS / binary _function_with_two_invocations.js
Created April 25, 2013 19:59
Write a function that takes a binary function, and makes it callable with two invocations
function applyf(binary) {
return function(x) {
return function(y) {
return binary(x, y);
};
};
}
addf = applyf(add);
addf(3)(4) //=> 7
@PatrickJS
PatrickJS / .bash_profile
Last active December 16, 2015 18:09
Bash Profile
function parse_git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "git:("${ref#refs/heads/}$(num_git_commits_ahead)")"
}
function num_git_commits_ahead {
num=$(git status 2> /dev/null \
| grep "Your branch is ahead of" \
| awk '{split($0,a," "); print a[9];}' 2> /dev/null) || return
if [[ "$num" != "" ]]; then
@PatrickJS
PatrickJS / Arrays.prototype.last.js
Created April 29, 2013 04:21
add .last() to all Arrays
Array.prototype.last = function(n) {
if (n === undefined) {
return this[this.length-1];
}
var answers = [];
for(var i = this.length-1; i>this.length-n-1;i-=1){
answers.unshift(this[i]);
}
return answers;
}
@PatrickJS
PatrickJS / String.prototype.reverse.js
Created April 29, 2013 04:28
add .reverse() to Strings
String.prototype.reverse = function() {
return this.split("").reverse().join("")
}
@PatrickJS
PatrickJS / fall_back_CDN_to_local.html
Last active December 16, 2015 21:29
Snippet: CDNs fail, but your scripts don't have to - fallback from CDN to local jQuery
<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js"></script>
<script>window.jQuery || document.write('<script type="text/javascript" src="scripts/jquery-ui-1.8.24.js"><\/script'')</script>
@PatrickJS
PatrickJS / FizzBuzz.js
Created May 2, 2013 20:51
Super-optimised version of FizzBuzz
// Super-optimised version of FizzBuzz
var array = [];
array[3] = array[6] = array[9] = array[12] = "Fizz<br>";
array[5] = array[10] = "Buzz<br>";
array[0] = "FizzBuzz<br>";
var str = "";
for( var i=1; i<=100; ++i) {
str += array[ i%15 ] || i + "<br>";
}
@PatrickJS
PatrickJS / append_two_arrays.js
Created May 21, 2013 07:17
Append an array to another array
Append an array to another array
var a = [4,5,6];
var b = [7,8,9];
[].push.apply(a, b);
uneval(a); // is: [4, 5, 6, 7, 8, 9]