Skip to content

Instantly share code, notes, and snippets.

View n8jadams's full-sized avatar

Nate Adams n8jadams

View GitHub Profile
@n8jadams
n8jadams / download-object-as-json-file.js
Created March 1, 2022 19:15
A function to help downloading a javascript object as a JSON file
export async function downloadObjectAsJSONFile(object, filename) {
if(!filename.endsWith('.json')) {
filename = `${filename}.json`
}
const json = JSON.stringify(object)
const blob = new Blob([json],{ type:'application/json' })
const href = await URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = href
link.download = filename
@n8jadams
n8jadams / sibling-gift-exchange.ts
Last active November 10, 2021 13:16
A quick typescript function to assign siblings in our yearly gift exchange
interface Participant {
readonly name: string
assignment?: Participant
}
const allParticipants: Participant[] = [
{ name: 'Nate' },
{ name: 'Kyle' },
{ name: 'Zach' },
{ name: 'Bryce' },
@n8jadams
n8jadams / testing-ie11-with-vituralbox.md
Last active October 30, 2021 12:55
Testing IE11 with VirtualBox

Testing IE11 with Virtualbox

First, follow the instructions here to get a Windows 7/8.1 IE11 virtualbox image.

Then in Virtualbox:

  1. File > Host Network Manager, click Create+
  2. Click the newly created Host Network (probably named vboxnet0) and copy the IPv4 Address. (Probably 192.168.56.1.)
  3. Import the .ova image, fill out the options (defaults are fine,) and start up the VM.
  4. In the VM's Settings menu, click Network and set Attached to: to be Host-only Adapter. The Name: field should populate with vboxnet0.
@n8jadams
n8jadams / convert-apple-alac-to-mp3.sh
Created October 16, 2021 12:28
Convert Apple ALAC (.m4a) to .mp3
#!/bin/sh
# Find and convert all apple alac (.m4a) files to .mp3.
find . -name '*.m4a' -print0 | while read -d '' -r file; do
ffmpeg -i "$file" -n -acodec libmp3lame -ab 320k "${file%.m4a}.mp3" < /dev/null && rm "$file"
done
@n8jadams
n8jadams / lint-format-staged.js
Last active July 8, 2021 17:15
Run eslint and prettier against your staged changes. Great when used as a precommit hook. (Assumes it's placed in top level of repo)
const path = require('path')
const { execSync, spawnSync } = require('child_process')
const prettier = require('prettier')
const fs = require('fs')
const micromatch = require('micromatch')
const lockfile = path.resolve(__dirname, 'linter-script-lockfile')
const eslintFileTypes = ['js', 'jsx', 'ts', 'tsx']
@n8jadams
n8jadams / lint-staged.js
Last active July 8, 2021 17:15
Run eslint against your staged changes (Assumes this is placed at the top level of your repo)
const path = require('path')
const { execSync, spawnSync } = require('child_process')
const fs = require('fs')
const micromatch = require('micromatch')
const lockfile = path.resolve(__dirname, 'linter-script-lockfile')
const eslintFileTypes = ['js', 'jsx', 'ts', 'tsx']
function readGlobsFromFile(filename) {
@n8jadams
n8jadams / execAndPipeOutput.php
Last active February 9, 2021 21:18
Execute a command and pipe its output to stdout (echo it) as it's recieved, rather than wait for the command to finish executing
<?php
function execAndPipeOutput($cmd)
{
echo PHP_EOL;
$proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes);
while(($line = fgets($pipes[1])) !== false) {
fwrite(STDOUT, $line);
}
while(($line = fgets($pipes[2])) !== false) {
@n8jadams
n8jadams / git-prune-local.sh
Created February 3, 2021 17:03
A nice git function to clean up your local branches. Drop in your bashrc file(s).
# Deletes local branches that have been deleted on the remote
function gpl() {
# Determine the name of the trunk
trunkname=$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's@^origin/@@')
# List branches that have been removed from origin and write to file
git branch --merged $(git rev-parse $trunkname) | awk '{$1=$1};1' | egrep -v "(^\*|^\s*($trunkname)$)" > /tmp/branchesToPurge
# Open the file for editing
vim /tmp/branchesToPurge
@n8jadams
n8jadams / react-router-dom-custom-prompt.tsx
Last active September 25, 2020 15:50
Custom <Prompt /> for React Router Dom
// Instead of using the <Prompt />'s implementation
// (which uses the browser's 'confirm()',) use your own custom component!
import React, { useState, useEffect, useRef } from 'react'
import { useHistory } from 'react-router-dom'
import { Modal } from './Modal' // your custom component
interface CustomPromptProps {
when: boolean
onConfirmNavigation?: () => void
@n8jadams
n8jadams / copy-to-clipboard.ts
Created September 25, 2020 15:42
Simple function to copy some text to the clipboard in the browser
function copyToClipboard(textToCopy: string): void {
const tmpInputEl = document.createElement('input')
tmpInputEl.value = textToCopy
tmpInputEl.type = 'text'
tmpInputEl.style.position = 'absolute'
tmpInputEl.style.left = '9000vw'
document.body.appendChild(tmpInputEl)
tmpInputEl.select()
document.execCommand('copy')
document.body.removeChild(tmpInputEl)