Skip to content

Instantly share code, notes, and snippets.

View lhorie's full-sized avatar

Leo Horie lhorie

View GitHub Profile
lhorie /
Last active June 12, 2020 12:59
Write better tests with one neat trick

Write better tests with one neat trick

TL:DR; assert your input

If you write non-trivial code, inevitably you'll run into a situation where setting up a test is not super readable. Maybe you have to mock a bunch of things, or the input data is a data structure that spans dozens of LOC, or maybe you are testing with a large-ish fixture that was defined in another file because linting or whatever.

Due to the nature of my job, I happen to read a lot of other people's code, so you can imagine how mildly annoying it is when I'm code reviewing something, and the test looks something like this:

test('codemod does what it needs to', async () => {
lhorie / reduced-example.zig
Last active March 9, 2021 16:52
zig async recursion
View reduced-example.zig
// test.zig
const std = @import("std");
pub const io_mode = .evented;
pub fn main() void {
fn parse(file: []const u8) void {
View gist:95d44b01759ac172332a59af4de4a478
This file has been truncated, but you can view the full file.
% yarn test
yarn run v1.22.10
$ fusion test --configPath=./jest.config.js
Determining test suites to run...Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
Why you should do it regularly:
Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
lhorie /
Last active February 15, 2022 03:48
Dear Javascript developer, OOP and FP are not opposites

Dear Javascript developer, OOP and FP are not opposites

In the Javascript community, I sometimes hear people say they prefer FP over OOP, and that FP is good and OOP is bad. The line of argument implies there's an FP vs OOP dichotomy, and that one should aim to use one and avoid the other.

I like to quote the Qc-Na parable because it illustrates a subtle but important insight about the two paradigms: they are not opposites and in fact have a lot in common. Here's the parable for those who haven't seen it:

The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."

Chastised, Anton took his leave from his master and returned to his cell, int

lhorie /
Last active March 24, 2022 02:02
Friday Fun: Svelte-like variable reactivity in 7LOC with with Proxy

Friday Fun: Svelte-like variable reactivity in 7LOC with with Proxy

Every once in a blue moon, I like to hack up some crazy toy proof of concept to get away from everyday's stiff serious production-ready coding. This morning, I decided to mess around with an idea of implementing something similar to Svelte's reactive variables, but using pure Javascript.

So here's that godawful eye-bleeding fun hack: a 7-line "svelte" (needless to say, it doesn't do nearly enough to be useful in the real world and breaks just about every best practice rule in the book because why the hell not)

Can you figure out why this works? Any ideas to make it more devilish/clever/insane are welcome :)

View parser.js
var readtable = {
"(": form,
" ": space, "\t": space, "\n": space, "\r": space,
var white = " \t\n\r"
var atomEnd = " \t\n\r);"
var formEnd = ")"
var escape = "\\"
function parse(s) {
s.marker = s.cursor
lhorie /
Created June 9, 2022 20:27
flow chart example
flowchart LR

A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Two| E[Result 2]
View gist:1204893c2f0e3ba3c14ac204b2df4b24
console.log(`%c ________________________________________
< mooooooooooooooooooooooooooooooooooooo >
\\ ^__^
\\ (oo)\\_______
(__)\\ )\\/\\
||----w |
|| ||`, "font-family:monospace")
lhorie /
Last active November 14, 2022 23:21
What's the longest keyword sequence in Javascript?