Skip to content

Instantly share code, notes, and snippets.

@hattmarris
hattmarris / docker-commands
Last active August 27, 2019 20:51
helpful-docker-commands
# Identifying a problem container
docker stats --all --format "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
# Checking specific container process
docker ps | grep <container id>
@hattmarris
hattmarris / jestcdir.sh
Created July 25, 2019 20:38
jest coverage in a directory
#!/usr/bin/env bash
echo "jest coverage in a directory"
if [ $# -eq 0 ]
then
echo "No dir supplied"
exit
fi
DIR=$1
#!/usr/bin/env bash
case $# in
2) DIR=.;SEARCH=$1;REPLACE=$2;;
3) DIR=$1;SEARCH=$2;REPLACE=$3;;
esac
echo "searching for string in $DIR for *.jsx files: $SEARCH"
echo "replacing with string in *.jsx files: $REPLACE"
@hattmarris
hattmarris / fetch-async-await.js
Last active March 25, 2018 10:12
Fetch Async Await
/* Async approach global namespace */
async function getData() {
return await fetch("https://jsonplaceholder.typicode.com/posts/1").then(response => response.json());
}
let data = {};
getData().then(d => data = d);
/* Class approach */
@hattmarris
hattmarris / hotfix.sh
Last active April 11, 2017 18:50
Git hotfix
git checkout master
git checkout -b hotfix # branch off master, make changes here
# PR could be submitted at this point for merge to master (production)
git checkout master
git merge hotfix # merge the fix into master
# Could deploy immediately
@hattmarris
hattmarris / batch-images.js
Created March 22, 2017 14:09
JavaScript - Batch Download Images
// <img>s wrapped in <a> tags
// Select images
var images = document.querySelectorAll('img');
// Get parent links
var links = [];
images.forEach(
function (el) {
links.push(el.parentElement);
}
@hattmarris
hattmarris / selectorTest.js
Last active April 30, 2016 00:23
Test attribute vs id selection
var id = getRandomInt();
var div = document.createElement('div');
div.setAttribute('id', id);
var reps = 100;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function selectByAttribute(reps) {
@hattmarris
hattmarris / Logger.js
Created April 13, 2016 17:45
Logger Module
function Logger(state) {
var on = setInitial(state),
levels = {
error: 0,
log: 1,
trace: 2
};
// Define on property getter and setter
Object.defineProperty(this, 'on', {
@hattmarris
hattmarris / download-images.js
Last active April 8, 2016 19:49
Get images on a page an download them
// Select a NodeList of image elements
var selector = 'ul.pages li img[src]'; // Images with src nested in <li> els in the <ul> with pages class
var images = document.querySelectorAll(selector);
function saveAll(images) {
for(var i=0; i<images.length; i++) {
var image = images[i];
saveImageAs(i+1, images[i].src); // use index+1 as their name for page number approximation
}
}
@hattmarris
hattmarris / mysql.sql
Created December 22, 2015 21:59
MySQL
# tabular view of data with scrolling
mysql> pager less -SFX
mysql> SELECT * FROM sometable;