Skip to content

Instantly share code, notes, and snippets.

View jonrandy's full-sized avatar
✴️
Sentient

Jon Randy jonrandy

✴️
Sentient
View GitHub Profile
@jonrandy
jonrandy / closure.js
Created September 6, 2025 08:44
Schroedinger's Closure
function makeFunction() {
let myVar = 123
return function (s) {
console.log(eval(s))
}
}
const func = makeFunction()
@jonrandy
jonrandy / hashmod.html
Created April 8, 2025 06:45
HTML Hash Modules (self contained JS modules in an HTML file)
<!doctype html>
<html lang="en">
<body>
<script type="hash-module" id="sum">
export const sum = (a, b) => a + b;
</script>
<!-- All hash modules need to go before this script. -->
<!-- This cant be a type="module", it must be executed immediately. -->
<script>
@jonrandy
jonrandy / range.js
Last active July 16, 2024 14:21
Range
const to = Symbol('number to')
// Number.prototype[to] = function to(end) {
// let [start, tend, rev] = (this>end) ? [end, this, true] : [this, end, false], arr = []
// for (let i=start; i<=tend; i++) arr.push(i)
// return rev ? arr.sort(_=>1) : arr
// }
Number.prototype[to] = function to(end, {step}={step:this<=end?1:-1}) {
let arr = [], i, d = end>this
@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)
}
@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 / 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 / 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 / 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 / carousel.html
Created November 15, 2022 02:48
Carousel without JS
@jonrandy
jonrandy / fisher.js
Last active July 16, 2024 08:29
Fisher Yates Shuffle
function shuffle(array) {
let a = [...array], m = a.length, i
while (m) {
// Pick a remaining element
i = ~~(Math.random() * m--);
// Swap it with the current element
[a[m], a[i]] = [a[i], a[m]]
}
return a
}