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 / 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.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 / 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 / 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 / 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 / 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 / randomDigits.js
Last active November 7, 2017 20:40
random decimal digits using Node crypto
// Limited to max 16 digits
function randomDigits(digits) {
return parseInt(
crypto
.randomBytes(Math.floor(digits/2))
.toString('hex')
,16)
.toString(10)
.substr(-digits);
}
@krohne
krohne / yesterday.js
Created January 19, 2017 13:59
Yesterday's date in one line
return ( (new Date( new Date( (new Date()).toISOString().slice(0,10) ) - 1 ) ).toISOString().slice(0,10) );
@krohne
krohne / downloadsize.js
Last active October 18, 2016 17:20
Nashorn Javascript CLI that will report the download size of a given URL as a command line parameter
#!env jjs
// jjs filesize.js -- config.txt
// Sample config.txt:
// http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf
var Files = Java.type('java.nio.file.Files');
var Paths = Java.type('java.nio.file.Paths');
var URL = Java.type('java.net.URL');
function getSize(url) {
@krohne
krohne / evens.js
Created October 18, 2016 15:27
Given an array of n integers (example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ), // remove all odd numbers, leaving only the even numbers.
// given an array of n integers (example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ),
// remove all odd numbers, leaving only the even numbers.
const x = [
-Math.MAX_VALUE, -113, -112, -100, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, -Math.MIN_VALUE, -0,
0, Math.MIN_VALUE, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113, Math.MAX_VALUE
];
console.log(x.filter( n => { return ( n % 2 === 0); }));