Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
@stefanfrede
stefanfrede / function_convert_arguments_to_array.js
Last active June 7, 2016 05:08
Convert arguments, an array-like object, into an array.
/**
* Common practice to convert arguments into an array:
*/
var args = Array.prototype.slice.call(arguments);
/* or the shorthand */
var args = [].slice.call(arguments);
@stefanfrede
stefanfrede / convert_truthy_falsy.js
Created February 1, 2016 06:34
Convert a truthy or falsy value to true boolean with the !! operator.
!!"" // false
!!0 // false
!!null // false
!!undefined // false
!!NaN // false
!!"hello" // true
!!1 // true
!!{} // true
!![] // true
@stefanfrede
stefanfrede / array_shuffle.js
Created January 30, 2016 08:50
This snippet here uses Fisher-Yates Shuffling Algorithm (https://www.wikiwand.com/en/Fisher–Yates_shuffle) to shuffle a given array.
/**
* Example:
* var a = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
* var b = shuffleArray( a );
* console.log(b);
* // [2, 7, 8, 6, 5, 3, 1, 4]
*/
function shuffleArray( arr ) {
var i,
@stefanfrede
stefanfrede / array_empty.js
Created January 30, 2016 08:47
The most performant way to empty an array.
/**
* Example:
* var foo = [ 1,2,3 ];
* emptyArray( foo );
* console.log( foo );
* // []
*/
function emptyArray( arr ) {
//empty the array
@stefanfrede
stefanfrede / array_filter_and_sort.js
Created January 30, 2016 08:44
Filter an array of strings to remove duplicates and sort it alphabetically.
var keywords = ['do', 'if', 'in', 'for', 'new', 'try', 'var', 'case', 'else', 'enum', 'null', 'this', 'true', 'void', 'with', 'break', 'catch', 'class', 'const', 'false', 'super', 'throw', 'while', 'delete', 'export', 'import', 'return', 'switch', 'typeof', 'default', 'extends', 'finally', 'continue', 'debugger', 'function', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'var', 'byte', 'case', 'char', 'else', 'enum', 'goto', 'long', 'null', 'this', 'true', 'void', 'with', 'break', 'catch', 'class', 'const', 'false', 'final', 'float', 'short', 'super', 'throw', 'while', 'delete', 'double', 'export', 'import', 'native', 'public', 'return', 'static', 'switch', 'throws', 'typeof', 'boolean', 'default', 'extends', 'finally', 'package', 'private', 'abstract', 'continue', 'debugger', 'function', 'volatile', 'interface', 'protected', 'transient', 'implements', 'instanceof', 'synchronized', 'do', 'if', 'in', 'for', 'let', 'new', 'try', 'var', 'case', 'else', 'enum', 'eval', 'null', 'this', 'true', 'void', 'with', 'brea
#!/bin/sh
# Alot of these configs have been taken from the various places
# on the web, most from here
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
# Set the colours you can use
black='\033[0;30m'
white='\033[0;37m'
red='\033[0;31m'