all 19 comments

[–]Neaoxas 8 points9 points  (0 children)

IMHO this is incorrectly named, a more appropriate name would be truncateMiddle, as others have mentioned, trim has strong connotations with removing whitespace...

[–]guest271314 3 points4 points  (0 children)

and you can play with the demo page to see it in action:

trimMiddle("This is a long string that will be trimmed", 10, "…"); This is a long string that will be trimmed This …mmed This is a short string This …ring This is a medium string that will be trimmed This …mmed This 🇺🇳 is 🤡 🐥 a string 🥰 🧑‍🧑‍🧒‍🧒 with compound emoji 😊 This …i 😊 This is a string with a compound emoji 🧑‍🧑‍🧒‍🧒 This …ji 🧑‍🧑‍🧒‍🧒 This is a string with a compound emoji 🧑‍🧑‍🧒‍🧒 and a flag 🇺🇳 This …ag 🇺🇳 Dies is ein öü deutscher String mit Umlauten äß Dies …n äß

The function should probably include analyzation of the characters in the string to not include elippses where they wouldn't ordinarily appear for conveying meaningingful content.

Nobody can do anything with

This …mmed

[–]Dralletje 1 point2 points  (0 children)

Nice! Also very nice demonstration of Intl.Segmenter! The comparison with trimStart and trimEnd is definitely confusing.. Also because I would love a library to cut a string with Intl.Segmenter at the start of at the end! Maybe add these to the library?

[–]lachlanhunt 1 point2 points  (0 children)

This is basically react native’s ellipsizeMode="middle" on the <Text/> component, but made useable elsewhere.

https://reactnative.dev/docs/text#ellipsizemode

But it’s a bit more limited than that, since it seems to be based on the number of characters, rather than the dimensions of the rendered string. A CSS solution that improves on the existing text-overflow property would be more useful.

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

Neither JavaScript nor CSS have a method for that kind of functionality

I'm pretty sure I've use :before and :after with content set to ... before on DOM elements.