
Wait, What?? LOCALES ARE OUT HERE WILDING!
Sunday, 29th of September 2024
So picture this:
I’m knee-deep in a project, everything’s cruising, and then 💣 BOOM 💣 —surprise! JavaScript’s throwing me a curveball like it’s the World Series. And the culprit? Locale-specific formatting! Who even thinks about that, right??
I’m using this fancy Intl.NumberFormat
API with se-SE
(yep, Swedish for Sweden because we’re classy like that) to format prices - Not only Sweden BTW but a little drama hurts nobody. Sounds easy-peasy, but plot twist—my test case decided to go rogue on me. It’s like,
"Nah fam, we’re not doing that."
Why? Turns out, Sweden likes to keep their spaces “non-breaking” (aka \u00A0
), while I was out here expecting the old boring regular space (\u0020
). Yeah, apparently spaces have personalities now.
The Drama:
I was out here hardcoding a string with a regular space, blissfully unaware that Sweden’s spaces play hard to get. Lesson learned: just because it looks like a space, doesn’t mean it’s the same space. Seriously, who knew a tiny space could throw such shade?
Cue the fix:
I hit that test with a regex makeover to chill with both space types, ‘cause we’re inclusive like that. Check it out:
expect(formattedPrice).toMatch(/1(\s|\u00A0)200,50/);
The Real Tea:
When you’re out here internationalizing 🌐 like a pro, remember—what you see ain’t always what you get. Even something as basic as a space can have a whole diva moment depending on the locale.
Stay woke, folks! 👐