Skip to content

Instantly share code, notes, and snippets.

View davidkpiano's full-sized avatar
🎹
Working on XState Dev Tools

David Khourshid davidkpiano

🎹
Working on XState Dev Tools
View GitHub Profile
@jose-mdz
jose-mdz / README.md
Last active March 23, 2026 18:22
Orthogonal Diagram Connector

Orthogonal Connectors

This algorithm returns the points that form an orthogonal path between two rectangles.

How to Use

// Define shapes
const shapeA = {left: 50,  top: 50, width: 100, height: 100};
const shapeB = {left: 200, top: 200, width: 50, height: 100};
@kentcdodds
kentcdodds / README.md
Last active March 30, 2024 11:39
user-package-stats

user-package-stats

I was poking around trying to figure out all the packages I have access to publish and got curious. So I write this little script to determine the download stats for all the packages I have publish access to.

Feel free to try it yourself. Just change the username passed to getUserDownloadStats.

By default, the stats are sorted by their average daily downloads (descending). That should give you an idea of the most "popular" package of a given user relative to how long that package has been around.

You can use it with npx like so:

@gullyn
gullyn / flappy.html
Last active November 19, 2025 15:40
Flappy bird in 205 bytes (improved!)
<body onload=z=c.getContext`2d`,setInterval(`c.width=W=150,Y<W&&P<Y&Y<P+E|9<p?z.fillText(S++${Y=`,9,9|z.fillRect(p`}*0,Y-=--M${Y+Y},P+E,9,W),P))):p=M=Y=S=6,p=p-6||(P=S%E,W)`,E=49) onclick=M=9><canvas id=c>

assert() (sometimes called invariant())

Instead of checks like:

if (value === null) {
  throw new Error("missing value")
}
doSomethingThatNeedsValue(value)
@steveruizok
steveruizok / render-state.js
Last active November 17, 2020 14:01
Render a State Designer state in the terminal.
import log from "ololog"
class Grid {
rows = []
width = 0
height = 0
chars = {
active: ["┌", "─", "┒", "┃", "┛", "━", "┕", "│"],
inactive: ["┌", "─", "┐", "│", "┘", "─", "└", "│"],
@raphaeltraviss
raphaeltraviss / inputsampler_component.ts
Last active November 7, 2020 20:20
Basic XState model for moving/striking with "looking at" vector in PhaserJS
import { assign, interpret, Machine, Interpreter, EventObject } from 'xstate';
// Phaser implicitly available
interface PawnContext {
moveTowards?: Phaser.Math.Vector2,
}
@steveruizok
steveruizok / distributeEvenly.ts
Last active November 23, 2020 19:01
A function to distribute boxes evenly along either the x or y axis.
function distributeEvenly<
T extends { x: number; y: number; height: number; width: number }
>(axis: "x" | "y", boxes: T[]) {
const mboxes = [...boxes]
const extent = axis === "x" ? "width" : "height"
mboxes.sort((a, b) => a[axis] - b[axis])
// Overall boxes span
const last = mboxes[mboxes.length - 1]
const dist = last[axis] + last[extent] - mboxes[0][axis]

Build the logic for a network quality indicator:

Requirements:

  1. Every time the REPORT_NETWORK_QUALITY event comes in, I want to assign its results to context (no matter what else happens)
  2. If the network quality is bad or worse, we want to show a warning
  3. If the network quality gets better, we want to hide the warning again
  4. The warning must show for a minimum of 2 seconds, we don't want any weird flickering.
  5. The user should be able to dismiss the warning forever using the DISMISS_NETWORK_QUALITY_WARNING. The warning should never show again.
/* So how does this work?
I'm using ANSI escape sequences to control the behavior of the terminal while
cat is outputting the text. I deliberately place these control sequences inside
comments so the C++ compiler doesn't try to treat them as code.*/
//
/*The commands in the fake code comment move the cursor to the left edge and
clear out the line, allowing the fake code to take the place of the real code.
And this explanation uses similar commands to wipe itself out too. */
//
#include <cstdio>
@cevr
cevr / editableTextMachine.ts
Last active November 14, 2021 15:25
xstate fp utils POC
import * as x from './xstate-fp';
export enum EditableTextState {
idle = 'idle',
editing = 'editing',
}
export enum EditableTextEvent {
mouseenter = 'mouseenter',
mouseleave = 'mouseleave',