r/badcode • • Mar 01 '22

js FE developer checks for mobile for responsivity

Post image
484 Upvotes

43 comments sorted by

105

u/Voltra_Neo Mar 01 '22

Given what y'all do with this, I might as well publish my browser "detector" on npm

70

u/Donni3D4rko Mar 01 '22

It's high performance regex

75

u/Voltra_Neo Mar 01 '22

High performance brain hazard

31

u/RandomSwissBuenzli Mar 01 '22

High performance eye cancer

2

u/Voltra_Neo Mar 01 '22

My thoughts exactly

13

u/RavynousHunter Mar 01 '22

Its damn near SCP levels of outright cognitohazard.

16

u/Voltra_Neo Mar 01 '22

I have no idea what any of this meant, and I understand GCC C++ compile errors

→ More replies (3)

94

u/Mancobbler Mar 01 '22

At this point if I include any regex in my code, I also include a link to regex101.com with a bunch of examples of what it’s supposed to match.

62

u/nwash57 Mar 01 '22

You could just put the examples as unit tests lol

54

u/Zarokima Mar 01 '22

Bold of you to assume they're unit testing.

2

u/nwash57 Mar 01 '22

No time like the present!

3

u/[deleted] Mar 01 '22

Good luck testing all those cases. lol

8

u/nwash57 Mar 01 '22

You don't have to do all of them... Some coverage is better than nothing. If youre going to link to a regex101 page of examples like the above poster, you could just loop through and verify matches on those same examples.

1

u/Mancobbler Mar 01 '22

Unit testing? Never heard of it.

You’re right though. I still think providing that is helpful if you’re trying to understand the regex, since the site gives you a great breakdown

2

u/rgnkn Mar 04 '22

First you drink a liter of beer.

Then you drink a gallon of beer.

After that you try to find out which experience was better.

That's unit testing - so simple!

48

u/nekokattt Mar 01 '22

Code like this is why everyone is shitting themselves that chrome and firefox changing from v99 to v100 is going to break loads of stuff.

4

u/supersharp Mar 02 '22

It's the V1H crisis

1

u/killersquirel11 Mar 03 '22

Why do you think Windows went from 8 to 10?

2

u/nekokattt Mar 03 '22

It didn't, it went to 8.1 which was technically 9, then to 10

29

u/pavilionhp_ learning from the best Mar 01 '22

This is from detectmobilebrowsers.com and is available in multiple other languages if you’d all like to know

23

u/RichCorinthian Mar 01 '22

It's OK, I'm sure there's a whole suite of unit tests to make sure it's correct and to prevent problems if you ever need to maintain or expand it. /s

4

u/RyanNerd shameless Mar 01 '22

The poor human transcriber. Smh.

2

u/pavilionhp_ learning from the best Mar 01 '22

They can copy most of it from here at least http://detectmobilebrowsers.com/

3

u/Budgiebrain222 Mar 01 '22

This is that regex stuff?

3

u/Lich_Hegemon Mar 02 '22

Regex was a mistake

2

u/ftgander Mar 01 '22

I love Regex but this is too far

2

u/tills1993 Mar 02 '22

What the hell is the point of the iife here? Everything is already scoped to isMobile.

2

u/[deleted] Mar 02 '22

// @ts-ignore

2

u/LeagueJunior9782 Mar 09 '22

this isn't normal regex, this is advanced regex!

Now excuse me, i think my eyes bleed.

44

u/[deleted] Mar 01 '22

[removed] — view removed comment

24

u/v_maria Mar 01 '22

This is awful but ugly problems require ugly solutions

21

u/[deleted] Mar 01 '22

[removed] — view removed comment

16

u/kristallnachte Mar 01 '22

Even better is "why the heck does it matter if they're on a phone or not?"

That's just not something useful.

What is useful is things like viewport size and input device.

1

u/jmona789 Mar 02 '22

Could be for a nav where sub items show on hover. Since hover doesn't make sense on a touch screen device

1

u/kristallnachte Mar 02 '22

That's still an input issue, not a device issue.

Hover works on phones when I use a mouse.

1

u/jmona789 Mar 02 '22

Well sure it works when you use a mouse but your users aren't going to be using a mouse on their phone.

→ More replies (1)

1

u/RobDoingStuff Mar 03 '22

But wouldn't a simple media query for screen size accurately accommodate like 99% of devices, anyway? Phones and tablets usually have significantly smaller resolutions than PCs.