Skip to content

Instantly share code, notes, and snippets.

@JLarky
JLarky / README.md
Created August 10, 2022 06:08
Make git diff work on binary bun lock files, i.e. bun.lockb

Hi, I hope this finds you well

Here's what I promise you:

image

Here's how to get it:

Set gitattributes somewhere in your repo

@JLarky
JLarky / README.md
Last active October 29, 2024 14:16
Ultimate example of react context hook with nice type-safe (TypeScript) wrappers and reduced boilerplate by using `ReturnType`
@JLarky
JLarky / README.md
Created August 1, 2022 17:29
Ultimate example of solidjs context hook with nice type-safe (TypeScript) wrappers and reduced boilerplate by using `ReturnType`
@JLarky
JLarky / Embed.tsx
Created September 10, 2024 06:21
React version of Embed block for react-sdk (sdk v2) for Builder.io
// https://github.com/BuilderIO/builder/blob/21c96eff31434f51adc9ec3e6071256572cd261b/packages/sdks/src/blocks/embed/embed.lite.tsx
import { useEffect, useRef, useState } from 'react';
export interface EmbedProps {
content: string;
}
const SCRIPT_MIME_TYPES = ['text/javascript', 'application/javascript', 'application/ecmascript'];
function isJsScript(script: HTMLScriptElement) {
@JLarky
JLarky / snippet.js
Created September 6, 2024 18:28
How to decode Astro props for <astro-island> element
((raw)=>{let i = { 0: t => m(t), 1: t => a(t), 2: t => new RegExp(t), 3: t => new Date(t), 4: t => new Map(a(t)), 5: t => new Set(a(t)), 6: t => BigInt(t), 7: t => new URL(t), 8: t => new Uint8Array(t), 9: t => new Uint16Array(t), 10: t => new Uint32Array(t) } , o = t => { let[l,e] = t; return l in i ? i[l](e) : void 0 } , a = t => t.map(o) , m = t => typeof t != "object" || t === null ? t : Object.fromEntries(Object.entries(t).map( ([l,e]) => [l, o(e)])); return m(raw)})(JSON.parse($0.getAttribute('props')))
@JLarky
JLarky / ServerOnly.tsx
Created August 22, 2024 00:30
How to render component only on the server
// https://x.com/JLarky/status/1826404005876301882
export function FancyStuff(props: { getFromServer?: undefined | (() => string) }) {
const [mousePos, setMousePos] = useState('0, 0');
useEffect(() => {
document.addEventListener('mousemove', (e) => {
setMousePos(`${e.clientX}, ${e.clientY}`);
});
}, []);
return (
@JLarky
JLarky / README.md
Created June 5, 2024 06:18
Is it possible to apply CSS transitions inline? Yes
@JLarky
JLarky / 0_README.md
Last active March 18, 2024 04:44
Turn string to number

Get this script

Download the file

curl https://gist.githubusercontent.com/JLarky/5fb7cc00112bdb6c79759298a1becb6b/raw/f84118472bbb6ced9a97e30a49ceddc8bd32357c/str2num.js -o ./str2num.js

Make it executable

#!/bin/sh
/*/.this-doesnt-exist 2>/dev/null
## Please do not edit this part of the script, this is a loader created by "npx bun-self"
if ! [ -x "$(command -v bun)" ]; then
## it's possible that bun is installed but not in the PATH, let's check if BUN_INSTALL is set
if [ -z "$BUN_INSTALL" ]; then
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
fi
@JLarky
JLarky / .prettierrc.js
Last active February 13, 2024 12:18
Use prettier formatting with Astro (pnpm)
/**
* @type {import('prettier').Options}
*/
module.exports = {
plugins: [require.resolve('prettier-plugin-astro')],
overrides: [
{
files: '**/*.astro',
options: { parser: 'astro' }