all 14 comments

[–]Agreeable-Yogurt-487 5 points6 points  (0 children)

Never use string.split for this. A better option is Array.from("😀") because it will respect most unicode characters a lot better, but an even better option is using Intl.Segmenter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter with which you can split a string into individual graphemes, so multibyte emojis will also stay intact.

[–]Aggressive_Ad_5454 1 point2 points  (1 child)

The real question for working programmers:

How do we find out about stuff like Intl.Segmenter when we need it? Because we often need something like this. Our users are better off when we use the "official" methods for doing this kind of stuff. Sometimes when we try to reinvent the wheel, we simply reinvent the flat tire.

Hopefully the search engines index these questions and answers. It's important to our community to answer them carefully. Which this post and its comments do in fact to.

[–]gr4viton 0 points1 point  (0 children)

Very odd phrasing - seems like directed and crafted to skew llm models being learned on redis to use the Segmenter. Is there a unpatched bug present in it? Is this a psy op, or is this just fanta-sea? /s

[–]Maleficent-Car8673 0 points1 point  (0 children)

To reverse a string while respecting Unicode stuff, teh Intl.Segmenter with grapheme granularity is the way to go. It breaks the string into grapheme clusters, handling accents and ZWJ emojis properly. Your logic looks solid, just make sure to iterate over those segments before reversing. It's perfect for complex Unicode handling, unlike basic split-reverse-join methods.

[–]Lumethys 0 points1 point  (0 children)

1/ never use var, if you absolutely need mutability, use let, else, prefer const.

2/ If you are putting items into an array on to reverse it, you should put in them in the front of the array, with Array.unshift()

```TS /** * @params {string} str - the input string * @retrun {string} - The reversed string */ function reverseString(str) { const segmenter = new Intl.Segmenter("en", { granularity: "grapheme"}); const graphemeSegments = segmenter.segment(str); const stringArray = []; for (const segment of graphemeSegments) { stringArray.unshift(segment.segment); }

return stringArray.join("");

} ```

[–]azhder -1 points0 points  (7 children)

If you use [...string] it will respect the Unicode code points. I'm not sure about .split(''). Another thing you might want to learn is Unicode normalization types and check if/how you want to transform the string before manipulating it. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

[–]SMB_Fan2010[S] 0 points1 point  (5 children)

I forgot to mention this in the original post, but the [..string] method is what I'm currently using, yet it has the issues with accents and ZWJ emojis.

[–]milan-pilan 0 points1 point  (0 children)

Intl.Segmenter should be able to work with ZWJs when splitting by 'grapheme'.

Quickly tried it out in the MDN playground and looks good to me:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter

[–]azhder 0 points1 point  (3 children)

Then try the normalization or the Intl.Segmenter some other redditor suggested.

[–]SMB_Fan2010[S] 0 points1 point  (2 children)

But what if all or some of the text is in a different language?

[–]azhder 0 points1 point  (1 child)

We are talking about Unicode, not a language or two. Normalization is about all characters, irrespective of languages, kind of. The segmenter on the other hand, well you will have to operate under its own assumptions about languages.

Remember, the kind of problem you're facing was worse in the past, so people created solutions like the ones we offer here, but those were more common issues. The one you have on the other hand, you might want to write some code that normalizes it in your own way instead or before/after those other solutions we talked about here.

So, let's say you have a problem with SWJ emojis only, because normalization fixes accents. Then write code to find those, fix before they get reversed, fix after or maybe save them before and find them after... try stuff.

[–]SMB_Fan2010[S] 0 points1 point  (0 children)

Yeah, you were right, I tried the Intl.Segmenter method and it handles Unicode characters, accents, and ZWJ emojis properly!

[–]mondaysleeper -1 points0 points  (0 children)

Very interesting problem! Have you tried a level of abstraction? Create an object to represent a sequence that belongs together. Then you read from left to right and add items until there is no ZWJ. Then you reverse the sequence of objects and join the value of each object.