Skip to content

Instantly share code, notes, and snippets.

View jonrandy's full-sized avatar
✴️
Sentient

Jon Randy jonrandy

✴️
Sentient
View GitHub Profile

Every programmer occasionally, when nobody’s home, turns off the lights, pours a glass of scotch, puts on some light German electronica, and opens up a file on their computer. It’s a different file for every programmer. Sometimes they wrote it, sometimes they found it and knew they had to save it. They read over the lines, and weep at their beauty, then the tears turn bitter as they remember the rest of the files and the inevitable collapse of all that is good and true in the world.

This file is Good Code. It has sensible and consistent names for functions and variables. It’s concise. It doesn’t do anything obviously stupid. It has never had to live in the wild, or answer to a sales team. It does exactly one, mundane, specific thing, and it does it well. It was written by a single person, and never touched by another. It reads like poetry written by someone over thirty.

Every programmer starts out writing some perfect little snowflake like this. Then they’re told on Friday they need to have six hundred snow

@jonrandy
jonrandy / mostFrequent.js
Last active July 16, 2024 08:31
Most common char in string JS
const mostFrequent = s=>s.match(/(.)\1+/g)?.sort((a,b)=>b.length-a.length)[0][0]||s[0]
const mostFrequentLetter = s=>([...s].sort().join('').match(/(\w)\1+/g)?.sort((a,b)=>b.length-a.length)[0]||s.match(/\w/))[0]
@jonrandy
jonrandy / paper.css
Created June 16, 2022 06:20
Paper style css
/* https://edent.gitlab.io/paper-prototype-css/ */
@font-face {
font-family: 'Reenie Beanie';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/reeniebeanie/v16/z7NSdR76eDkaJKZJFkkjuvWxXPq1qw.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@jonrandy
jonrandy / splitIntegerByRatios.js
Created May 31, 2022 03:33
Split integer by ratios
function allocateInt(amount, ratios) {
if (ratios.length == 1) {
return [amount]
} else {
const rs = [...ratios]
const divisor = rs.reduce((a,i)=>a+i)
const thisAmount = Math.round(amount*rs.shift()/divisor)
return [
thisAmount,
...allocateInt(amount-thisAmount, rs)
@jonrandy
jonrandy / fizzBuzz.js
Last active February 20, 2023 02:50
FizzBuzz
const fizzBuzz = x=>[x,f='Fizz',b='Buzz',f+b][!(x%5)*2+!(x%3)]
const fizzBuzz = (x,f='Fizz',b='Buzz')=>[x,f,b,f+b][!(x%5)*2+!(x%3)]
const fizzBuzz = x=>({1:x,6:f="Fizz",10:b="Buzz",0:f+b}[x**4%15])
const fizzBuzz = (x,f="Fizz",b="Buzz")=>({1:x,6:f,10:b,0:f+b}[x**4%15])
@jonrandy
jonrandy / isPrimeRegex.js
Last active May 31, 2022 03:34
isPrime using regex
const isPrime = x=>!'1'.repeat(x).match(/^1?$|^(11+?)\1+$/)
@jonrandy
jonrandy / isValidCSSColour.js
Created January 5, 2022 04:09
isValidCSSColour
const isValidCSSColour = str => {
const s = new Option().style
s.color = str
return s.color != ''
}
@jonrandy
jonrandy / golf.js
Created October 25, 2021 02:50
JS Golfing Tips
var n = 53.73;
var rounded = n|0; //53
x|0 //Round x down
func`arg` //Call function with one string argument
getContext`2d`; // ^
`rgb(${r}, ${g}, ${b})` //Template literals
C //together with <canvas id=C> instead of document.getElementById("C");
1e3 //Shorter than 1000
1e-3 //Shorter than 0.001
@jonrandy
jonrandy / metho.js
Last active July 16, 2024 14:21
Add parameterised methods safely to prototypes etc.
function add(target, f, outerSyntax=false){
return f.length ?
outerSyntax ?
addProperty(target, f)
:
addWithParams(target, f)
:
addSimple(target, f)
}