Followup to my previous post. I have created a package that can be used to do pure-Elm locale aware Date
and Time.Posix
formatting (kind of like a simplified version of JS’s Intl.DateTimeFormat
). Apologies to aguzubiaga for not reaching out in collaboration, I procrastinated on this for a long time and then did all of it in a flurry of avoiding-my-actual-job-work.
Under the hood, it uses justinmimbs/date
and ryannhg/date-format
for the actual formatting work, so my main contribution is figuring out how to parse the Unicode standard format strings into the correct tokens and automating the process of getting the correct info out of the 375 JSON files in the most recent version of Unicode’s CLDR project.
Without further ado, I present:
elm-cldr
Naming
I decided to go with the name “elm-cldr” to make it clear that this is coming from the Unicode’s CLDR project. While I want the API to be familiar to those who know JS’s Intl
API, it is not currently a feature-complete replacement for that API. It has the basics, and I think will cover a lot of use cases, but it would need to be expanded in order to cover the various options that can be passed into Intl
.
Examples
There are fully operational examples in the ./examples
directory, so that anyone who wants to review this before I publish it can start from those if they wish.
DCE and sizing
Because of Elm’s automatic DCE, the size that this library has is dependent on how many locales you want to include. However, every locale that you may wish to match against needs to be included. For this reason, the fromString
function allows you to provide the list of locales that you would like to match against so that Elm will automatically eliminate all of the other locales. Two lists of locales are included to get you started, but you can build your own list any way that you want.
In order to get an idea of how big the file sizes were, the two examples take opposite approaches for using this. The “Static” example just uses a single locale, allowing 374 to be eliminated, while the “Dynamic” example includes all locales in the match list resulting in the maximum size. The examples are otherwise identical. I compiled each using elm make --optimize
to a standalone .js file and then gzipped it using the gzip command on my mac. I then measured the file sizes using ls -lh
:
- dynamic.js: 2.4M
- dynamic.js.gz: 93K
- static.js: 183K
- static.js.gz: 37K
Including all of the 375 locales does increase the size of the .js file by a lot, but gzip is able to bring the size differential down to something that is hopefully manageable.
My ask of you
If you are someone who is interested in a library like this existing, please take a look and provide your feedback! Would this help you? Do the names of things make sense? Now is the easiest time for me to make any changes, since I have not published it as a package yet.