|
1 | | -Low-level bindings for the `Intl` object https://tc39.es/ecma402/#intl-object |
| 1 | +# `web-intl` |
| 2 | + |
| 3 | +Low-level bindings for the ECMA 402 specification for the `Intl` object https://tc39.es/ecma402/#intl-object |
| 4 | + |
| 5 | +## How to use this library |
| 6 | + |
| 7 | +Assuming these imports |
| 8 | + |
| 9 | +```purs |
| 10 | +import Data.Array as Array |
| 11 | +import Data.Array.NonEmpty as NonEmpty |
| 12 | +import Data.JSDate as JSDate |
| 13 | +import Effect.Class.Console as Console |
| 14 | +import Web.Intl.Collator as Collator |
| 15 | +import Web.Intl.DateTimeFormat as DateTimeFormat |
| 16 | +import Web.Intl.Locale as Locale |
| 17 | +import Web.Intl.NumberFormat as NumberFormat |
| 18 | +``` |
| 19 | + |
| 20 | +we can construct a `Locale` using the `new` or `new_` constructors. |
| 21 | + |
| 22 | +```purs |
| 23 | +main = do |
| 24 | + en_US <- Locale.new "en-US" { hourCycle: "h24" } |
| 25 | + es_MX <- Locale.new_ "es-MX" |
| 26 | +``` |
| 27 | + |
| 28 | +All service constructors take a non-empty array of locales as first argument. |
| 29 | + |
| 30 | +```purs |
| 31 | + let locales = NonEmpty.cons' en_US [ es_MX ] |
| 32 | +``` |
| 33 | + |
| 34 | +Now we can use the `Collator` module to sort a collection of strings by [natural sort order](https://en.wikipedia.org/wiki/Natural_sort_order), |
| 35 | + |
| 36 | +```purs |
| 37 | + collator <- Collator.new locales { numeric: true } |
| 38 | + let |
| 39 | + sortedStrings = Array.sortBy (Collator.compare collator) [ "Chapter 1", "Chapter 11", "Chapter 2" ] |
| 40 | + Console.logShow sortedStrings -- [ "Chapter 1", "Chapter 2", "Chapter 11" ] |
| 41 | +``` |
| 42 | + |
| 43 | +or we can format a date using `DateTimeFormat`, |
| 44 | + |
| 45 | +```purs |
| 46 | + dateTimeFormat <- DateTimeFormat.new locales { dateStyle: "full", timeZone: "UTC" } |
| 47 | + let |
| 48 | + formattedDate = DateTimeFormat.format dateTimeFormat (JSDate.fromTime 0.0) |
| 49 | + Console.logShow formattedDate -- "Thursday, January 1, 1970" |
| 50 | +``` |
| 51 | + |
| 52 | +or use `NumberFormat` for formatting currencies for example. |
| 53 | + |
| 54 | +```purs |
| 55 | + numberFormat <- NumberFormat.new locales { style: "currency", currency: "USD" } |
| 56 | + let |
| 57 | + formattedNumber = NumberFormat.format numberFormat 123456.789 |
| 58 | + Console.logShow formattedNumber -- "$123,456.79" |
| 59 | +``` |
| 60 | + |
| 61 | +More examples are in the `Test.Main` module. |
0 commit comments