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 / return_submit_ime.js
Created July 3, 2026 03:55
How to handle 'enter' form submission WITHOUT breaking IME input (Korean, Japanese, Chinese etc.)
// put this in key event - key 229 is legacy (older browser send different 'key' for return while composing)
if (e.isComposing || e.keyCode === 229) return;
// safe to submit here if you want
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/isComposing
@jonrandy
jonrandy / promise-all.js
Created May 26, 2026 06:12
Promise.all Implementation in JS
const all = async arr => {
const results = []
for (p of arr) results.push(await p)
return results
}
@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 / observePosition.js
Created June 11, 2024 03:21
Observe Position Changes of an HTML Element
export function observePosition(element, callback) {
const trackDiv = document.createElement("div")
Object.assign(trackDiv.style, {
position: "fixed",
pointerEvents: "none",
width: "2px",
height: "2px",
})
if (element.children.length) {
element.insertBefore(trackDiv, element.firstChild)
@jonrandy
jonrandy / arrayNegativeIndexProxy.js
Created May 31, 2024 11:22
Proxy an Array to allow negative indexes (like Array.at)
const arr = [1, 2, 3, 4]
const handler = {
get(target, prop, receiver) {
// try to convert property to a number
const n = +prop
// if it's a number less than zero, return the correct item from the array
if (!isNaN(n) && n<0) {
return Reflect.get(target, target.length + n, receiver)
// otherwise continue as normal with the property
@jonrandy
jonrandy / flagEmoji.js
Created May 22, 2024 03:39
Get Flag Emoji
// pass in 2 letter country code
const getFlagEmoji = countryCode=>String.fromCodePoint(...[...countryCode.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt()))
const fib = n => {
const r5 = fib.r5 || (fib.r5=5**.5)
return Math.floor((((1+r5)/2)**n - ((1-r5)/2)**n)/r5)
}
@jonrandy
jonrandy / isPalindrome.js
Created October 31, 2023 08:34
isPalindrome
const isPalindrome = ([...s]) => ''+s == s.reverse()
function fibonacci(n) {
if (n < 0) throw RangeError("Negative arguments not implemented")
return fib(n)[0]
}
function fib(n) {
if (n) {
const [a, b] = fib(~~(n / 2)),
c = a * (b * 2n - a),
d = a * a + b * b