Skip to content

Instantly share code, notes, and snippets.

@pbatey
pbatey / .zshrc-mark-jump
Last active June 5, 2024 17:52
mark and jump commands for faster navigation
# Add `. ~/.zshrc-mark-jump` to ~/.zshrc for faster navigation with mark and jump
export MARKPATH=$HOME/.marks
function jump {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
mark=$1
if [ -z "$mark" ]; then marks; return; fi
if [ "$mark" = "." ]; then mark=$(basename "$PWD"); fi
@pbatey
pbatey / s3-mp4-video.ts
Created March 28, 2024 22:18
express route to serve mp4 video files from AWS s3
// npm i express@^4.15.2 @aws-sdk/client-s3@^3.540.0
import { Request, Response, Router } from 'express'
import { GetObjectCommand, HeadObjectCommand, S3Client } from '@aws-sdk/client-s3'
const router = Router()
/** stream a video from S3 */
router.get('/:filepath(*.mp4)', async (req: Request, res: Response) => {
const filepath = req.params.filepath || req.params[0]
const range = req.headers.range;
#
# This file should be sourced from ~/.bashrc or ~/.zshrc
#
# export VAULT_ADDR=<your vault URL>
# source ~/.yv-funcs
#
# It requires yq, gdate, and gum to be installed (brew install yq coreutils gum)
#
# It requires a yaml file ~/.yv-funcs.rc to contain entries:
#
@pbatey
pbatey / aws-parameters
Last active July 13, 2023 21:44
Bash script to update aws ssm parameters from a yaml file
#!/bin/bash
tmp=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
rm -rf "$tmp"
}
trap finish EXIT
OPTS=""
if [[ $1 == --region ]]; then
OPTS="--region $2"
@pbatey
pbatey / withShutdown.js
Created February 17, 2023 16:33
Function to shutdown nodejs/express http.Server gracefully
module.exports = withShutdown
// This may be unnessary with server.closeIdleConnections and server.closeAllConnections added in Node v18.2.0.
/**
* Adds a shutdown function that destroys active connections
* (such as keep-alive). Idle connections end immediately,
* others are destroyed after all data is written.
*
* @param {http.Server} server http.Server instance
@pbatey
pbatey / shell-colors.md
Last active September 16, 2022 21:16
A couple of alias to remind me how to change colors in bash or zsh

A couple of alias to remind me how to change colors in bash or zsh

in ~/.bashrc or ~/.zshrc

alias colors='for ((i=30;i<=37;i++));do echo -e "\033[0;"$i"m echo -e \"\\\033[0;"$i"m"\";done;echo -e "\033[0;m echo -e \"\\\033[0;m\""'

alias declare_clr='\
  echo "declare -A clr";
  for a in 30m/black 31m/red 32m/green 33m/yellow 34m/purple 35m/magenta 36m/cyan 37m/white m/reset; do \
@pbatey
pbatey / two_git_accounts.md
Last active September 11, 2022 23:24
Two git accounts

Two Github Accounts

My workplace has recently begun using github-enterprise hosted by github in the cloud, rather than using github-enterprise in a self-hosted environment. This means that I now have another account tied to my company user-identity which conflicted with my git ssh configuration.

I wasn't thrilled with the other solutions I'd found to manage two accounts:

  • Shell script wrappers around git that set the SSH_COMMAND environemnt varialbe on the fly, and
  • Changing the origin in the repo's .git/config to use a fake hostname (github.com-personal) and map the fake hostname in ~/.ssh/config.
@pbatey
pbatey / README.md
Last active April 7, 2022 17:43
A react hook to sync useState with useSearchParams

A react hook to sync useState with useSearchParams

Usage

useSearchState is used similarly to useState in that it returns a value and setter. The simplest use case is to tie the value to the search params with a key.

MyComponent.tsx

@pbatey
pbatey / toWordCase.js
Created January 30, 2021 16:31
Convert camelCase and snake_case to Word Case
const noVowelsToUpper = key => key.match(/[aeiouy]/) ? key : key.toUpperCase()
const lowerCaseTwoLetter = w => {
const words = [ 'of', 'to', 'in', 'it', 'is', 'as', 'at', 'by', 'or', 'on', 'if', 'an' ]
if (w.length === 2 && words.indexOf(w.toLowerCase()) >= 0) return w.toLowerCase()
return w
}
const upperCaseTwoLetter = w => {
const words = [ 'CA', 'ID' ]
if (w.length === 2 && words.indexOf(w.toUpperCase()) >= 0) return w.toUpperCase()
@pbatey
pbatey / toFraction.mjs
Last active January 23, 2021 23:21
Show number as fraction in javascript
/**
* Render a number as a fraction
* @param {number} v the decimal value to convert to a fraction
* @param {number[]} denom the list of denominators
* @returns {string} the fraction closest to the decimal value
*/
const toFraction = (v, denom=[2,3,4,7,8,10,16,32,64,100]) => {
denom = denom.sort((a,b)=>a-b)
// whole and fractional parts