|
| 1 | +type intl |
| 2 | + |
| 3 | +/* Supported locales */ |
| 4 | +@bs.deriving({jsConverter: newType}) |
| 5 | +type locale = [@bs.as("zh") #CH | @bs.as("ru-RU") #RUS | @bs.as("sv-SE") #SWE | @bs.as("en-US") #US] |
| 6 | + |
| 7 | +module Date = { |
| 8 | + module Weekday = { |
| 9 | + @bs.deriving({jsConverter: newType}) |
| 10 | + type t = [#long | #short | #narrow] |
| 11 | + |
| 12 | + let make = value => tToJs(value) |
| 13 | + } |
| 14 | + |
| 15 | + module Era = { |
| 16 | + @bs.deriving({jsConverter: newType}) |
| 17 | + type t = [#long | #short | #narrow] |
| 18 | + |
| 19 | + let make = value => tToJs(value) |
| 20 | + } |
| 21 | + |
| 22 | + module Year = { |
| 23 | + @bs.deriving({jsConverter: newType}) |
| 24 | + type t = [#numeric | @bs.as("2-digit") #twoDigit] |
| 25 | + |
| 26 | + let make = value => tToJs(value) |
| 27 | + } |
| 28 | + |
| 29 | + module Day = { |
| 30 | + @bs.deriving({jsConverter: newType}) |
| 31 | + type t = [#numeric | @bs.as("2-digit") #twoDigit] |
| 32 | + |
| 33 | + let make = value => tToJs(value) |
| 34 | + } |
| 35 | + |
| 36 | + module Month = { |
| 37 | + /* Helper for month option */ |
| 38 | + @bs.deriving({jsConverter: newType}) |
| 39 | + type t = [#long | #short | #narrow | #numeric | @bs.as("2-digit") #twoDigit] |
| 40 | + |
| 41 | + let make = value => tToJs(value) |
| 42 | + } |
| 43 | + |
| 44 | + /* Options for Intl.DateTimeFormat */ |
| 45 | + @bs.deriving(abstract) |
| 46 | + type options = { |
| 47 | + @bs.optional |
| 48 | + weekday: Weekday.abs_t, |
| 49 | + @bs.optional |
| 50 | + era: Era.abs_t, |
| 51 | + @bs.optional |
| 52 | + year: Year.abs_t, |
| 53 | + @bs.optional |
| 54 | + day: Day.abs_t, |
| 55 | + @bs.optional |
| 56 | + month: Month.abs_t, |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + Intl.DateTimeFormat |
| 61 | + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat |
| 62 | + */ |
| 63 | + @bs.new @bs.scope("Intl") |
| 64 | + external dateTimeFormat: (abs_locale, option<options>) => intl = "DateTimeFormat" |
| 65 | + |
| 66 | + /* Intl.DateTimeFormat.prototype.format() */ |
| 67 | + @bs.send external format: (intl, Js.Date.t) => string = "format" |
| 68 | + |
| 69 | + let make = (~locale=#US, ~options=?, date) => |
| 70 | + dateTimeFormat(localeToJs(locale), options)->format(date) |
| 71 | +} |
0 commit comments