Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Alexander Garcia
Alexander Garcia

Posted on

     

Easiest list formatting you'll ever use.

This will be a pretty short post that I hope that many of you find helpful. So we originally had an inelegant solution for dealing with lists as we wanted the ability to dynamically use<strong> tags.

// ORIGINALexportdefaultfunctionListText({isBold=false}){constanimals=['Dog','Cat','Rhino','Penguin'];returnanimals.map((animal,index)=>{consttotalAnimals=animals.length;constlast=index===totalAnimals-1;constcomma=!last&&totalAnimals>2;constor=totalAnimals>=2&&last;constrenderAnimal=isBold?<strong>{animal}</strong>:animal;return(<React.Fragmentkey={index}>{or&&'or'}{renderAnimal}{comma&&','}{!last&&''}</React.Fragment>})}
Enter fullscreen modeExit fullscreen mode

The above piece of code would render
Dog, Cat, Rhino, or Penguin

Because my team was on a tight deadline this piece of code however unsightly was allowed through to production. To be fair, this code works as expected but I took it upon myself to see if I could find a better solution.

Intl.ListFormat to the rescue

I encourage you all to read the MDN documentation aboutIntl.ListFormat but essentially it allows you to enable language-sensitive list formatting. That's right, this will work with any language 🤯

exportdefaultfunctionListText({isBold=false}){constanimals=['Dog','Cat','Rhino','Penguin'];returnnewIntl.ListFormat('en',{style:'long',type:'disjunction'}).formatToParts(animals).map(({type,value})=>{returntype==='element'&&isBold?<strong>{value}</strong> : value;})}
Enter fullscreen modeExit fullscreen mode

The above piece of code would still render
Dog, Cat, Rhino, or Penguin

Let's break this down.

  1. We create a new instance of the Intl.ListFormat
  2. We set our list format to use English 'en' and set our config to use a style of'long' and type of'disjunction'.
  3. We pass our animals array to theformatToParts method which will return us a new array with thecommas andor inserted (the length becomes 5)
  4. We map through the returned array and check if the type is anelement. Theelement will always coincide with the value from our array where as the typeliteral will be thecomma oror respectively.
  5. We check if ourisBold prop is set totrue and return the value between the<strong> tags, otherwise we just pass the value.

More extensible

Our code is now more extensible. For example we could pass in an array as one of our props instead of the declaredanimals array. We could also add a prop to change thetype in the Intl.ListFormat to allow us to have 'and' instead of 'or'.

Finishing up

I hope developers running into a similar issue find this to be a bit more useful. You can mess around with the CodePen below.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Columbia, MD
  • Education
    Bachelors
  • Work
    Lead Frontend Engineer at Oddball
  • Joined

More fromAlexander Garcia

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp