Skip to content

Instantly share code, notes, and snippets.

View SlideeScherz's full-sized avatar

Scott Scherzer SlideeScherz

View GitHub Profile
@SlideeScherz
SlideeScherz / parseByContentType.ts
Created September 22, 2023 15:10
Parse a Response object by its content type, to avoid a JSON.parse error
const parseByContentType = async <T = unknown>(data: Response): Promise<T> => {
const contentType = data.headers.get('Content-Type');
if (contentType === null) {
console.log('contentType is null');
return (await data.text()) as T;
} else if (contentType.includes('application/json')) {
return (await data.json()) as T;
} else if (contentType.includes('text')) {
return (await data.text()) as T;
@SlideeScherz
SlideeScherz / generate-directory-tree.sh
Created March 23, 2024 04:01
generate-directory-tree
#!/bin/bash
tree=$(tree -tf --noreport -I '*~' --charset ascii $1 |
sed -e 's/| \+/ /g' -e 's/[|`]-\+/ */g' -e 's:\(* \)\(\(.*/\)\([^/]\+\)\):\1[\4](\2):g')
printf "# Project tree\n\n${tree}"
@SlideeScherz
SlideeScherz / mock-env-jest.md
Last active April 11, 2024 14:40
Snippet for mocking .env variables in Jest

Mocking .env in Jest

Mocking .env variables in isolation during Jest tests can be useful to test different scenarios without changing the actual .env file. This is helpful for functions that read from the .env file especially in a different scope or context.

getFilePath.ts

export const getFilePath = () => process.env.FILE_PATH;
@SlideeScherz
SlideeScherz / git-line-count.md
Last active April 12, 2024 03:59
git-line-count

git line count

git ls-files | grep -E '.(ts|tsx|js|jsx)$' | xargs wc --lines

output:

$ git ls-files | grep -e '.ts$' -e '.tsx$' | xargs wc --lines
@SlideeScherz
SlideeScherz / verify-local-env.ps1
Created April 12, 2024 14:35
Confirm vscode, nodejs and npm are correctly installed.
$nodeVersion = node -v
Write-Host "Node.js Version: $nodeVersion"
if ($nodeVersion -eq $null) {
Write-Host "Node.js is not installed."
exit 1
}
$npmVersion = npm -v
Write-Host "npm Version: $npmVersion"
if ($npmVersion -eq $null) {
#!/bin/bash
# Install VS Code extensions
# if need to use powershell: https://gist.github.com/vmandic/ef80f1097521c16063b3b1c3a687d244
# `code --install-extension` will echo
# echo Installing VS Code Extensions...
extensions=(
dsznajder.es7-react-js-snippets