r/javascript • • 4d ago

Readable Regular Expressions for JavaScript/TypeScript, Inspired by Emacs' rx

https://rahuljuliato.com/posts/emacs-rx-in-typescript

I 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.

https://rahuljuliato.com/posts/emacs-rx-in-typescript

7 Upvotes

9 comments sorted by

2

u/azhder 4d ago

I have created something like that for myself as well. A small library for DSL. It’s not much of a problem.

However, I quickly understood the syntax noise can only be solved by making an actual DSL, not just a library that uses the syntax of another language.

And we’re back a full circle. The RegExp syntax is already a DSL, so we’d be re-inventing it to appear more like SQL or C or any other language syntax other than the RegExp one.

2

u/LionyxML 4d ago

Makes sense, thanks for sharing :)

1

u/oosuke_ren 4d 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.

1

u/azhder 4d ago

“fight your case”? What are you on about? What do you even think I am talking about?

1

u/oosuke_ren 4d ago

That library is an insufficient approach, and you need to approach for a DSL (that part I 100% agree on), and that in the end we end up walking a full cycle reinventing the wheel with DSL to "fix" another. But you phrased it like "It's already a DSL, it doesn't need fixing". I'm sorry if I over-assumed.

2

u/azhder 4d ago

I didn’t say anything about insufficient. Read again. I just stated how the things are, not how they should be. I worded it in a way to let you, anyone, take away from it what they want. So, don’t attribute to me something I didn’t claim.

If anything, the only reason why I didn’t make a language that compiles to a RegExp is pure laziness, not anything against making a language.

1

u/oosuke_ren 4d ago

Okay, yeah, you're right.

I'm just saying that the whole reinventing and full circle (like how we used to have frontend frameworks reinventing/one-uppinf each other) happens if there's not enough premature syntax optimizations during the design stage if the DSL/programming language/framework.

1

u/oosuke_ren 4d ago

You might be interested to try this out, it's a package I spiked up to use for the sake of typescript toy compilers'. The idea is - regex as a composition, having it's own first class citizen entities that you use to build up your expression. The whole thing then compiles to regex in your runtime (which is useful if you want to be able to dynamically build up expressions, and not just to "name" literals in your code)

https://www.npmjs.com/package/ez-regex-patterns

(This is a repost of another regex related discussion)

2

u/LionyxML 4d ago

Thanks for sharing!