Devansh Jethmalani Profile picture
Rebellious, Irrational, Untamed. I make softwares, and things that help making them. Sometimes I pen my musings. 22.
Mar 20, 2023 7 tweets 3 min read
I made a runtime type checker and here's why I think it's cool...npm.im/@sthir/runtime… I'll also compare it with zod where applicable as it seems to be the most popular solution and my TL keeps hyping it.

1. Negligible Size

baseline: 137B
sthir: 1250B (×9 bigger, 1750B without tree-shaking)
zod: 10570B (×77 bigger, same without tree-shaking) https://bundlejs.com/?text=%22const+isUser+%3D+x+%3D%3E%5Cn++typeof+x+%3D%3D%3D+%5C%22object%5C%22+%26%26+x+%21%3D%3D+null+%26%5Cn++typeof+x.name+%3D%3D%3D+%5C%22string%5C%22+%26%26%5Cn++typeof+x.email+%3D%3D%3D+%5C%22string%5C%22+%26%26+x.email.includes%28%5C%22%40%5C%22%29%5Cn%5Cnconsole.log%28isUser%28%7B+name%3A+%5C%22Devansh%5C%22%2C+email%3A+%5C%22foo%5C%22+%7D%29%29%22
https://bundlejs.com/?q=%28import%29https%3A%2F%2Funpkg.com%2F%40sthir%2Fruntime-checker%2Fdist%2Fsthir-runtime-checker.esm.js&treeshake=%5B*+as+t%5D&text=%22const+isUser+%3D+x+%3D%3E%5Cn++t.is%28x%2C+t.object%28%7B%5Cn++++name%3A+t.string%2C%5Cn++++email%3A+t.then%28t.string%2C+t.predicate%28s+%3D%3E+s.includes%28%5C%22%40%5C%22%29%29%29%5Cn++%7D%29%29%5Cn%5Cnconsole.log%28isUser%28%7B+name%3A+%5C%22Devansh%5C%22%2C+email%3A+%5C%22foo%5C%22+%7D%29%29%22
https://bundlejs.com/?q=%28import%29https%3A%2F%2Funpkg.com%2Fzod%2Flib%2Findex.mjs&treeshake=%5B*+as+z%5D&text=%22const+isUser+%3D+x+%3D%3E%5Cn++z.object%28%7B%5Cn++++name%3A+z.string%28%29%2C%5Cn++++email%3A+z.string%28%29.refine%28s+%3D%3E+s.includes%28%5C%22%40%5C%22%29%29%5Cn++%7D%29.safeParse%28x%29.success%5Cn%5Cnconsole.log%28isUser%28%7B+name%3A+%5C%22Devansh%5C%22%2C+email%3A+%5C%22foo%5C%22+%7D%29%29%22
Mar 19, 2022 4 tweets 1 min read
Interviewer: Map an array
Me: xs.flatMap(x => [f(x)])

Interviewer: Ugh okay... How about filtering an array
Me: xs.flatMap(x => f(x) ? [x] : [])

Interviewer: ... concat concat two arrays
Me: [as, bs].flatMap(x => x) Interviewer: SPLICE. Like xs.splice(n, m, ...a)?

Me: xs.flatMap((x, i) =>
0 <= i && i <= n - 1 ? [x] :
n <= i && i <= n + m ? []
i === n + m + 1 ? [a, x] :
[x]
)
Jan 3, 2022 14 tweets 6 min read
🎁 New Year's Gift for Y'all

❓ `@​sthir/predicate` — An eDSL to write typed predicates

🧙🏻‍♂️ Write your logical expression in a JS-like eDSL and get the typed predicate inferred.

🔗

👇🏻 Let me explain...github.com/devanshj/sthir… We often write predicates, for example while filtering an array. But its type (as in `x is NarrowedX`) doesn't get inferred... Image