Skip to content

Instantly share code, notes, and snippets.

View krohne's full-sized avatar

Gregory Krohne krohne

  • Atlanta, Georgia, USA
View GitHub Profile
@krohne
krohne / excludesTime.js
Created November 17, 2017 17:48
Check if time range excludes a time
// You could play around with allowed inputs
import Moment from 'moment';
import { extendMoment } from 'moment-range';
const moment = extendMoment(Moment);
function excludesTime(start, end, when) {
let startTime = moment(start, 'HH:mm');
const endTime = moment(end, 'HH:mm');
@krohne
krohne / countdown.sh
Last active December 27, 2024 06:58
Countdown timer in bash shell script
#!/bin/bash
# $1 = # of seconds
# $@ = What to print after "Waiting n seconds"
countdown() {
secs=$1
shift
msg=$@
while [ $secs -gt 0 ]
do
printf "\r\033[KWaiting %.d seconds $msg" $((secs--))
@krohne
krohne / x100.js
Last active February 26, 2018 13:57
Multiply by 100 by string manipulation, to avoid floating point problems. Mostly for handling currency.
function x100(nStr) {
function replacer(match, p1, p2) {
return `${`${p1}${p2.padEnd(2, '0').substr(0,2)}`.padStart(1, '0')}.${p2.padEnd(2, '0').substr(2)}`;
}
return String(nStr) // just in case
.replace(/(\d*)\.?(\d*)/, replacer);
}
const tests = [
'0.00',
@krohne
krohne / toDigits.js
Created March 14, 2018 22:32
Force string to numeric-only string that can be converted to integer by replacing non-digits with integer character code
function toDigits (str = '') {
// replace every (/g) non-digit (\D) with the character code (charCodeAt) for that non-digit.
return str.replace(/\D/g, nondigit => nondigit.charCodeAt(0) );
}
console.log(toDigits('A1b2C3-4');
@krohne
krohne / update_repos.sh
Last active May 21, 2018 13:02
Update collection of git repositories quickly in parallel
#!/bin/bash
# $1 - a folder containing multiple git repos; i.e. ~/work
# The Mac way to get number of logical cores
# You can run more parallel processes than the number of cores,
# but at some point the process will slow to a crawl
cores=$( sysctl -n hw.ncpu )
# list all paths below $1 in single column, and pipe to xargs:
# ls -d1 $1/*/ |
# Run a bash command on each path, using $cores number of processes:
# xargs -I % -n 1 -P $cores bash --login -c
@krohne
krohne / decode_jwt.js
Created May 21, 2018 15:42
Decode Base64 JWT from login request in Postman
const jsonData = JSON.parse(responseBody);
const payload = jsonData.id_token.split('.')[1]; // Assuming the JWT is in id_token
const parsed = JSON.parse(atob(payload));
pm.environment.set('user_id', parsed.user_id); // Assuming user_id is in the payload
@krohne
krohne / update_repos_routematch.sh
Last active January 18, 2019 15:37
Update local Git repositories in parallel, 1 thread per core
#!/bin/bash
export GIT_ROOT=/Volumes/Routematch/Git
doit() {
cd $GIT_ROOT/$1
# What branch is this repo on?
# If master branch, checkout package.json and pull latest updates
# If .nvmrc exists, use specified version, else use node version 4
# Update dependencies
@krohne
krohne / set_remote_logging.sh
Last active January 11, 2019 20:21
Turn logging level up or down on remote platform
#!/bin/bash
# Your bastion username should be in ~/.ssh/config
# on|off platform [container]
# on preprod tripnotificationagent
export setting=$1
export platform=$2
export container=$3
if [ -z ${container} ]; then export container=$(grep '"name":\s*"\w*",' $PWD/package.json | awk -F '"' '{ print $4 }'); fi
export service="rmpay_${container}_1"
case $setting in
@krohne
krohne / restart_platform_container.sh
Last active January 11, 2019 20:21
Restart remote platform container, usually because a source file or logging level has been changed
#!/bin/bash
# Your bastion username should be in ~/.ssh/config
# platform [container]
# preprod tripnotificationagent
export platform=$1
export container=$2
if [ -z ${container} ]; then export container=$(grep '"name":\s*"\w*",' $PWD/package.json | awk -F '"' '{ print $4 }'); fi
echo "Container: $container"
if [ -z ${container} ];
then
@krohne
krohne / copy_to_platform.sh
Last active January 11, 2019 20:20
Copy source file to remote platform docker container. If running from the repository path, container is optional
#!/bin/bash
# Your bastion username should be in ~/.ssh/config
# platform path [container]
# preprod /routes/genesys/router.js tripnotificationagent
export platform=$1
export repo=$(basename "$PWD")
export filename=$2
export container=$3
if [ -z ${container} ]; then export container=$(grep '"name":\s*"\w*",' $PWD/package.json | awk -F '"' '{ print $4 }'); fi
export service="rmpay_${container}_1"