Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
#!/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'
@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
@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_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 / 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 / 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 / array_flatten.js
Created February 8, 2016 09:47
These are the three ways to merge a multidimensional array into a single array.
/**
* Given:
* var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]];
*
* Result:
* // [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
// Using concat() and apply()
var myNewArray1 = [].concat.apply([], myArray);
@stefanfrede
stefanfrede / array_deduplicate.js
Created February 8, 2016 09:58
How to remove duplicate elements, of different data types, from an Array.
/**
* Primitives
*/
// If an Array only contains primitive values, we can deduplicate it by only using the filter and indexOf methods.
var deduped = [ 1, 1, 'a', 'a' ].filter(function (el, i, arr) {
return arr.indexOf(el) === i;
});
console.log(deduped); // [ 1, 'a' ]
@stefanfrede
stefanfrede / anki_code_js.txt
Last active January 27, 2018 08:20
Latex snippet for Anki to preview JavaScript code.
// Inline
[latex]\mintinline[bgcolor=black]{js}|...code goes here...|[/latex]
// Multi line
[latex]
\begin{minted}
[
frame=lines,
framesep=2mm,
@stefanfrede
stefanfrede / partial_application_recipes.js
Last active June 27, 2016 05:34
Quickly and simply apply a single argument.
// leftmost
const callFirst = (fn, larg) =>
function (...rest) {
return fn.call(this, larg, ...rest);
}
// rightmost
const callLast = (fn, rarg) =>
function (...rest) {
return fn.call(this, ...rest, rarg);