4 ms·
Is the javascript regex literal or builtin RegExp type not sufficient for your needs? What are they missing? https://developer.mozilla.org/en-US/docs/Web/Java
by reificator 6y ago
Is the javascript regex literal or builtin RegExp type not sufficient for your needs? What are they missing?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- tengbretson 6y agoI think the op is asking for actual type refinement by a regex condition.
- reificator 6y agoThat would make a lot of sense given the topic at hand. Thanks for the clarification.
- chadlavi 6y agoespecially as a library maintainer, I would really love to be able to say, for example, "you have to pass an email address to this prop" or "you have to pass a US phone number"
- pseudonom- 6y agoThis can be fairly smoothly achieved already: https://dev.to/gcanti/functional-design-smart-constructors-14nb https://dev.to/gcanti/functional-design-smart-constructors-1...
- chadlavi 6y agoYeah I'm familiar with branded types, but they're cumbersome and require API changes beyond just type narrowing to implement. I just wish they'd figure out how to implement strong types in typescript without js side effects.
- reificator 6y agoSo you're the one I should blame whenever some regex fails to validate my perfectly standards compliant email address! I get very frustrated when I sign up for storefronts using an email with a `.dev` address and some wanker decides that I shouldn't be allowed to give them money because my email address doesn't end in `@gmail.com`. Stop this. Regex is not the right tool for this job. If you're going to use the regex hammer at least use the full standards compliant one shown here: https://stackoverflow.com/a/201378 https://stackoverflow.com/a/201378 > The problem with regular expressions is that telling someone that their perfectly valid e-mail address is invalid (a false positive) because your regular expression can't handle it is just rude and impolite from the user's perspective. A state engine for the purpose can both validate and even correct e-mail addresses that would otherwise be considered invalid as it disassembles the e-mail address according to each RFC.
- chadlavi 6y agoNo, it's an inner-source library, but thanks for expressing your generalized frustration at me! But also I would use a standards-compliant regex. I also hate it when people don't implement standards to spec.
- spicybright 6y agoOr if you're even too lazy for that, just match `.+@.+` and handle errors properly if sending an email fails (which you'll obviously have to handle anyways as a valid email address might not be in service)