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

intl-datetimeformat-pattern

v0.1.1

Published

Library to map CLDR Pattern/Skeleton to Intl.DateTimeFormat options

Downloads

347

Readme

intl-datetimeformat-pattern

Library to map CLDR Pattern/Skeleton to a valid Intl.DateTimeFormat options object.

Overview

ECMA-402, the Javascript Internationalization Specification does not include a way to format a CLDR Pattern or Skeleton, which is a common features in other programming languages. The reason for this omition is that using a pattern requires the programmer to specify a localized pattern for each locale. That model does not work well for the Web because users will tripover easily by using the same pattern for all locales, which defeats the purposes of Intl. Instead, it expects a bag of options that represent the desired configuration for the output, e.g.:

const options = {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    year: '2-digit'
};
const dt = new Intl.DateTimeFormat('en', options);
dt.format(Date.now()); // yields 'Fri, Oct 27, 17'

As a result, Javascript engines can decide what is the best possible output based on the provided locale, and the bag of options provided as the second argument for Intl.DateTimeFormat constructor. This complicates things if what you have is a pattern or skeleton from CLDR, and assuming that you, as a developer, know what you are doing, you should be able to rely on patterns to format your date values.

This Library

This library provides a single function that when invoked with a string value representing the pattern or skeleton, returns the most likely options object that can be used as the option bag for Intl.DateTimeFormat constructor.

The translation process is based on CLDR's Date Field Symbol Table:

  • http://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table

Examples

import patternToOptions from "intl-datetimeformat-pattern";
const options = patternToOptions("EEE, MMM d, ''yy");
const dt = new Intl.DateTimeFormat('en', options);
dt.format(Date.now()); // yields 'Fri, Oct 27, 17'

Cavets

The mapping process is loose, and for most cases, if the pattern or skeleton is described in CLDR/ICU, it is very likely that it produces the equivalent output. Other arbitrary patterns might result in different results, althought the result will probably preserve a similar set of components and settings that those described in the pattern.

Javascript engines will analyze the options derivated from the pattern, and will attempt to choose the best fit. Usually, that internal best-fit representation matches the original pattern, but this is implementation specific. Microsoft Internet Explorer and Edge do not use CLDR/ICU at the moment, which means they are more likely to differ from the rest of the browsers that are using ICU under the hood.

Contribute

Check out the Contributing document for the details. Thanks!

License

This software is free to use under the MIT license. See the LICENSE file for license text and copyright information.