Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
crazy4groovy / kaprekar.js
Last active December 12, 2022 07:02
Kaprekar's Constant (JavaScript)
// https://brilliant.org/wiki/kaprekars-constant
// 4 digits == 6174
// 3 digits (>= 123) == 495
function kaprekar(num) {
const sortedSmall = String(num).split('').sort()
const sortedBig = sorted.slice().reverse()
const a = Number(sortedBig.join(''))
const b = Number(sortedSmall.join(''))
@crazy4groovy
crazy4groovy / Caddyfile
Last active December 9, 2022 16:52
Caddy - localhost (with subdomains) ssl proxy server (see comments)
my_subdomain.localhost {
reverse_proxy localhost:3000
}
@crazy4groovy
crazy4groovy / speech.js
Last active December 6, 2022 17:39
browser voice / speechSynthesis API (JavaScript)
if (typeof speechSynthesis === 'undefined') {
return;
}
const name = 'Steven Olsen'
console.log(speechSynthesis.getVoices().length, "voices")
speechSynthesis.getVoices().forEach((voice, i) => {
const utt = new SpeechSynthesisUtterance(`${name} number ${i}`)
utt.voice = voice
@crazy4groovy
crazy4groovy / fetch-article.deno.js
Last active December 3, 2022 18:06
YouTube embedded videos checker (Deno)
/* Note: some URLs and logic below may need tweaking!! */
const domain = Deno.env.WEBSITE_DOMAIN; // <<<<<<<<<<<<<<<<<<<<<<<<
async function findMaxPageCount() {
const r = await fetch(`${domain}/news/`)
.then((r) => r.text())
.then((r) => r.replace(/\r?\n/g, ""));
// eg. <div class="pagination"><p class="counter pull-right"> Page 1 of 72 </p>
return Number(r.match(/class="pagination".*?Page 1 of (\d+)/)?.pop());
@crazy4groovy
crazy4groovy / event.html
Created December 1, 2022 16:33
simple HTML page to download a file
<!DOCTYPE html>
<html lang="en-US">
<meta charset="utf-8">
<title>Redirecting&hellip;</title>
<link rel="canonical" href="/event.ics">
<script>location="/event.ics"</script>
<meta http-equiv="refresh" content="0; url=/event.ics">
<meta name="robots" content="noindex">
<h1>Redirecting&hellip;</h1>
@crazy4groovy
crazy4groovy / package.json
Created October 13, 2022 15:36
dependency-cruiser to generate ES import dependencies as a map (SVG)
...
"scripts": {
"arch": "depcruise --include-only \"^src\" --output-type dot src | dot -T svg > dependencygraph.svg",
...
"dependencies": {
"dependency-cruiser": "^11.16.0",
...
@crazy4groovy
crazy4groovy / app.js
Last active November 4, 2022 20:27
context + hook pattern (ReactJS)
import { UserProvider } from "./context"
import { Component } from "./component"
export default function App() {
return (
<UserProvider>
<h1>Context App</h1>
<Component />
<br />
<Component />
@crazy4groovy
crazy4groovy / brev.sh
Created June 17, 2022 02:58
brev env setup
# https://docs.brev.dev/howto/automatically-set-up/
##### Homebrew #####
(echo ""; echo "##### Homebrew #####"; echo "";)
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | bash -
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/brev/.bash_profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/brev/.zshrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install fnm
@crazy4groovy
crazy4groovy / useEvent.tsx
Last active May 19, 2022 20:25 — forked from diegohaz/useEvent.tsx
Make a component callback prop stable (i.e. cannot be used during render phase) (ReactJS)
type AnyFunction = (...args: any[]) => any
function useEvent<T extends AnyFunction>(callback?: T) {
const ref = useRef<AnyFunction | undefined>(() => {
throw new Error("Cannot call an event handler while rendering.")
})
useLayoutEffect(() => {
ref.current = callback
})
return useCallback<AnyFunction>(
@crazy4groovy
crazy4groovy / toKeyedArray.js
Created April 26, 2022 03:46
A wrapper that takes an object and defines some array-like behavior for it (JavaScript)
// credit: https://www.30secondsofcode.org/articles/s/javascript-object-array-proxy
const toKeyedArray = obj => {
const methods = {
map(target) {
return (callback) =>
Object.keys(target).map(key => callback(target[key], key, target));
},
reduce(target) {
return (callback, accumulator) =>