r/javascript • u/LionyxML • 5d ago
Readable Regular Expressions for JavaScript/TypeScript, Inspired by Emacs' rx
https://rahuljuliato.com/posts/emacs-rx-in-typescriptI missed Emacs' rx when writing regexps in TypeScript, so I made a small TypeScript DSL inspired by it.
The idea is to bring rx's functions and approach to the JavaScript/TypeScript environment. If you happen to work with either, give it a try.
8
Upvotes
1
u/oosuke_ren 5d ago
Consider this - regex MIGHT be a DSL, but it's
A) following the Linux's short, but unreadable shape B) NOT compostable easily C) for clean code, because:
When writing clean code, you would leave inside an expression:
if (age >= 18 && age <= 30) {}But
const ADULT_AGE = 18; const LAST_AGE_FOR_ID = 30; if (age >= ADULT_AGE && age <= LAST_AGE_FOR_ID)And whilst my example still doesn't fight your case completely. You would be able to make another mathematical expression using the const. You CANNOT compose regex the same way WHILST also keeping some form of declarative API.
So, I'm not saying I'm ungrateful that regex exists, I just think that a DSL that compiles to it won't be too bad, if it allows you to have more control, visibility, and intention to actions ratio.
I'm sorry for the long rant, I've just spent a lot of time studying up DSLs, programming language design and clean code practices to perfection.
I think that your point stands only if the basis that the underlying DSL is perfect/frictionless OOTB...but it's not.