Skip to content

Instantly share code, notes, and snippets.

View trevor-atlas's full-sized avatar
👾
Variety is the spice of life, or something

Trevor Atlas trevor-atlas

👾
Variety is the spice of life, or something
View GitHub Profile
@trevor-atlas
trevor-atlas / emoji-banner-slack.sh
Created August 24, 2018 19:53
banner 'yes' 'thumbsup'
banner() {
figlet -f banner "$1" | sed -e "s/#/:${2}:/g" | sed -e 's/ /:blank:/g' | pbcopy
}
const suffixNumber = (n) => {
// gives us the last digit of n
const d = n % 10;
if (d === 1 && n !== 11) return `${n}st`;
if (d === 2 && n !== 12) return `${n}nd`;
if (d === 3 && n !== 13) return `${n}rd`;
return `${n}th`;
}
// test it out
@trevor-atlas
trevor-atlas / console-log.sh
Last active April 17, 2018 19:24
Git pre-commit hook that detects if the developer forgot to remove all calls to `console.log`
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
consoleregexp='^\+.*console\.\(log\|error\|info\)('
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
@trevor-atlas
trevor-atlas / gdocToHtml.js
Created January 8, 2018 20:29
convert a google doc to HTML
function ConvertGoogleDocToCleanHtml() {
var body = DocumentApp.getActiveDocument().getBody();
var numChildren = body.getNumChildren();
var output = [];
var images = [];
var listCounters = {};
// Walk through all the child elements of the body.
for (var i = 0; i < numChildren; i++) {
var child = body.getChild(i);
@trevor-atlas
trevor-atlas / factorial.js
Created January 8, 2018 20:29
calculate factorials
'Use Strict';
// isInteger polyfill.
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
// Calculate and return the factorial of n.
@trevor-atlas
trevor-atlas / es6-compose.md
Last active December 9, 2017 00:51 — forked from JamieMason/es6-compose.md
ES6 JavaScript compose function

ES6 JavaScript Compose Function

Definition

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
const pipe = (...fns) => compose.apply(compose, fns.reverse());

Example

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
window.location.hostname === '[::1]' || // [::1] is the IPv6 localhost address
window.location.hostname.match( // 127.0.0.1/8 is considered localhost for IPv4
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
@trevor-atlas
trevor-atlas / squirt.js
Last active October 2, 2017 18:55 — forked from joelpt/squirt.js
Manually calculate the square root of a number with Javascript
// The Babylonian Method
// http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
// @param n - the number to compute the square root of
// @param guess - the best guess so far (can omit from initial call)
function squirt(n, guess) {
if (!guess) {
guess = n / 2.0; // Take an initial guess at the square root
}
const divided = n / guess; // Divide our guess into the number
const newGuess = (divided + guess) / 2.0; // Use average of guess and divided as our new guess
@trevor-atlas
trevor-atlas / tag.js
Created June 28, 2017 00:45
An easier to use console.log
const tag = (tag, param) => {
console.log(tag, param);
return param;
};
const foobar = tag('foobar', tag('foo', 100 * tag('bar', 500)));
console.log('foobar is: ', foobar);
//>> bar 500
//>> foo 50000
//>> foobar 50000
//>> foobar is: 50000
@trevor-atlas
trevor-atlas / traceProperty.js
Last active June 3, 2020 15:52
Trace an object's property over time
const traceProperty = (object, property) => {
let value = object[property];
Object.defineProperty(object, property, {
get() {
console.trace(`${property} requested`);
return value;
},
set(newValue) {
console.trace(`setting ${property} to `, newValue);
value = newValue;