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 / 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 / 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 / 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 / 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
)
@kylelambert101
kylelambert101 / ListSort.js
Created August 20, 2020 00:41
JavaScript -> Sort a list of objects
const myList = [
{ title: "First one", link: "https://en.wikipedia.org" },
{ title: "Second One", link: "https://en.wikipedia.org" },
{ title: "Actual First One", link: "https://en.wikipedia.org" },
];
// Sort the list by {each}.title, alphabetically
const sortedList = myList.sort((a, b) => (a.title > b.title ? 1 : -1))
@kylelambert101
kylelambert101 / Logging.py
Created August 18, 2020 17:35
Python -> Logging
'''
Use this to set up basic configuration for application logging.
Call this at the top in one file, then any following functions (even
in other files) can send log statements via the typical logging
functions (logging.info, logging.error, etc)
'''
# Set up logging
logging.basicConfig(
level=logging.DEBUG,