npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cldr-listpattern

v1.0.0

Published

CLDR ListPattern Formatter Library

Downloads

5

Readme

listpattern

This is a single-utility formatter for CLDR listPatterns.

npm install --save cldr-listpattern
yarn add cldr-listpattern

What does it do though?

Formatting a variable list of words into a sentence is simple for a single language. For instance in English, we can just join everything with commas and add an , and for the last element. But this isn't the case for every language.

// English
['lions', 'tigers', 'bears'] => 'lions, tigers, and bears'

vs

// Chinese
['lions', 'tigers', 'bears'] => 'lions、tigers和bears'

This tool uses available localization data from the CLDR to allow you to make these types of lists generically in the locale of your choosing.

TLDR: Give it the CLDR data, generate a formatter, optionally send config

const ListPattern = require('cldr-listpattern');

// You can use cldr.js to do this for real
const enUsListPatternCLDRData = {…};

const enUsPatternFormatter = new ListPattern(enUsListPatternCLDRData);

const dataList = ['lions', 'tigers', 'bears'];

// Use the 'standard' formatter
enUsPatternFormatter(dataList);

// > "lions, tigers, and bears"

// Optionally, use a different available listPattern
enUsPatternFormatter(dataList, {
  patternType: 'or',
});

// > "lions, tigers, or bears"

CLDR data

You'll probably want to use cldr-js and keep your data up to date, but if you look in the json CLDR data, you'll see files like this:

{
  "main": {
    "en": {
      "identity": {
        "version": {
          "_number": "$Revision: 13744 $",
          "_cldrVersion": "32.0.1"
        },
        "language": "en"
      },
      "listPatterns": {
        "listPattern-type-standard": {
          "start": "{0}, {1}",
          "middle": "{0}, {1}",
          "end": "{0}, and {1}",
          "2": "{0} and {1}"
        },
        "listPattern-type-or": {
          "start": "{0}, {1}",
          "middle": "{0}, {1}",
          "end": "{0}, or {1}",
          "2": "{0} or {1}"
        },
        "listPattern-type-standard-short": {
          "start": "{0}, {1}",
          "middle": "{0}, {1}",
          "end": "{0}, and {1}",
          "2": "{0} and {1}"
        },
        "listPattern-type-unit": {
          "start": "{0}, {1}",
          "middle": "{0}, {1}",
          "end": "{0}, {1}",
          "2": "{0}, {1}"
        },
        "listPattern-type-unit-narrow": {
          "start": "{0} {1}",
          "middle": "{0} {1}",
          "end": "{0} {1}",
          "2": "{0} {1}"
        },
        "listPattern-type-unit-short": {
          "start": "{0}, {1}",
          "middle": "{0}, {1}",
          "end": "{0}, {1}",
          "2": "{0}, {1}"
        }
      }
    }
  }
}

You can either send in that entire object (easier for people using a CLDR tool), or the inner listPatterns object, where the root level keys are each listPattern-type-<foo> names (easier for everyone else). We detect this based on the existence of the main.<lang>.identity sub-object.

Constructor

ListPattern(<object>locale_data[, <object>default_config])

Requiring the listpattern module returns a ListPattern constructor. It can't do anything until you generate an instance with CLDR data. It is recommended that you 'memoize' or cache these for each locale that you need to support.

const ListPattern = require('cldr-listpattern');
const enUsListPatternFormatter = new ListPattern(enCldrData);

A common shortening of this technique is to just invoke the constructor directly with the require statement.

const enUsListPatternFormatter = require('cldr-listpattern')(enCldrData);

Default configuration

The section below details how to send configuration information to the formatter on each invocation of the formatter, but if you have only a single formatter style that you always override with (or similar), then you can optionally pass the config as the second argument during the construction of the formatter.

const enUsListPatternFormatter = require('cldr-listpattern')(enCldrData, {patternType: 'or'});

This means that anything you format with enUsListPattern going forward will default to the or pattern type, instead of standard.

Configuration

formatter(<array>list_data[, <object>config])

patternType

The CLDR can, but is not required, to define many "types" of list patterns for a single locale. This might be something like a list that uses or instead of and. It might be a list of scientific units. It might be a list that needs to be more compact.

You can set the type of list by invoking the formatter with a configuration object, and setting the patternType parameter to your alternate list type.

enUsPatternFormatter(dataList, {
  patternType: 'or',
});

By default, listpattern will render the standard list pattern that the cldr defines in the listPattern-type-standard key of the data.

There is no set list of available listPattern formatters, so we will look in your data object for a key in your initial CLDR data with the name that matches listPattern-type-<your_config_value>. If this formatter doesn't exist for a given locale, it will fall back to the standard list pattern. If you set up a default config object when you instantiated your formatter, it will use the config there first, and then fall back to standard.

Author's note

If this fallback behavior is too silent for you, let me know, we could add lifecycle hooks, but I'm not convinced yet. I'm a little worried that a fallback from an or style pattern to an and style pattern will change the meaning of a sentence, not just the 'style'.

TODO:

I'd love for this to Just Work™ in node by pulling in the CLDR automatically in those cases.

License

MIT