Skip to content

Instantly share code, notes, and snippets.

View kendallroth's full-sized avatar
🍁

Kendall Roth kendallroth

🍁
View GitHub Profile
@kendallroth
kendallroth / README.md
Last active June 15, 2023 20:32
Strongly type React Hook Form field names

Typing RHF Fields

While react-hook-form supports strong typing everywhere, including support for types inferred from a Yup schema (Yup.InferType<Schema>), passing field names to controls is still untyped. However, this can be resolved with RHF's FieldPath type and a helper function.

Options

// Option 1: Declared per form, less boilerplate per control
export const getOrganizationFormField = (name: FieldPath<OrganizationForm>) => name;
getOrganizationFormField("billingAddress.address1");
@kendallroth
kendallroth / cache.ts
Created August 25, 2022 05:10
Simple TS caching mechanism (use Map instead though...)
type CacheKey = string | number;
interface Cache<T> {
/** Number of cache entries */
readonly count: number;
/** Get an item from the cache */
get: (key: CacheKey) => T | null;
/** Check whether cache contains a given item */
has: (key: CacheKey) => boolean;
/** Cached items */
@kendallroth
kendallroth / reduce-jupyter-git-conflicts.md
Last active July 20, 2022 15:56
Reduce Jupyter Notebook Git conflicts

Jupyter Git Conflicts

Jupyter Notebooks have a tendency to cause Git conflicts when working collaboratively on a notebook. This is primarily due to cell metadata that really doesn't matter, and can be removed with a pre-save configuration hook. This hook will be run across all Jupyter Notebooks; however, it utilizes a custom opt-in metadata configuration option (reduce_git_conflicts). If the opt-ine is not specified, or set to false, the Notebook will save as normal.

  1. Add pre-save hook to Jupyter Notebook config
  2. Enable reducing Git conflicts in Notebook metadata

NOTE: This approach does modify the notebook save file itself, which could lead to issues if there are fields that must remain in the file but lead to conflicts (and would otherwise be removed).

NOTE: This approach was developed with Jupyter Notebook in mind. While Jupyter Lab is quite similar, there are several differences (generating config, editing metadata).

@kendallroth
kendallroth / ignore_diffs.md
Created January 24, 2022 17:43
Exclude files from Gid diff

Some projects may have files that should be skipped in Git diff but still show in Git status (and be tracked). This can be made possible with a custom diff driver that uses a no-op command.

Adapted from StackOverflow - @KurzedMetal.

Custom diff driver

Create a global no-op diff command driver with the following command. Note that driver can be limited to local respository by removing the --global flag.

git config diff.nodiff.command /bin/true
@kendallroth
kendallroth / updateNestedValue.ts
Created January 20, 2022 23:14
Update value in nested objects (with/without mutation)
/**
* Dynamically sets a deeply nested value in an object (returns new object)
*
* NOTE: By default it will create any missing nested keys!
*
* @param obj - Object which contains the value to set
* @param path - Path to nested key being set
* @param value - Target value to set
* @param recursive - Whether to create non-existing paths
* @returns Updated nested object
@kendallroth
kendallroth / .vimrrc
Created January 1, 2022 20:15
Standard Vim configuration
"""""""""""""""""""""""""""""""""""""""""""""""""""
" 01. General
""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible
filetype off
""""""""""""""""""""""""""""""""""""""""""""""""""
" 02. Vundle plugins
""""""""""""""""""""""""""""""""""""""""""""""""""
set rtp+=~/.vim/bundle/Vundle.vim
@kendallroth
kendallroth / forward_wsl2_ports.ps1
Last active March 5, 2024 16:04
Forward WSL2 ports to host
# Forward WSL2 ports to host machine/platform (handles Windows Firewall)
#
# NOTE: 'iex' is a shortform for 'Invoke-Expression'
# Ports that should be forwarded to WSL2 and allowed through firewall (comma-separated)
$ports = @(8081);
# WSL IP address changes whenever WSL restarts
$wsl_ip = $(wsl hostname -I).Trim();
@kendallroth
kendallroth / multiple_ssh_keys.md
Last active August 28, 2017 17:33
Manage multiple SSH keys and Git accounts

Managing multiple SSH keys and Git accounts

It can sometimes be useful to have multiple SSH keys for the same Git hosts, which requires some additional SSH configuration.

Example

A typical example involves using the same host (GitHub) with multiple accounts on the same computer. Since each account should use its own SSH key, we need to use a combination of the SSH configuration and Git remotes to determine which SSH key to use.

Configuration

@kendallroth
kendallroth / print_github_markdown.md
Created March 22, 2017 14:10
Print GitHub Markdown

Print GitHub Markdown

Either create a browser snippet or execute from console:

var content = document.querySelector('.markdown-body');
var body = document.querySelector('body');
body.innerHTML = '';
body.appendChild(content);
@kendallroth
kendallroth / togglefiles.vbs
Created October 11, 2016 15:45
Add "Toggle Hidden Files" to Windows context menu
option explicit
On Error GoTo 0
'Dim sResult: sResult = Wscript.ScriptName ''' debugging
Const HKEY_CURRENT_USER = &H80000001
Dim strComputer, strKeyPath, strValName, dwValue, arrValues, objReg, WshShell
arrValues = Array (" unknown", " show hidden files", " hide hidden files" )
strComputer = "."