Skip to content

Instantly share code, notes, and snippets.

View jamesholcomb's full-sized avatar

James Holcomb jamesholcomb

View GitHub Profile
@jamesholcomb
jamesholcomb / deep-assign.js
Last active March 2, 2022 15:25
Deeply traverse an object to replace or redact values using a regex and replacer function
export const assignDeep = (input, regex, replacer) => {
const json = JSON.stringify(input)
return JSON.parse(json, (k, v) =>
typeof v === "string" && regex.test(k) ? replacer(v) : v
)
}
export const redactDeep = (input, regex) =>
assignDeep(input, regex, () => "[REDACTED]")
@jamesholcomb
jamesholcomb / deep-merge-example.js
Last active June 30, 2020 16:28
Deep merge with comparator to ensure null/undefined data values are patched
import { mergeWith, isNil } from "lodash"
var existing = {
a: 1,
b: 2,
d: {
a: 1,
b: [],
c: { test1: 123, test2: 321 }
},
@jamesholcomb
jamesholcomb / script.sh
Created October 1, 2020 18:46
Bash functions
#!/bash/bin
# Recursively find all occurences of X in all *.js files excluding node_modules
egrep -lir --include=*.js "X" --exclude-dir="node_modules" .
@jamesholcomb
jamesholcomb / codes.js
Created October 31, 2020 16:57
Country codes
export const COUNTRY_CODES = {
AF: "Afghanistan",
AL: "Albania",
DZ: "Algeria",
AS: "American Samoa",
AD: "Andorra",
AO: "Angola",
AI: "Anguilla",
AQ: "Antarctica",
AG: "Antigua and Barbuda",
@jamesholcomb
jamesholcomb / script.sh
Last active October 12, 2022 14:17
Code mod to remove // @ts-check from
# recurisvely remove lines containing // @ts-check from all javascript files in current directory
find . -type f -name "*.js" -print0 | xargs -0 sed -i '' -e '/@ts-check/d'
@jamesholcomb
jamesholcomb / del-keys
Created March 9, 2021 15:41
The definitive script to remove keys by pattern including spaces from redis on MacOS
redis-cli --scan --pattern "myprefix:*" | tr \\n \\0 | xargs -0 redis-cli unlink
@jamesholcomb
jamesholcomb / gist:dee325408d68e22bf3491624887757a8
Last active July 8, 2021 19:32
How to move to a fork after cloning? Switch the cloned fork to a new remote origin.
git clone https://github.com/otherusername/some-awesome-repo.git
# make changes, commit as needed
git remote set-url origin https://github.com/myusername/myforkedrepo.git
git push
~done~
@jamesholcomb
jamesholcomb / index.ts
Last active July 9, 2021 19:41
bullmq example showing add job to queue with same jobId
// only one job is added despite removeOnComplete: true
import { Queue, QueueScheduler, Job, Worker } from "bullmq"
const now = () => `[${new Date().toISOString()}]`
const queue = new Queue("Paint", {
defaultJobOptions: {
removeOnComplete: true,
},
})
@jamesholcomb
jamesholcomb / index.ts
Last active August 18, 2021 11:58
bullmq missing lock repro
import IORedis from "ioredis"
import { Queue, QueueScheduler, Job, Worker } from "bullmq"
const main = async (): Promise<void> => {
const qs = new QueueScheduler("Paint", {
connection: new IORedis(),
})
const qPaint = new Queue("Paint", {
connection: new IORedis(),
defaultJobOptions: {
@jamesholcomb
jamesholcomb / clone-collection.sh
Created September 19, 2022 14:49
Clones a mongodb collection and its indexes into __collection__
#!/bin/bash
if [[ -z "$1" || -z "$2" ]]
then echo -e "Usage: clone-collection.sh <db> <collection>"; exit 1
fi
# clones a mongodb collection and its indexes into __collection__
mongodump -v -d $1 -c $2 --out=- | mongorestore -v -d $1 -c __$2__ --drop --dir=-