til.duyet.net
  • 🤪Today I Learned
  • Data Engineering
    • ☁️AWS
      • Make an NVMe volume available for use on AWS EC2
      • AWS EMR
        • S3 Dist CP
        • Manage services
        • EMR - Tez
        • Issues
    • ⚒️Tools
    • 💻Shell
      • MacOS's Touch ID on Terminal
      • Using `sed` to find and replace in file
      • Merging contents of multiple .csv files into single .csv file
    • env from ConfigMap or Secrets
    • ☸️Kubernetes, Helm, Kustomize
      • initContainer to download file to pod
      • Kustomize: omission of resources
      • K8S: Services and Labels
      • K8S: PVC stuck in status “Terminating”
      • K8S: Port Forward
      • K8S: Pull an Image from a Private Registry
      • Happy helm
      • Helm: render manifest locally
      • Helm: Public Helm chart repository with GitHub Pages
    • 🔄Apache Airflow
      • Extend official Docker image
      • Generate offline SQL upgrade script
      • Airflow in Docker
      • Mastering Airflow UI
      • Best Practices for Airflow and ETLs
      • Airflow in Docker Compose
      • Useful SQL queries for Apache Airflow
    • 🐳Docker
      • "Distroless" Docker Images
      • Docker cleanup
      • Optimize the Docker Image Size
      • The best Docker base image for Python
  • Database
    • Google BigQuery
      • BigQuery Cancel Running Query
      • BigQuery - Split string and get the first part
      • BigQuery - UNNEST in SELECT
      • Bigquery - Sample queries for audiences based
      • BigQuery cookbook for Google Analytics Exported Data
    • Apache Hive
      • Hive - SHOW schemas/tables/create
    • AWS Redshift / Postgres
      • Amazon Redshift Utilities
      • Postgres - Index Summary
      • Postgres - List tables
      • Redshift - GRANT
      • Redshift - tables and their owners
      • Redshift - Check the table size
    • Presto
      • Aliyun Data Lake Analytics (Presto) - Add partition on non-existing location
      • AWS Athena - Add Partition
  • Programming
    • 🍪Rust
      • cheats.rs
      • Imperative vs Declarative
      • Generate Struct from JSON
    • 🐍Python
      • YAML config file with environment variables
      • date_range_generator
      • get_all_s3_keys
      • Pipenv
    • 👻Golang
      • Functions and Methods in Go?
      • Convert JSON to Go struct
    • 💎Javascript / Typescript
      • Intl.ListFormat
    • ✨FE / React
      • Beautiful icons, images, ..
      • Flexbox
      • Create hook to inject JS script
  • Unix
    • Git - Pretty git branch graphs
    • Checking files in Docker build context
    • Bash get the directory of the current script
    • Vim
    • Find and replace
  • Miscellaneous
    • [Fig] Single machine and distributed system structure
    • Deploying Machine Learning Models at Scale
    • Bypass a Chrome certificate/HSTS error
    • Articles
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. Programming
  2. Javascript / Typescript

Intl.ListFormat

Convert arrays to human-readable lists

const books = [
    'Harry Potter',
    'Bhagavad Gita',
    'The Alchemist',
    'Birthday Girl'
]

const listFormatter1 = new Intl.ListFormat('en', {
    style: 'long',
    type: 'conjunction'
})
console.log(listFormatter1.format(books));
// Harry Potter, Bhagavad Gita, The Alchemist, and Birthday Girl

const listFormatter2 = new Intl.ListFormat('en-GB', {
    style: 'short',
    type: 'disjunction'
})
console.log(listFormatter2.format(books));
// Harry Potter, Bhagavad Gita, The Alchemist, or Birthday Girl

The first part is to create the formatter using the Intl.ListFormat method. This method accepts two optional parameters.

  • locales - A string with a BCP 47 language tag, or an array of such strings.

  • options - An object with some or all of the following properties:

    • localeMatcher - The locale matching algorithm to use. Possible values are lookup and best fit; the default is best fit.

    • type - The format of the output message. Possible values are conjunction that stands for “and”-based lists (default, e.g., “A, B, and C”), or disjunction that stands for “or”-based lists (e.g., “A, B, or C”). unitstands for lists of values with units (e.g., “5 pounds, 12 ounces”).

    • style - The length of the formatted message. Possible values are: long(default, e.g., “A, B, and C”); short (e.g., “A, B, C”), or narrow (e.g., “A B C”). When style is short or narrow, unit is the only allowed value for the type option.

PreviousJavascript / TypescriptNextFE / React

Last updated 3 years ago

Was this helpful?

Source:

💎
https://www.amitmerchant.com/how-to-convert-arrays-to-human-readable-lists-in-javascript/