DEV Community

Discussion on: Clean up your code with these tips!

Collapse
 
vojtechp profile image
VojtechP

Regex is not much readable when there are more strings.

const isLocal = domain && /^(localhost|127\.0\.0\.1)$/.test(domain);

This is for me better solution:

const localDomains = [ 'localhost', '127.0.0.1' ];
const isLocal = domain && localDomains.includes(domain);
Collapse
 
dechamp profile image
DeChamp

This is also a very nice solution. I’ll add it to the tips! Thank you. I know that for some people, regex can be a eyesore. But there is beauty in its power.

Collapse
 
miniscruff profile image
miniscruff

Not much difference but I like to use sets for these comparisons for faster lookups.

const localDomains = new Set([...])
localDomains.has(...)
Thread Thread
 
dechamp profile image
DeChamp

I actually also like this as a solution. Thank you!