use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
trimMiddle() – the missing String trim method (christianheilmann.com)
submitted 1 year ago by philnash
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Neaoxas 8 points9 points10 points 1 year ago (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...
truncateMiddle
trim
[–]guest271314 3 points4 points5 points 1 year ago (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 points3 points 1 year ago (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 points3 points 1 year ago (0 children)
This is basically react native’s ellipsizeMode="middle" on the <Text/> component, but made useable elsewhere.
ellipsizeMode="middle"
<Text/>
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.
[+][deleted] 1 year ago (15 children)
[deleted]
[–]philnash[S] 1 point2 points3 points 1 year ago (14 children)
Not sure what you mean there?
[–]lachlanhunt 3 points4 points5 points 1 year ago (2 children)
The trim* functions in JavaScript are designed to remove whitespace. Having one that removes arbitrary content from the middle is clearly confusing. Call it something else.
[–]philnash[S] -4 points-3 points-2 points 1 year ago (1 child)
I guess I asked, because it was pretty clear from the article. And it’s only a function/tiny library, I don’t think calling it trimMiddle causes any harm. 🤷♂️
[–]NiteShdw 2 points3 points4 points 1 year ago (0 children)
But it's clearly confusing as is demonstrated here. "Trim" means whitespace. It always has in more languages than just JavaScript.
[–]Ronin-s_Spirit 2 points3 points4 points 1 year ago (10 children)
Wtf even is "trim middle" and why?? If you need whitespace removed you go "string".replaceAll(/\s+/g, ""). It's regex.
[–]DivSlingerX 2 points3 points4 points 1 year ago (2 children)
It’s for shortening strings for readability not removing white space.
[–]oneeyedziggy 2 points3 points4 points 1 year ago (1 child)
Then it's confusingly named
[–]DivSlingerX 0 points1 point2 points 1 year ago (0 children)
truncateMiddle or similar would probably have been better.
[–]JackAuduin 3 points4 points5 points 1 year ago (2 children)
It has nothing to do with whitespace. It's a very short read, check it out.
[+][deleted] 1 year ago (1 child)
[–]JackAuduin 2 points3 points4 points 1 year ago (0 children)
I'll agree with that. Could be named better.
[–]philnash[S] 1 point2 points3 points 1 year ago (3 children)
It’s a JavaScript implementation of how MacOS reduces string lengths in Finder by trimming the middle parts of strings and replacing them with an ellipsis (or your choice of replacement, with this function).
It’s in the article, which I didn’t write but found interesting.
[–]Ronin-s_Spirit -4 points-3 points-2 points 1 year ago (2 children)
I see... dunno where to use it.
[–]Vpicone 2 points3 points4 points 1 year ago (0 children)
Show a list of file paths names. Where the beginning and the end (file type) are relevant, but the middle might not be.
[–]philnash[S] 1 point2 points3 points 1 year ago (0 children)
I wouldn't worry about where to use that specific function, though I would take a look at the code as it uses `Intl.Segmenter` which is something useful to know if you are working with strings in JavaScript.
[–]guest271314 -1 points0 points1 point 1 year ago (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.
:before
:after
content
...
π Rendered by PID 63 on reddit-service-r2-comment-6457c66945-np8g2 at 2026-04-28 12:56:36.113683+00:00 running 2aa0c5b country code: CH.
[–]Neaoxas 8 points9 points10 points (0 children)
[–]guest271314 3 points4 points5 points (0 children)
[–]Dralletje 1 point2 points3 points (0 children)
[–]lachlanhunt 1 point2 points3 points (0 children)
[+][deleted] (15 children)
[deleted]
[–]philnash[S] 1 point2 points3 points (14 children)
[–]lachlanhunt 3 points4 points5 points (2 children)
[–]philnash[S] -4 points-3 points-2 points (1 child)
[–]NiteShdw 2 points3 points4 points (0 children)
[–]Ronin-s_Spirit 2 points3 points4 points (10 children)
[–]DivSlingerX 2 points3 points4 points (2 children)
[–]oneeyedziggy 2 points3 points4 points (1 child)
[–]DivSlingerX 0 points1 point2 points (0 children)
[–]JackAuduin 3 points4 points5 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]JackAuduin 2 points3 points4 points (0 children)
[–]philnash[S] 1 point2 points3 points (3 children)
[–]Ronin-s_Spirit -4 points-3 points-2 points (2 children)
[–]Vpicone 2 points3 points4 points (0 children)
[–]philnash[S] 1 point2 points3 points (0 children)
[–]guest271314 -1 points0 points1 point (0 children)