Skip to content

Instantly share code, notes, and snippets.

View kylelambert101's full-sized avatar

Kyle kylelambert101

View GitHub Profile
@kylelambert101
kylelambert101 / jq.bash
Last active September 23, 2022 15:06
Bash -> jq CheatSheet
# Aggregate keys of all objects in an array
jsonArrayData | jq '[.[] | keys] | flatten | unique'
# Map array to reduced objects and sort by a field
# Assumes array of {id, name, items:[]}
jsonArrayData | jq '[.[] | {id, name, itemCount:.items | length}] | sort_by(.itemCount)'
# Filter an array by matching property
jsonArrayData | jq '. [] | select(.id == "SomeId")'
@kylelambert101
kylelambert101 / usePrevious.js
Created September 22, 2021 18:13
React -> Use Previous State Values
// From https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state
function Counter() {
const [count, setCount] = useState(0);
const prevCountRef = useRef();
useEffect(() => {
prevCountRef.current = count;
});
const prevCount = prevCountRef.current;
@kylelambert101
kylelambert101 / Conditionals.bash
Last active March 15, 2022 13:03
Bash -> if/else | while | until
# Simple if/else block
MYVAL=$(some_command)
if [[ $MYVAL -eq 1 ]]
then
echo "It was one"
elif [[ $MYVAL -eq 2 ]]
then
echo "It was two"
else
echo "It was not one"
@kylelambert101
kylelambert101 / SimpleContext.tsx
Created February 14, 2022 13:52
React -> Simple Context
import * as React from 'react';
export interface SimpleContextType {
userName: string | undefined;
setUserName: (userName: string | undefined) => void;
}
export const SimpleContext = React.createContext<SimpleContextType>({ userName: undefined, setUserName: () => undefined });
export const SimpleContextProvider = ({ children }: { children: React.ReactNode }): React.ReactElement => {
@kylelambert101
kylelambert101 / git-prompt.sh
Last active September 21, 2021 19:22
Git Bash -> Custom Prompt
# To be placed in ~/.config/git/git-prompt.sh
# Will be picked up by git bash /etc/profile.d/git-prompt.sh as a custom configuration
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[34m\]' # change to blue
PS1="$PS1"'[`date +%F_%T`] ' # date
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'\u' # user
PS1="$PS1"'\[\033[95m\]' # change to purple
PS1="$PS1"'@\h ' # @host<space>
@kylelambert101
kylelambert101 / InlineTypeGuard.ts
Last active May 13, 2021 01:14
TypeScript -> Inline Type Guard
// Source: https://www.benmvp.com/blog/filtering-undefined-elements-from-array-typescript/
/*
* Let's say I have a list of MyObjects and I want to filter down to all of the contained MySubProperties.
*
* I need to filter out the objects with undefined property values since they cannot have a subproperty, and
* I need TypeScript to understand that the result type of that filter is MyProperty[] rather than
* (MyProperty | undefined)[] - that way, ts won't flag the access of p.subProperty for possibly undefined values.
*/
@kylelambert101
kylelambert101 / RegularExpressions.js
Last active April 25, 2021 16:18
JavaScript -> Regular Expressions
/*- To test without capturing matches -*/
const isUpperCaseLetter = /[A-Z]+/.test(myString);
/*- Named capture groups -*/
const result = 'John Doe'.match(/^(?<first>\w+)\s+(?<last>\w+)/)?.groups;
/* result:
{
first: "John",
last: "Doe"
}
@kylelambert101
kylelambert101 / GitDropboxRemote.bash
Last active November 19, 2020 20:28
Git -> Set dropbox as a remote repo
# From https://medium.com/@optalgo/use-dropbox-as-a-git-repo-96c0e6412549
#Create and setup the Dropbox base repo
mkdir -p ~/Dropbox/Git
cd ~/Dropbox/Git
# Do the following for every project you want to add
git init --bare myProject.git
# Add Dropbox as a remote branch named "origin"
cd ~/myProject
@kylelambert101
kylelambert101 / CallCMD.bash
Created November 3, 2020 14:40
Git Bash -> Call CMD Prompt
# To run a cmd command from git bash, prefix with "cmd //c"
cmd //c echo %PATH%
# I have experienced scenarios where I have custom batch files that are in my cmd
# path and I want to call them from Git Bash.
# To do this, alias the command names in ~/.bashrc to add the cmd prefix:
# .bashrc
alias search='cmd //c search'
alias explore='cmd //c explore'
@kylelambert101
kylelambert101 / TitleCase.js
Created August 25, 2020 18:15
JavaScript -> Convert a string to titlecase
export const convertToTitleCase = (str) => {
return str
// Split on spaces
.split(' ')
// Capitalize the first letter of each word
.map((word) =>
word.length > 0
? word[0].toUpperCase() + word.substr(1).toLowerCase()
: word
)