Skip to content

Instantly share code, notes, and snippets.

View kyle-west's full-sized avatar
🍰
Writing code (as always)

Kyle West kyle-west

🍰
Writing code (as always)
View GitHub Profile
@kyle-west
kyle-west / randLetter.js
Created July 7, 2023 15:58
Generate a random letter between range
const randLetter = (start = "A", end = "Z") => String.fromCharCode(
Math.floor(Math.random() * (end.charCodeAt(0) - start.charCodeAt(0) + 1)) + start.charCodeAt(0)
)
randLetter() // "F"
randLetter('a', 'd') // "d"
@kyle-west
kyle-west / RaspberryPi.init.md
Last active April 2, 2023 19:47
Set up RaspberryPi for first time. These are all the steps I take to initialize my Pi the way I like it. It's opinionated. Use it if you want, at your own risk. I have it public so that I can find it without login.

Require Login

sudo nano /etc/lightdm/lightdm.conf

Make sure these two pieces are set/unset:

greet-hide-users=false
...
@kyle-west
kyle-west / rotate-one-dimensional-array.js
Last active November 5, 2022 13:37
Rotate (right or left) a 2D matrix implemented as a 1D array. Also it should be noted that this is called "transpose" in the math world.
// You can adjust these params to suit your actual dimensions
const width = 10;
const height = 10;
const idx = (y, x) => y * height + x
// GIVEN:
// const matrix1D = [0,1,2,3, /* ... */, 98,99]
// const matrix2D = [
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
@kyle-west
kyle-west / console.js
Created October 12, 2022 14:26
Print console.log to the browser in React
import { useEffect, useState } from 'react'
const _logEntries = new Set()
function log (...args) {
const entry = { args, id: performance.now() }
_logEntries.add(entry)
window.dispatchEvent(new CustomEvent('custom-log', { detail: entry }))
}
@kyle-west
kyle-west / make.js
Last active April 18, 2022 16:37
A simple DOM node stamping function
window.make = (type, { children = [], ...props } = {}, attrs = {}) => {
let elem = document.createElement(type)
Object.assign(elem, props)
if (children instanceof Array) {
children.forEach(child => elem.appendChild(child))
} else if (children instanceof Node) {
elem.appendChild(children)
} else {
elem.innerText = children
}
@kyle-west
kyle-west / secret-santa.js
Last active December 7, 2023 17:04
Classic Secret Santa Problem in 4 lines. Guarantees mathematically that no one will give to themselves and no two will give to each other.
function secretSanta(...names) {
const randomized = [...names].sort(() => Math.random() - Math.random());
const derrangement = [...randomized]
derrangement.unshift(derrangement.pop()) // shifting ensures a derrangement of the randomized list
return Object.fromEntries(randomized.map((name, idx) => [name, derrangement[idx]]))
}
secretSanta('Alice', 'Bob', 'Carter', 'David', 'Emily')
// { Carter: 'Emily', Bob: 'Carter', David: 'Bob', Alice: 'David', Emily: 'Alice' }
@kyle-west
kyle-west / useImperativeHandle_to_sync_refs.js
Last active March 4, 2025 23:48
This is how I like to use useImperativeHandle to "sync" or compose refs in React
import React, { useImperativeHandle, useRef, forwardRef, useCallback, useEffect } from 'react'
export const Component = forwardRef(({ callbackRef }, externalRef) => {
const internalRef = useRef()
// use imperative handle lets you "sync" any number of external refs with the value of the internalRef
useImperativeHandle(externalRef, () => internalRef.current)
useImperativeHandle(callbackRef, () => internalRef.current)
useEffect(() => {
@kyle-west
kyle-west / lang-cp
Created August 6, 2020 15:20
Copy string keys from a locale JSON file to another
#!/bin/bash
###########################################################################
# USAGE Example:
# lang-cp from_dir from_file_prefix to_dir key1 key2 key3@KEY_3_NEW_NAME key4
###########################################################################
from_list=$(find $1 | grep "$2_.*json")
echo "Copying from $2..."
for f in $from_list; do
lang=$(echo $f | cut -d '_' -f 2 | cut -d '.' -f 1)
to_lang=${lang//-/_}
@kyle-west
kyle-west / covid-19
Last active March 11, 2020 14:32
Get the latest stats on on the COVID-19 (Coronavirus) for your state on the command line (with notifications on Mac OSX)
#!/bin/bash
url='https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27US%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true'
__getStats () {
json <(curl -s "$url") "
const data = \$.features.filter(x => x && x.attributes && x.attributes.Province_State && x.attributes.Province_State.includes && x.attributes.Province_State.includes('$1'))
const { Confirmed, Deaths, Recovered } = (data && data[0] && data[0].attributes) || {};
Confirmed && console.log('Confirmed: ' + Confirmed, 'Recovered: ' + Recovered, 'Deaths: ' + Deaths);
!Confirmed && console.log('No Data Found on $1')
@kyle-west
kyle-west / co-authored
Created November 27, 2019 16:13
Append a commit message with a "Co-authored-by" message with just a GitHub username
#!/bin/bash
###########################################################################################
# co-authored : append a Co-authored-by message to your commit history with just a username
# co-authored <github-username> [<github-username>...]
###########################################################################################
for coAuthorUsername in $@; do
msg=$(git log --format=%B -n1)
coAuth="Co-authored-by: $(json <(curl -s "https://api.github.com/users/${coAuthorUsername}") $.name) <${coAuthorUsername}@users.noreply.github.com>"