F
u
l
l
S
t
a
c
k
S
n
i
p
p
e
t
s
.

A variety of code snippets, from backend to frontend, written in a clean-as-I-can-make-it style, ready for you to copy and paste wherever you may need them.

Check out the Full Stack Goodies page for a full PDF of these snippets, organized by language and level of the stack you would use them.

For TypeScript snippets, be sure to check out the magical JavaScript switch if you would like the snippet in vanilla JavaScript.

Client

useDidMount()

useDidMount.ts
import { useState, useEffect } from 'react'
export const useDidMount = (): boolean => {
const [didMount, setDidMount] = useState<boolean>(false)
useEffect(() => {
setDidMount(true)
}, [])
return didMount
}

sendSlackMessage()

sendSlackMessage.ts
export const sendSlackMessage = (message: string): void => {
process.env.SLACK_WEBHOOK_URL &&
fetch(process.env.SLACK_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: message,
}),
})
}

Backend

JavaScript (Node.js)

sendSlackMessage()

sendSlackMessage.ts
import fetch from "node-fetch";
export const sendSlackMessage = (message: string): void => {
process.env.SLACK_WEBHOOK_URL &&
fetch(process.env.SLACK_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: message,
}),
})
}

C#

PatchFiltererService

PatchFiltererService.cs
using System;
using System.Linq;
using Microsoft.AspNetCore.JsonPatch;
namespace JsonPatchFilterExample.Services
{
// a security filter for JSON patch filter operations
// see the full blog post at https://chrisfrew.in/blog/filtering-json-patch-in-c-sharp/
public static class PatchFiltererService
{
public static JsonPatchDocument<T> ApplyAttributeFilterToPatch<T, TU>(JsonPatchDocument<T> patch)
where T : class
where TU : Attribute
{
// Get path for all attributes of type TU that are in type T
var allowedPaths = typeof(T)
.GetProperties()
.Where(x => x.GetCustomAttributes(false).OfType<TU>().Any())
.Select(x => x.Name);
// Now build a new JSONPatchDocument based on properties in T that were found above
var filteredPatch = new JsonPatchDocument<T>();
patch.Operations.ForEach(x =>
{
if (allowedPaths.Contains(x.path))
{
filteredPatch.Operations.Add(x);
}
});
return filteredPatch;
}
}
}

Devops

Bash

buildColorPrompt()

buildColorPrompt.sh
function buildColorPrompt() {
# I always like showing what directory I am in (special character "\w" in PS1) - store the equivalent in this 'directory' variable
directory=$(pwd)
# Modify these to whatever you'd like!
PROMPT_TEXT="awesome-shell-prompt-colors@awesome-machine [$directory] "
# Colors seperated by comma - acceptable values are:
# black, white, red, green, yellow, blue, magenta, cyan, light gray, light red, light green, light yellow, light blue, light magenta, light cyan
PROMPT_COLORS="red,white,blue"
# Colors!
BLACK="\e[30m"
WHITE="\e[97m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
LIGHT_GRAY="\e[37m"
DARK_GRAY="\e[90m"
LIGHT_RED="\e[91m"
LIGHT_GREEN="\e[92m"
LIGHT_YELLOW="\e[93m"
LIGHT_BLUE="\e[94m"
LIGHT_MAGENTA="\e[95m"
LIGHT_CYAN="\e[96m"
# End formatting string
END_FORMATTING="\[\e[0m\]"
# split PROMPT_COLORS into array
count=0
IFS=','
for x in $PROMPT_COLORS
do
colors_array[$count]=$x
((count=count+1))
done
unset IFS
# break PROMPT_TEXT into character array
letters=()
for (( i=0 ; i < ${#PROMPT_TEXT} ; i++ )) {
letters[$i]=${PROMPT_TEXT:$i:1}
}
# build prompt with colors
color_index=0
ps1='\['
for (( i=0 ; i < ${#letters[@]} ; i++ )) {
# Determine color in this giant case statement
color="${colors_array[color_index]}"
case $color in
"black")
COLOR=$BLACK
;;
"red")
COLOR=$RED
;;
"green")
COLOR=$GREEN
;;
"yellow")
COLOR=$YELLOW
;;
"blue")
COLOR=$BLUE
;;
"magenta")
COLOR=$MAGENTA
;;
"cyan")
COLOR=$CYAN
;;
"light gray")
COLOR=$LIGHT_GRAY
;;
"dark gray")
COLOR=$DARK_GRAY
;;
"light red")
COLOR=$LIGHT_RED
;;
"light green")
COLOR=$LIGHT_GREEN
;;
"light yellow")
COLOR=$LIGHT_YELLOW
;;
"light blue")
COLOR=$LIGHT_BLUE
;;
"light magenta")
COLOR=$LIGHT_MAGENTA
;;
"light cyan")
COLOR=$LIGHT_CYAN
;;
"white")
COLOR=$WHITE
;;
*)
COLOR=$WHITE
;;
esac
# add to ps1 var - color, then letter, then the end formatter
ps1+=$COLOR"${letters[$i]}"
# reset color index if we are at the end of the color array, otherwise increment it
if (( $color_index == ${#colors_array[@]} - 1 ))
then
color_index=0
else
((color_index=color_index+1))
fi
}
ps1+="$END_FORMATTING\]"
# Finally: set the PS1 variable
PS1=$ps1
}
# Set the special bash variable PROMPT_COMMAND to our custom function
PROMPT_COMMAND=buildColorPrompt;

zsh

buildColorPrompt()

buildColorPrompt.sh
function buildColorPrompt() {
# I always like showing what directory I am in
directory=$(pwd)
# Modify these to whatever you'd like!
PROMPT_TEXT="youruser@yourmachine [$directory]"
# Comma seperated colors - as many or as few as you'd like
PROMPT_COLORS="15"
# This will be the color of everything in the input part of the prompt (here set to 15 = white)
PROMPT_INPUT_COLOR="15"
# split PROMPT_COLORS into array
colors_array=("${(@s/,/)PROMPT_COLORS}") # @ modifier
# break PROMPT_TEXT into character array
letters=()
for (( i=1 ; i < ${#PROMPT_TEXT}+1 ; i++ )) {
letters[$i]=${PROMPT_TEXT:$i-1:1}
}
# build prompt with colors
color_index=1
ps1=""
for (( i=1 ; i < ${#letters[@]}+1 ; i++ )) {
# Determine color in this giant case statement
color="${colors_array[color_index]}"
# add to ps1 var - color, then letter, then the end formatter
ps1+="%F{$color}${letters[$i]}"
# reset color index if we are at the end of the color array, otherwise increment it
if (( $color_index == ${#colors_array[@]} ))
then
color_index=1
else
((color_index=color_index+1))
fi
}
# end color formating
ps1+="%F{$PROMPT_INPUT_COLOR} %# "
# Finally: set the PROMPT variable
PROMPT=$ps1
}
# set the precmd() hook to our custom function
precmd() {
buildColorPrompt;
}