Units.res

 1type intlNumberFormat
 2
 3type options = {
 4  style: [#decimal | #currency | #percent | #unit],
 5  unit?: string,
 6  currency?: string,
 7  maximumFractionDigits?: int,
 8  unitDisplay?: [#short | #narrow | #long],
 9}
10
11@new @scope("Intl")
12external numberFormat: (Nullable.t<string>, options) => intlNumberFormat = "NumberFormat"
13
14@send
15external format: (intlNumberFormat, float) => string = "format"
16
17@send
18external formatInt: (intlNumberFormat, int) => string = "format"
19
20let distance = (value_kms: float, unit: [#mile | #km]) => {
21  let value = unit == #km ? value_kms : value_kms / 1.609344
22  let fractionDigits = switch value {
23  | _ if value > 100.0 => 0
24  | _ => 1
25  }
26  numberFormat(
27    Nullable.undefined,
28    {
29      style: #unit,
30      unit: unit == #km ? "kilometer" : "mile",
31      maximumFractionDigits: fractionDigits,
32      unitDisplay: #narrow,
33    },
34  )->format(value)
35}