diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ce313b..3289de50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next version +- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65 - Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181 - Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191 - Rename `Object.empty` to `Object.make` for consistency. diff --git a/rescript.json b/rescript.json index f5c04d1e..ab02df12 100644 --- a/rescript.json +++ b/rescript.json @@ -8,6 +8,7 @@ }, { "dir": "test", + "subdirs": ["intl"], "type": "dev" } ], diff --git a/src/Core__Intl.mjs b/src/Core__Intl.mjs index ffed57db..0d8e8a98 100644 --- a/src/Core__Intl.mjs +++ b/src/Core__Intl.mjs @@ -5,6 +5,8 @@ var Collator; var DateTimeFormat; +var ListFormat; + var Locale; var NumberFormat; @@ -13,12 +15,19 @@ var PluralRules; var RelativeTimeFormat; +var Segmenter; + +var Segments; + export { Collator , DateTimeFormat , + ListFormat , Locale , NumberFormat , PluralRules , RelativeTimeFormat , + Segmenter , + Segments , } /* No side effect */ diff --git a/src/Core__Intl.res b/src/Core__Intl.res index 90f0015d..822b4a4f 100644 --- a/src/Core__Intl.res +++ b/src/Core__Intl.res @@ -1,6 +1,24 @@ module Collator = Core__Intl__Collator module DateTimeFormat = Core__Intl__DateTimeFormat +module ListFormat = Core__Intl__ListFormat module Locale = Core__Intl__Locale module NumberFormat = Core__Intl__NumberFormat module PluralRules = Core__Intl__PluralRules module RelativeTimeFormat = Core__Intl__RelativeTimeFormat +module Segmenter = Core__Intl__Segmenter +module Segments = Core__Intl__Segments + +/** +@throws RangeError +*/ +external getCanonicalLocalesExn: string => array = "Intl.getCanonicalLocales" + +/** +@throws RangeError +*/ +external getCanonicalLocalesManyExn: array => array = "Intl.getCanonicalLocales" + +/** +@throws RangeError +*/ +external supportedValuesOfExn: string => array = "Intl.supportedValuesOf" diff --git a/src/intl/Core__Intl__Collator.res b/src/intl/Core__Intl__Collator.res index dce5716c..300b8766 100644 --- a/src/intl/Core__Intl__Collator.res +++ b/src/intl/Core__Intl__Collator.res @@ -1,17 +1,42 @@ type t +type usage = [#sort | #search] +type sensitivity = [#base | #accent | #case | #variant] +type caseFirst = [#upper | #lower | #"false"] + +type options = { + localeMatcher?: Core__Intl__Common.localeMatcher, + usage?: usage, + sensitivity?: sensitivity, + ignorePunctuation?: bool, + numeric?: bool, + caseFirst?: caseFirst, +} + +type resolvedOptions = { + locale: string, + usage: usage, + sensitivity: sensitivity, + ignorePunctuation: bool, + collation: [Core__Intl__Common.collation | #default], + numeric?: bool, + caseFirst?: caseFirst, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + @new external make: unit => t = "Intl.Collator" @new external makeWithLocale: string => t = "Intl.Collator" @new external makeWithLocales: array => t = "Intl.Collator" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.Collator" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.Collator" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.Collator" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.Collator" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.Collator" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.Collator" @val external supportedLocalesOf: array => t = "Intl.Collator.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = "Intl.Collator.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" @send external compare: (t, string, string) => int = "compare" diff --git a/src/intl/Core__Intl__Common.mjs b/src/intl/Core__Intl__Common.mjs new file mode 100644 index 00000000..d856702b --- /dev/null +++ b/src/intl/Core__Intl__Common.mjs @@ -0,0 +1,2 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ diff --git a/src/intl/Core__Intl__Common.res b/src/intl/Core__Intl__Common.res new file mode 100644 index 00000000..e0cea141 --- /dev/null +++ b/src/intl/Core__Intl__Common.res @@ -0,0 +1,156 @@ +type localeMatcher = [#lookup | @as("best fit") #bestFit] + +type calendar = [ + | #buddhist + | #chinese + | #coptic + | #dangi + | #ethioaa + | #ethiopic + | #gregory + | #hebrew + | #indian + | #islamic + | #"islamic-umalqura" + | #"islamic-tbla" + | #"islamic-civil" + | #"islamic-rgsa" + | #iso8601 + | #japanese + | #persian + | #roc +] + +type collation = [ + | #compat // (Arabic) + | #dict // (Sinhala) + | #emoji // (root) + | #eor // (root) + | #phonebk // (German) + | #phonetic // (Lingala) + | #pinyin // (Chinese) + | #stroke // (Chinese) + | #trad + | #unihan // (Chinese, Japanese, and Korean; not available in Chrome or Edge) + | #zhuyin +] // (Chinese) + +type numberingSystem = [ + | #adlm + | #ahom + | #arab + | #arabext + | #bali + | #beng + | #bhks + | #brah + | #cakm + | #cham + | #deva + | #diak + | #fullwide + | #gong + | #gonm + | #gujr + | #guru + | #hanidec + | #hmng + | #hmnp + | #java + | #kali + | #kawi + | #khmr + | #knda + | #lana + | #lanatham + | #laoo + | #latn + | #lepc + | #limb + | #mathbold + | #mathdbl + | #mathmono + | #mathsanb + | #mathsans + | #mlym + | #modi + | #mong + | #mroo + | #mtei + | #mymr + | #mymrshan + | #mymrtlng + | #nagm + | #newa + | #nkoo + | #olck + | #orya + | #osma + | #rohg + | #saur + | #segment + | #shrd + | #sind + | #sinh + | #sora + | #sund + | #takr + | #talu + | #tamldec + | #telu + | #thai + | #tibt + | #tirh + | #tnsa + | #vaii + | #wara + | #wcho +] + +type oneTo21 = [ + | #1 + | #2 + | #3 + | #4 + | #5 + | #6 + | #7 + | #8 + | #9 + | #10 + | #11 + | #12 + | #13 + | #14 + | #15 + | #16 + | #17 + | #18 + | #19 + | #20 + | #21 +] + +type zeroTo20 = [ + | #0 + | #1 + | #2 + | #3 + | #4 + | #5 + | #6 + | #7 + | #8 + | #9 + | #10 + | #11 + | #12 + | #13 + | #14 + | #15 + | #16 + | #17 + | #18 + | #19 + | #20 +] diff --git a/src/intl/Core__Intl__DateTimeFormat.res b/src/intl/Core__Intl__DateTimeFormat.res index 97a5b158..009445e3 100644 --- a/src/intl/Core__Intl__DateTimeFormat.res +++ b/src/intl/Core__Intl__DateTimeFormat.res @@ -1,18 +1,123 @@ type t +type dateStyle = [#full | #long | #medium | #short] +type timeStyle = [#full | #long | #medium | #short] +type dayPeriod = [#narrow | #short | #long] +type weekday = [#narrow | #short | #long] +type era = [#narrow | #short | #long] +type year = [#numeric | #"2-digit"] +type month = [#numeric | #"2-digit" | #narrow | #short | #long] +type day = [#numeric | #"2-digit"] +type hour = [#numeric | #"2-digit"] +type minute = [#numeric | #"2-digit"] +type second = [#numeric | #"2-digit"] + +/** +Firefox also supports IANA time zone names here +Node v19+ supports "shortOffset", "shortGeneric", "longOffset", and "longGeneric". +*/ +type timeZoneName = [ + | #short + | #long + | #shortOffset + | #shortGeneric + | #longOffset + | #longGeneric +] + +type hourCycle = [#h11 | #h12 | #h23 | #h24] +type formatMatcher = [#basic | @as("best fit") #bestFit] +type fractionalSecondDigits = [#0 | #1 | #2 | #3] + +type options = { + dateStyle?: dateStyle, // can be used with timeStyle, but not other options + timeStyle?: timeStyle, // can be used with dateStyle, but not other options + calendar?: Core__Intl__Common.calendar, + dayPeriod?: dayPeriod, // only has an effect if a 12-hour clock is used + numberingSystem?: Core__Intl__Common.numberingSystem, + localeMatcher?: Core__Intl__Common.localeMatcher, + timeZone?: string, + hour12?: bool, + hourCycle?: hourCycle, + formatMatcher?: formatMatcher, + // date-time components + weekday?: weekday, + era?: era, + year?: year, + month?: month, + day?: day, + hour?: hour, + minute?: minute, + second?: second, + fractionalSecondDigits?: fractionalSecondDigits, + timeZoneName?: timeZoneName, +} + +type resolvedOptions = { + dateStyle?: dateStyle, + timeStyle?: timeStyle, + weekday?: weekday, + era?: era, + year?: year, + month?: month, + day?: day, + hour?: hour, + minute?: minute, + second?: second, + fractionalSecondDigits?: fractionalSecondDigits, + timeZoneName?: timeZoneName, + calendar: Core__Intl__Common.calendar, + hour12: bool, + hourCycle: hourCycle, + locale: string, + numberingSystem: Core__Intl__Common.numberingSystem, + timeZone: string, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + +type dateTimeComponent = [ + | #day + | #dayPeriod + | #era + | #fractionalSecond + | #hour + | #literal + | #minute + | #month + | #relatedYear + | #second + | #timeZone + | #weekday + | #year + | #yearName +] + +type dateTimePart = { + \"type": dateTimeComponent, + value: string, +} + +type dateTimeRangeSource = [#startRange | #shared | #endRange] +type dateTimeRangePart = { + \"type": dateTimeComponent, + value: string, + source: dateTimeRangeSource, +} + @new external make: unit => t = "Intl.DateTimeFormat" @new external makeWithLocale: string => t = "Intl.DateTimeFormat" @new external makeWithLocales: array => t = "Intl.DateTimeFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.DateTimeFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.DateTimeFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.DateTimeFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.DateTimeFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.DateTimeFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat" @val external supportedLocalesOf: array => t = "Intl.DateTimeFormat.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = "Intl.DateTimeFormat.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" @send external format: (t, Core__Date.t) => string = "format" @send diff --git a/src/intl/Core__Intl__ListFormat.mjs b/src/intl/Core__Intl__ListFormat.mjs new file mode 100644 index 00000000..d856702b --- /dev/null +++ b/src/intl/Core__Intl__ListFormat.mjs @@ -0,0 +1,2 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ diff --git a/src/intl/Core__Intl__ListFormat.res b/src/intl/Core__Intl__ListFormat.res new file mode 100644 index 00000000..43a6174a --- /dev/null +++ b/src/intl/Core__Intl__ListFormat.res @@ -0,0 +1,53 @@ +type t + +type listType = [ + | #conjunction + | #disjunction + | #unit +] +type style = [ + | #long + | #short + | #narrow +] + +type options = { + localeMatcher?: Core__Intl__Common.localeMatcher, + \"type"?: listType, + style?: style, +} + +type listPartComponentType = [ + | #element + | #literal +] + +type listPart = { + \"type": listPartComponentType, + value: string, +} + +type resolvedOptions = { + locale: string, + style: style, + \"type": listType, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + +@new external make: unit => t = "Intl.ListFormat" +@new external makeWithLocale: string => t = "Intl.ListFormat" +@new external makeWithLocales: array => t = "Intl.ListFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.ListFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.ListFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.ListFormat" + +@val external supportedLocalesOf: array => t = "Intl.ListFormat.supportedLocalesOf" +@val +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = + "Intl.ListFormat.supportedLocalesOf" + +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" + +@send external format: (t, array) => string = "format" +@send external formatToParts: (t, array) => array = "formatToParts" diff --git a/src/intl/Core__Intl__Locale.res b/src/intl/Core__Intl__Locale.res index b2ecec9b..5d6afb47 100644 --- a/src/intl/Core__Intl__Locale.res +++ b/src/intl/Core__Intl__Locale.res @@ -1,7 +1,20 @@ type t +type options = { + baseName?: string, + calendar?: Core__Intl__Common.calendar, + collation?: Core__Intl__Common.collation, + hourCycle?: [#h11 | #h12 | #h23 | #h24], + caseFirst?: [#upper | #lower | #"false"], + numberingSystem?: Core__Intl__Common.numberingSystem, + numeric?: bool, + language?: string, + script?: string, + region?: string, +} + @new external make: string => t = "Intl.Locale" -@new external makeWithOptions: (string, {..}) => t = "Intl.Locale" +@new external makeWithOptions: (string, options) => t = "Intl.Locale" @get external baseName: t => string = "baseName" @get external calendar: t => option = "calendar" diff --git a/src/intl/Core__Intl__NumberFormat.mjs b/src/intl/Core__Intl__NumberFormat.mjs index d856702b..88f119a6 100644 --- a/src/intl/Core__Intl__NumberFormat.mjs +++ b/src/intl/Core__Intl__NumberFormat.mjs @@ -1,2 +1,9 @@ // Generated by ReScript, PLEASE EDIT WITH CARE -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ + + +var Grouping; + +export { + Grouping , +} +/* No side effect */ diff --git a/src/intl/Core__Intl__NumberFormat.res b/src/intl/Core__Intl__NumberFormat.res index 50f9866a..f229a3f5 100644 --- a/src/intl/Core__Intl__NumberFormat.res +++ b/src/intl/Core__Intl__NumberFormat.res @@ -1,28 +1,222 @@ +module Grouping = Core__Intl__NumberFormat__Grouping + type t +/** +An ISO 4217 currency code. e.g. USD, EUR, CNY +*/ +type currency = string +type currencyDisplay = [#symbol | #narrowSymbol | #code | #name] +type currencySign = [#accounting | #standard] +type notation = [#scientific | #standard | #engineering | #compact] + +/** +Used only when notation is #compact +*/ +type compactDisplay = [#short | #long] + +type signDisplay = [ + | #auto + | #always + | #exceptZero + | #never + | #negative +] + +type style = [#decimal | #currency | #percent | #unit] + +/** +Defined in https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier +Only used when style is #unit +*/ +type unitSystem = string + +/** +Only used when style is #unit +*/ +type unitDisplay = [#long | #short | #narrow] + +type rounding = [ + | #ceil + | #floor + | #expand + | #trunc + | #halfCeil + | #halfFloor + | #halfExpand + | #halfTrunc + | #halfEven +] + +type roundingPriority = [#auto | #morePrecision | #lessPrecision] + +type roundingIncrement = [ + | #1 + | #2 + | #5 + | #10 + | #20 + | #25 + | #50 + | #100 + | #200 + | #250 + | #500 + | #1000 + | #2000 + | #2500 + | #5000 +] + +type trailingZeroDisplay = [#auto | #stripIfInteger | #lessPrecision] + +type options = { + compactDisplay?: compactDisplay, + numberingSystem?: Core__Intl__Common.numberingSystem, + currency?: currency, + currencyDisplay?: currencyDisplay, + currencySign?: currencySign, + localeMatcher?: Core__Intl__Common.localeMatcher, + notation?: notation, + signDisplay?: signDisplay, + style?: style, + /** + required if style == #unit + */ + unit?: unitSystem, + unitDisplay?: unitDisplay, + useGrouping?: Grouping.t, + roundingMode?: rounding, // not available in firefox v110 + roundingPriority?: roundingPriority, // not available in firefox v110 + roundingIncrement?: roundingIncrement, // not available in firefox v110 + /** + Supported everywhere but Firefox as of v110 + */ + trailingZeroDisplay?: trailingZeroDisplay, + // use either this group + minimumIntegerDigits?: Core__Intl__Common.oneTo21, + minimumFractionDigits?: Core__Intl__Common.zeroTo20, + maximumFractionDigits?: Core__Intl__Common.zeroTo20, + // OR these + minimumSignificantDigits?: Core__Intl__Common.oneTo21, + maximumSignificantDigits?: Core__Intl__Common.oneTo21, +} + +type resolvedOptions = { + // only when style == "currency" + currency?: currency, + currencyDisplay?: currencyDisplay, + currencySign?: currencySign, + // only when notation == "compact" + compactDisplay?: compactDisplay, + // only when style == "unit" + unit: unitSystem, + unitDisplay: unitDisplay, + roundingMode?: rounding, // not available in firefox v110 + roundingPriority?: roundingPriority, // not available in firefox v110 + roundingIncrement?: roundingIncrement, // not available in firefox v110 + // either this group + minimumIntegerDigits?: Core__Intl__Common.oneTo21, + minimumFractionDigits?: Core__Intl__Common.zeroTo20, + maximumFractionDigits?: Core__Intl__Common.zeroTo20, + // OR these + minimumSignificantDigits?: Core__Intl__Common.oneTo21, + maximumSignificantDigits?: Core__Intl__Common.oneTo21, + // always present + locale: string, + notation: notation, + numberingSystem: Core__Intl__Common.numberingSystem, + signDisplay: signDisplay, + style: style, + useGrouping: Grouping.t, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + +type numberFormatPartType = [ + | #compact + | #currency + | #decimal + | #exponentInteger + | #exponentMinusSign + | #exponentSeparator + | #fraction + | #group + | #infinity + | #integer + | #literal + | #minusSign + | #nan + | #plusSign + | #percentSign + | #unit + | #unknown +] + +type numberFormatPart = { + \"type": numberFormatPartType, + value: string, +} + +type rangeSource = [#startRange | #endRange | #shared] + +type numberFormatRangePart = { + \"type": numberFormatPartType, + value: string, + source: rangeSource, +} + @new external make: unit => t = "Intl.NumberFormat" @new external makeWithLocale: string => t = "Intl.NumberFormat" @new external makeWithLocales: array => t = "Intl.NumberFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.NumberFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.NumberFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.NumberFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.NumberFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.NumberFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.NumberFormat" @val external supportedLocalesOf: array => t = "Intl.NumberFormat.supportedLocalesOf" + @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = "Intl.NumberFormat.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" @send external format: (t, float) => string = "format" @send -external formatToParts: (t, float) => array<{"type": string, "value": string}> = "formatToParts" +external formatRange: (t, ~start: float, ~end: float) => array = "formatRange" +@send +external formatToParts: (t, float) => array = "formatToParts" +@send +external formatRangeToParts: (t, ~start: float, ~end: float) => array = + "formatRange" @send external formatInt: (t, int) => string = "format" + +@send +external formatIntRange: (t, ~start: int, ~end: int) => array = "formatRange" +@send +external formatIntToParts: (t, int) => array = "formatToParts" + @send -external formatIntToParts: (t, int) => array<{"type": string, "value": string}> = "formatToParts" +external formatIntRangeToParts: (t, ~start: int, ~end: int) => array = + "formatRange" @send external formatBigInt: (t, Core__BigInt.t) => string = "format" + +@send +external formatBigIntRange: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => array = + "formatRange" +@send +external formatBigIntToParts: (t, Core__BigInt.t) => array = "formatToParts" + +@send +external formatBigIntRangeToParts: ( + t, + ~start: Core__BigInt.t, + ~end: Core__BigInt.t, +) => array = "formatRange" + +@send external formatString: (t, string) => string = "format" + @send -external formatBigIntToParts: (t, Core__BigInt.t) => array<{"type": string, "value": string}> = - "formatToParts" +external formatStringToParts: (t, string) => array = "formatToParts" diff --git a/src/intl/Core__Intl__NumberFormat__Grouping.mjs b/src/intl/Core__Intl__NumberFormat__Grouping.mjs new file mode 100644 index 00000000..73c227aa --- /dev/null +++ b/src/intl/Core__Intl__NumberFormat__Grouping.mjs @@ -0,0 +1,35 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Core__Type from "../Core__Type.mjs"; + +function parseJsValue(value) { + var value$1 = Core__Type.Classify.classify(value); + if (typeof value$1 !== "object") { + return ; + } + switch (value$1.TAG) { + case "Bool" : + return { + NAME: "bool", + VAL: value$1._0 + }; + case "String" : + switch (value$1._0) { + case "always" : + return "always"; + case "auto" : + return "auto"; + case "min2" : + return "min2"; + default: + return ; + } + default: + return ; + } +} + +export { + parseJsValue , +} +/* No side effect */ diff --git a/src/intl/Core__Intl__NumberFormat__Grouping.res b/src/intl/Core__Intl__NumberFormat__Grouping.res new file mode 100644 index 00000000..a65f5296 --- /dev/null +++ b/src/intl/Core__Intl__NumberFormat__Grouping.res @@ -0,0 +1,15 @@ +type t + +type parsed = [#bool(bool) | #always | #auto | #min2] + +external fromBool: bool => t = "%identity" +external fromString: [#always | #auto | #min2] => t = "%identity" + +let parseJsValue = value => + switch Core__Type.Classify.classify(value) { + | String("always") => Some(#always) + | String("auto") => Some(#auto) + | String("min2") => Some(#min2) + | Bool(value) => Some(#bool(value)) + | _ => None + } diff --git a/src/intl/Core__Intl__PluralRules.res b/src/intl/Core__Intl__PluralRules.res index 6d71bcbf..008f5ab3 100644 --- a/src/intl/Core__Intl__PluralRules.res +++ b/src/intl/Core__Intl__PluralRules.res @@ -1,21 +1,69 @@ type t +type localeType = [#cardinal | #ordinal] + +type options = { + localeMatcher?: Core__Intl__Common.localeMatcher, + \"type"?: localeType, + // use either this group + minimumIntegerDigits?: Core__Intl__Common.oneTo21, + minimumFractionDigits?: Core__Intl__Common.zeroTo20, + maximumFractionDigits?: Core__Intl__Common.zeroTo20, + // OR this group + minimumSignificantDigits?: Core__Intl__Common.oneTo21, + maximumSignificantDigits?: Core__Intl__Common.oneTo21, +} + +type pluralCategories = [ + | #zero + | #one + | #two + | #few + | #many + | #other +] + +type resolvedOptions = { + locale: string, + pluralCategories: array, + \"type": localeType, + // either this group + minimumIntegerDigits?: Core__Intl__Common.oneTo21, + minimumFractionDigits?: Core__Intl__Common.zeroTo20, + maximumFractionDigits?: Core__Intl__Common.zeroTo20, + // OR this group + minimumSignificantDigits?: Core__Intl__Common.oneTo21, + maximumSignificantDigits?: Core__Intl__Common.oneTo21, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + @new external make: unit => t = "Intl.PluralRules" @new external makeWithLocale: string => t = "Intl.PluralRules" @new external makeWithLocales: array => t = "Intl.PluralRules" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.PluralRules" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.PluralRules" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.PluralRules" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.PluralRules" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.PluralRules" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.PluralRules" @val external supportedLocalesOf: array => t = "Intl.PluralRules.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = "Intl.PluralRules.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" type rule = [#zero | #one | #two | #few | #many | #other] @send external select: (t, float) => rule = "select" @send external selectInt: (t, int) => rule = "select" @send external selectBigInt: (t, Core__BigInt.t) => rule = "select" + +@send +external selectRange: (t, ~start: float, ~end: float) => rule = "selectRange" + +@send +external selectRangeInt: (t, ~start: int, ~end: int) => rule = "selectRange" + +@send +external selectRangeBigInt: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => rule = + "selectRange" diff --git a/src/intl/Core__Intl__RelativeTimeFormat.res b/src/intl/Core__Intl__RelativeTimeFormat.res index 8a561977..8cbf2138 100644 --- a/src/intl/Core__Intl__RelativeTimeFormat.res +++ b/src/intl/Core__Intl__RelativeTimeFormat.res @@ -1,30 +1,46 @@ type t +type numeric = [#always | #auto] +type style = [#long | #short | #narrow] +type timeUnit = [#year | #quarter | #month | #week | #day | #hour | #minute | #second] + +type options = { + localeMatcher?: Core__Intl__Common.localeMatcher, + numeric?: numeric, + style?: style, +} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + +type resolvedOptions = { + locale: string, + numeric: numeric, + style: style, + numberingSystem: string, +} + +type relativeTimePartComponent = [#literal | #integer] +type relativeTimePart = { + \"type": relativeTimePartComponent, + value: string, + unit?: timeUnit, +} + @new external make: unit => t = "Intl.RelativeTimeFormat" @new external makeWithLocale: string => t = "Intl.RelativeTimeFormat" @new external makeWithLocales: array => t = "Intl.RelativeTimeFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.RelativeTimeFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.RelativeTimeFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.RelativeTimeFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.RelativeTimeFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.RelativeTimeFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.RelativeTimeFormat" @val external supportedLocalesOf: array => t = "Intl.RelativeTimeFormat.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = "Intl.RelativeTimeFormat.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" - -type timeUnit = [#year | #quarter | #month | #week | #day | #hour | #minute | #second] +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" @send external format: (t, int, timeUnit) => string = "format" @send -external formatToParts: ( - t, - int, - timeUnit, -) => array<{ - "type": string, - "value": string, - "unit": option, -}> = "formatToParts" +external formatToParts: (t, int, timeUnit) => array = "formatToParts" diff --git a/src/intl/Core__Intl__Segmenter.mjs b/src/intl/Core__Intl__Segmenter.mjs new file mode 100644 index 00000000..d856702b --- /dev/null +++ b/src/intl/Core__Intl__Segmenter.mjs @@ -0,0 +1,2 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ diff --git a/src/intl/Core__Intl__Segmenter.res b/src/intl/Core__Intl__Segmenter.res new file mode 100644 index 00000000..c6def395 --- /dev/null +++ b/src/intl/Core__Intl__Segmenter.res @@ -0,0 +1,40 @@ +/*** +Not supported in Firefox +*/ +type t + +type granularity = [#grapheme | #word | #sentence] + +type options = { + localeMatcher?: Core__Intl__Common.localeMatcher, + granularity?: granularity, +} + +type pluralCategories = [ + | #zero + | #one + | #two + | #few + | #many + | #other +] + +type resolvedOptions = {locale: string, granularity: granularity} + +type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher} + +@new external make: unit => t = "Intl.Segmenter" +@new external makeWithLocale: string => t = "Intl.Segmenter" +@new external makeWithLocales: array => t = "Intl.Segmenter" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.Segmenter" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.Segmenter" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.Segmenter" + +@val external supportedLocalesOf: array => t = "Intl.Segmenter.supportedLocalesOf" +@val +external supportedLocalesOfWithOptions: (array, supportedLocalesOptions) => t = + "Intl.Segmenter.supportedLocalesOf" + +@send external resolvedOptions: t => resolvedOptions = "resolvedOptions" + +@send external segment: (t, string) => Core__Intl__Segments.t = "segment" diff --git a/src/intl/Core__Intl__Segments.mjs b/src/intl/Core__Intl__Segments.mjs new file mode 100644 index 00000000..d856702b --- /dev/null +++ b/src/intl/Core__Intl__Segments.mjs @@ -0,0 +1,2 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ diff --git a/src/intl/Core__Intl__Segments.res b/src/intl/Core__Intl__Segments.res new file mode 100644 index 00000000..33b06ea1 --- /dev/null +++ b/src/intl/Core__Intl__Segments.res @@ -0,0 +1,18 @@ +/*** + A Segments instance is an object that represents the segments of a specific string, subject to the locale and options of its constructing Intl.Segmenter instance. +https://tc39.es/ecma402/#sec-segments-objects +*/ +type t + +type segmentData = { + segment: string, + index: int, + isWordLike: option, + input: string, +} + +@send +external containing: t => segmentData = "containing" + +@send +external containingWithIndex: (t, int) => segmentData = "containing" diff --git a/test/Intl/IntlTests.mjs b/test/Intl/IntlTests.mjs new file mode 100644 index 00000000..6fcf5875 --- /dev/null +++ b/test/Intl/IntlTests.mjs @@ -0,0 +1,134 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Js_exn from "rescript/lib/es6/js_exn.js"; +import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Option from "../../src/Core__Option.mjs"; +import * as Intl__LocaleTest from "./Intl__LocaleTest.mjs"; +import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js"; +import * as Intl__CollatorTest from "./Intl__CollatorTest.mjs"; +import * as Intl__SegmenterTest from "./Intl__SegmenterTest.mjs"; +import * as Intl__ListFormatTest from "./Intl__ListFormatTest.mjs"; +import * as Intl__PluralRulesTest from "./Intl__PluralRulesTest.mjs"; +import * as Intl__NumberFormatTest from "./Intl__NumberFormatTest.mjs"; +import * as Intl__DateTimeFormatTest from "./Intl__DateTimeFormatTest.mjs"; +import * as Intl__RelativeTimeFormatTest from "./Intl__RelativeTimeFormatTest.mjs"; + +console.log("---"); + +console.log("Intl"); + +console.log(Intl.getCanonicalLocales("EN-US")); + +console.log(Intl.getCanonicalLocales([ + "EN-US", + "Fr" + ])); + +try { + console.log(Intl.getCanonicalLocales("bloop")); +} +catch (raw_e){ + var e = Caml_js_exceptions.internalToOCamlException(raw_e); + if (e.RE_EXN_ID === Js_exn.$$Error) { + console.error(e._1); + } else { + throw e; + } +} + +try { + console.log(Intl.supportedValuesOf("calendar")); + console.log(Intl.supportedValuesOf("collation")); + console.log(Intl.supportedValuesOf("currency")); + console.log(Intl.supportedValuesOf("numberingSystem")); + console.log(Intl.supportedValuesOf("timeZone")); + console.log(Intl.supportedValuesOf("unit")); +} +catch (raw_e$1){ + var e$1 = Caml_js_exceptions.internalToOCamlException(raw_e$1); + if (e$1.RE_EXN_ID === Js_exn.$$Error) { + console.error(e$1._1); + } else { + throw e$1; + } +} + +try { + Intl.supportedValuesOf("someInvalidKey"); + console.error("Shouldn't have been hit"); +} +catch (raw_e$2){ + var e$2 = Caml_js_exceptions.internalToOCamlException(raw_e$2); + if (e$2.RE_EXN_ID === Js_exn.$$Error) { + var e$3 = e$2._1; + var message = Core__Option.map(e$3.message, (function (prim) { + return prim.toLowerCase(); + })); + var exit = 0; + if (message === "invalid key : someinvalidkey") { + console.log("Caught expected error"); + } else { + exit = 1; + } + if (exit === 1) { + console.warn("Unexpected error message: \"" + message + "\""); + throw e$3; + } + + } else { + var e$4 = Caml_js_exceptions.as_js_exn(e$2); + if (e$4 !== undefined) { + throw Caml_option.valFromOption(e$4); + } + console.error("Unexpected error"); + } +} + +var _collator = Intl__CollatorTest._collator; + +var collator = Intl__CollatorTest.collator; + +var resolvedOptions = Intl__DateTimeFormatTest.resolvedOptions; + +var timeZone = Intl__DateTimeFormatTest.timeZone; + +var _locale = Intl__LocaleTest._locale; + +var locale = Intl__LocaleTest.locale; + +var currencyFormatter = Intl__NumberFormatTest.currencyFormatter; + +var roundingFormatter = Intl__NumberFormatTest.roundingFormatter; + +var groupingFormatter1 = Intl__NumberFormatTest.groupingFormatter1; + +var groupingFormatter2 = Intl__NumberFormatTest.groupingFormatter2; + +var sigFormatter = Intl__NumberFormatTest.sigFormatter; + +var options = Intl__NumberFormatTest.options; + +var _formatter = Intl__SegmenterTest._formatter; + +var formatter = Intl__SegmenterTest.formatter; + +var segments = Intl__SegmenterTest.segments; + +export { + _collator , + collator , + resolvedOptions , + timeZone , + _locale , + locale , + currencyFormatter , + roundingFormatter , + groupingFormatter1 , + groupingFormatter2 , + sigFormatter , + options , + _formatter , + formatter , + segments , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__CollatorTest.mjs b/test/Intl/Intl__CollatorTest.mjs new file mode 100644 index 00000000..3945f3a8 --- /dev/null +++ b/test/Intl/Intl__CollatorTest.mjs @@ -0,0 +1,46 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.Collator"); + +new Intl.Collator(); + +new Intl.Collator("en-US"); + +var _collator = new Intl.Collator([ + "en-US", + "en-GB" + ]); + +var collator = new Intl.Collator("en-US", { + sensitivity: "base", + ignorePunctuation: true, + numeric: true, + caseFirst: "upper" + }); + +Intl.Collator.supportedLocalesOf([ + "en-US", + "en-GB" + ]); + +Intl.Collator.supportedLocalesOf([ + "en-US", + "en-GB" + ], { + localeMatcher: "lookup" + }); + +console.log(collator.resolvedOptions()); + +console.log(collator.compare("hi", "hï")); + +console.log(Intl.Collator.supportedLocalesOf(["hi"])); + +export { + _collator , + collator , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__DateTimeFormatTest.mjs b/test/Intl/Intl__DateTimeFormatTest.mjs new file mode 100644 index 00000000..140ae17c --- /dev/null +++ b/test/Intl/Intl__DateTimeFormatTest.mjs @@ -0,0 +1,90 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; + +console.log("---"); + +console.log("Intl.DateTimeFormat"); + +Intl.DateTimeFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ]); + +Intl.DateTimeFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ], { + localeMatcher: "lookup" + }); + +var formatter = new Intl.DateTimeFormat(undefined, { + dateStyle: "full", + timeStyle: "full" + }); + +console.log(formatter.format(new Date(Date.now()))); + +console.log(formatter.formatRange(new Date(2023, 1, 1), new Date(2023, 12, 31))); + +var options = { + timeZone: "UTC", + hour12: false, + hourCycle: "h24", + weekday: "narrow", + era: "narrow", + year: "2-digit", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + fractionalSecondDigits: 3, + timeZoneName: "longGeneric" +}; + +var formatter$1 = new Intl.DateTimeFormat(undefined, options); + +console.log(formatter$1.format(new Date(Date.now()))); + +var newrecord = Caml_obj.obj_dup(options); + +var formatter$2 = new Intl.DateTimeFormat(undefined, (newrecord.timeZoneName = "long", newrecord)); + +console.log(formatter$2.format(new Date(Date.now()))); + +var newrecord$1 = Caml_obj.obj_dup(options); + +var formatter$3 = new Intl.DateTimeFormat(undefined, (newrecord$1.timeZoneName = "longOffset", newrecord$1)); + +console.log(formatter$3.format(new Date(Date.now()))); + +var newrecord$2 = Caml_obj.obj_dup(options); + +var formatter$4 = new Intl.DateTimeFormat(undefined, (newrecord$2.timeZoneName = "short", newrecord$2)); + +console.log(formatter$4.format(new Date(Date.now()))); + +var newrecord$3 = Caml_obj.obj_dup(options); + +var formatter$5 = new Intl.DateTimeFormat(undefined, (newrecord$3.timeZoneName = "shortGeneric", newrecord$3)); + +console.log(formatter$5.format(new Date(Date.now()))); + +var newrecord$4 = Caml_obj.obj_dup(options); + +var formatter$6 = new Intl.DateTimeFormat(undefined, (newrecord$4.timeZoneName = "shortOffset", newrecord$4)); + +console.log(formatter$6.format(new Date(Date.now()))); + +var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions(); + +var timeZone = resolvedOptions.timeZone; + +export { + options , + formatter$6 as formatter, + resolvedOptions , + timeZone , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__ListFormatTest.mjs b/test/Intl/Intl__ListFormatTest.mjs new file mode 100644 index 00000000..592a5eb7 --- /dev/null +++ b/test/Intl/Intl__ListFormatTest.mjs @@ -0,0 +1,65 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.ListFormat"); + +new Intl.ListFormat(); + +new Intl.ListFormat("en-US"); + +new Intl.ListFormat([ + "en-US", + "en-GB" + ]); + +new Intl.ListFormat("en-US", { + type: "conjunction", + style: "long" + }); + +var _formatter = new Intl.ListFormat([ + "en-US", + "en-GB" + ], { + type: "conjunction", + style: "long" + }); + +Intl.ListFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ]); + +Intl.ListFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ], { + localeMatcher: "lookup" + }); + +var formatter = new Intl.ListFormat(undefined, { + type: "conjunction", + style: "long" + }); + +console.log(formatter.resolvedOptions()); + +console.log(formatter.format([ + "one", + "two", + "three" + ])); + +console.log(formatter.formatToParts([ + "one", + "two", + "three" + ])); + +export { + _formatter , + formatter , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__LocaleTest.mjs b/test/Intl/Intl__LocaleTest.mjs new file mode 100644 index 00000000..a8349a84 --- /dev/null +++ b/test/Intl/Intl__LocaleTest.mjs @@ -0,0 +1,46 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.Locale"); + +var _locale = new Intl.Locale("en-US"); + +var locale = new Intl.Locale("en-US", { + calendar: "hebrew", + collation: "compat", + hourCycle: "h24", + caseFirst: "upper", + numeric: true + }); + +console.log(locale.baseName); + +console.log(locale.calendar); + +console.log(locale.caseFirst); + +console.log(locale.collation); + +console.log(locale.hourCycle); + +console.log(locale.language); + +console.log(locale.numberingSystem); + +console.log(locale.numeric); + +console.log(locale.region); + +console.log(locale.script); + +console.log(locale.maximize()); + +console.log(locale.minimize()); + +export { + _locale , + locale , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__NumberFormatTest.mjs b/test/Intl/Intl__NumberFormatTest.mjs new file mode 100644 index 00000000..d19804e9 --- /dev/null +++ b/test/Intl/Intl__NumberFormatTest.mjs @@ -0,0 +1,58 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Intl__NumberFormat__Grouping from "../../src/intl/Core__Intl__NumberFormat__Grouping.mjs"; + +console.log("---"); + +console.log("Intl.NumberFormat"); + +var currencyFormatter = new Intl.NumberFormat("fr-FR", { + currency: "EUR", + style: "currency" + }); + +console.log(Intl.NumberFormat.supportedLocalesOf([ + "fr-FR", + "en-US" + ])); + +console.log(currencyFormatter.format(123.23)); + +var roundingFormatter = new Intl.NumberFormat(undefined, { + roundingIncrement: 500, + minimumFractionDigits: 2, + maximumFractionDigits: 2 + }); + +var groupingFormatter1 = new Intl.NumberFormat(undefined, { + useGrouping: Caml_option.some(true) + }); + +var groupingFormatter2 = new Intl.NumberFormat(undefined, { + useGrouping: "auto" + }); + +var sigFormatter = new Intl.NumberFormat(undefined, { + minimumIntegerDigits: 1, + minimumFractionDigits: 1, + maximumFractionDigits: 1, + minimumSignificantDigits: 1, + maximumSignificantDigits: 1 + }); + +var options = sigFormatter.resolvedOptions(); + +console.log(options); + +console.log(Core__Intl__NumberFormat__Grouping.parseJsValue(options.useGrouping)); + +export { + currencyFormatter , + roundingFormatter , + groupingFormatter1 , + groupingFormatter2 , + sigFormatter , + options , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__PluralRulesTest.mjs b/test/Intl/Intl__PluralRulesTest.mjs new file mode 100644 index 00000000..b4fe8030 --- /dev/null +++ b/test/Intl/Intl__PluralRulesTest.mjs @@ -0,0 +1,45 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.PluralRules"); + +new Intl.PluralRules(); + +new Intl.PluralRules("en-US"); + +new Intl.PluralRules([ + "en-US", + "en-GB" + ]); + +new Intl.PluralRules(undefined, { + type: "ordinal", + minimumSignificantDigits: 3, + maximumSignificantDigits: 5 + }); + +var _formatter = new Intl.PluralRules("en-US", { + type: "ordinal", + minimumSignificantDigits: 3, + maximumSignificantDigits: 5 + }); + +var formatter = new Intl.PluralRules(["en-US"], { + type: "ordinal", + minimumSignificantDigits: 3, + maximumSignificantDigits: 5 + }); + +console.log(formatter.resolvedOptions()); + +console.log(formatter.select(2.0)); + +console.log(formatter.select(2)); + +export { + _formatter , + formatter , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__RelativeTimeFormatTest.mjs b/test/Intl/Intl__RelativeTimeFormatTest.mjs new file mode 100644 index 00000000..062d89be --- /dev/null +++ b/test/Intl/Intl__RelativeTimeFormatTest.mjs @@ -0,0 +1,54 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.RelativeTimeFormat"); + +Intl.RelativeTimeFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ]); + +Intl.RelativeTimeFormat.supportedLocalesOf([ + "en-US", + "en-GB" + ], { + localeMatcher: "lookup" + }); + +new Intl.RelativeTimeFormat(); + +new Intl.RelativeTimeFormat("en-US"); + +new Intl.RelativeTimeFormat([ + "en-US", + "en-GB" + ]); + +new Intl.RelativeTimeFormat(undefined, { + numeric: "always", + style: "narrow" + }); + +var _formatter = new Intl.RelativeTimeFormat("en-US", { + numeric: "always", + style: "narrow" + }); + +var formatter = new Intl.RelativeTimeFormat(["en-US"], { + numeric: "always", + style: "narrow" + }); + +console.log(formatter.resolvedOptions()); + +console.log(formatter.format(3, "hour")); + +console.log(formatter.formatToParts(3, "hour")); + +export { + _formatter , + formatter , +} +/* Not a pure module */ diff --git a/test/Intl/Intl__SegmenterTest.mjs b/test/Intl/Intl__SegmenterTest.mjs new file mode 100644 index 00000000..757c7c4d --- /dev/null +++ b/test/Intl/Intl__SegmenterTest.mjs @@ -0,0 +1,56 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +console.log("---"); + +console.log("Intl.Segmenter"); + +Intl.Segmenter.supportedLocalesOf([ + "en-US", + "en-GB" + ]); + +Intl.Segmenter.supportedLocalesOf([ + "en-US", + "en-GB" + ], { + localeMatcher: "lookup" + }); + +new Intl.Segmenter(); + +new Intl.Segmenter("en-US"); + +new Intl.Segmenter([ + "en-US", + "en-GB" + ]); + +new Intl.Segmenter(undefined, { + granularity: "word" + }); + +var _formatter = new Intl.Segmenter("en-US", { + granularity: "word" + }); + +var formatter = new Intl.Segmenter(["en-US"], { + granularity: "word" + }); + +console.log(formatter.resolvedOptions()); + +var segments = formatter.segment("This is a sentence with several words"); + +console.log(segments); + +console.log(segments.containing()); + +console.log(segments.containing(1)); + +export { + _formatter , + formatter , + segments , +} +/* Not a pure module */ diff --git a/test/TempTests.mjs b/test/TempTests.mjs index 753da260..4239421f 100644 --- a/test/TempTests.mjs +++ b/test/TempTests.mjs @@ -1,6 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Core__Int from "../src/Core__Int.mjs"; +import * as IntlTests from "./intl/IntlTests.mjs"; import * as Core__Dict from "../src/Core__Dict.mjs"; import * as Core__JSON from "../src/Core__JSON.mjs"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; @@ -93,24 +94,6 @@ console.log(Core__Float.fromString("0.1")); console.info(""); -console.info("Intl"); - -console.info("---"); - -var currencyFormatter = new Intl.NumberFormat("fr-FR", { - currency: "EUR", - style: "currency" - }); - -console.log(Intl.NumberFormat.supportedLocalesOf([ - "fr-FR", - "en-US" - ])); - -console.log(currencyFormatter.format(123.23)); - -console.info(""); - console.info("JSON"); console.info("---"); @@ -306,10 +289,6 @@ if (globalThis.hello !== undefined) { console.log("hello"); } -var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions(); - -var timeZone = resolvedOptions.timeZone; - var z = 1.2 % 1.4; var intFromBigInt = Core__BigInt.toInt(BigInt("10000000000")); @@ -330,12 +309,56 @@ console.log(Core__Int.fromString(undefined, "99999999999999999")); console.log(Core__Int.fromString(2, "010101")); +var _collator = IntlTests._collator; + +var collator = IntlTests.collator; + +var resolvedOptions = IntlTests.resolvedOptions; + +var timeZone = IntlTests.timeZone; + +var _locale = IntlTests._locale; + +var locale = IntlTests.locale; + +var currencyFormatter = IntlTests.currencyFormatter; + +var roundingFormatter = IntlTests.roundingFormatter; + +var groupingFormatter1 = IntlTests.groupingFormatter1; + +var groupingFormatter2 = IntlTests.groupingFormatter2; + +var sigFormatter = IntlTests.sigFormatter; + +var options = IntlTests.options; + +var _formatter = IntlTests._formatter; + +var formatter = IntlTests.formatter; + +var segments = IntlTests.segments; + export { + _collator , + collator , + resolvedOptions , + timeZone , + _locale , + locale , + currencyFormatter , + roundingFormatter , + groupingFormatter1 , + groupingFormatter2 , + sigFormatter , + options , + _formatter , + formatter , + segments , date , dict , dict2 , f , - currencyFormatter , json , map , myObject , @@ -350,8 +373,6 @@ export { x , array$1 as array, timeout , - resolvedOptions , - timeZone , z , intFromBigInt , Bugfix , diff --git a/test/TempTests.res b/test/TempTests.res index 56bdcc40..d2d25f8e 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -1,5 +1,7 @@ open RescriptCore +include IntlTests + Console.info("") Console.info("Array") Console.info("---") @@ -41,17 +43,6 @@ Console.log(10->Int.toFixedWithPrecision(~digits=2)) Console.log("0"->Int.fromString) Console.log("0.1"->Float.fromString) -Console.info("") -Console.info("Intl") -Console.info("---") -let currencyFormatter = Intl.NumberFormat.makeWithLocaleAndOptions( - "fr-FR", - {"currency": "EUR", "style": "currency"}, -) - -Console.log(Intl.NumberFormat.supportedLocalesOf(["fr-FR", "en-US"])) -Console.log(currencyFormatter->Intl.NumberFormat.format(123.23)) - Console.info("") Console.info("JSON") Console.info("---") @@ -191,9 +182,6 @@ if globalThis["hello"] !== undefined { Console.log("hello") } -let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions -let timeZone = resolvedOptions["timeZone"] - let z = Float.mod(1.2, 1.4) let intFromBigInt = BigInt.fromString("10000000000")->BigInt.toInt diff --git a/test/intl/IntlTests.res b/test/intl/IntlTests.res new file mode 100644 index 00000000..d5fa1c6d --- /dev/null +++ b/test/intl/IntlTests.res @@ -0,0 +1,53 @@ +include Intl__CollatorTest +include Intl__DateTimeFormatTest +include Intl__ListFormatTest +include Intl__LocaleTest +include Intl__NumberFormatTest +include Intl__PluralRulesTest +include Intl__RelativeTimeFormatTest +include Intl__SegmenterTest + +open RescriptCore + +Console.log("---") +Console.log("Intl") + +Intl.getCanonicalLocalesExn("EN-US")->Console.log +Intl.getCanonicalLocalesManyExn(["EN-US", "Fr"])->Console.log + +try { + Intl.getCanonicalLocalesExn("bloop")->Console.log +} catch { +| Exn.Error(e) => Console.error(e) +} + +try { + Intl.supportedValuesOfExn("calendar")->Console.log + Intl.supportedValuesOfExn("collation")->Console.log + Intl.supportedValuesOfExn("currency")->Console.log + Intl.supportedValuesOfExn("numberingSystem")->Console.log + Intl.supportedValuesOfExn("timeZone")->Console.log + Intl.supportedValuesOfExn("unit")->Console.log +} catch { +| Exn.Error(e) => Console.error(e) +} + +try { + Intl.supportedValuesOfExn("someInvalidKey")->ignore + + Console.error("Shouldn't have been hit") +} catch { +| Exn.Error(e) => + switch Error.message(e)->Option.map(String.toLowerCase) { + | Some("invalid key : someinvalidkey") => Console.log("Caught expected error") + | message => { + Console.warn(`Unexpected error message: "${message->Option.getUnsafe}"`) + Error.raise(e) + } + } +| e => + switch Error.fromException(e) { + | Some(e) => Error.raise(e) + | None => Console.error("Unexpected error") + } +} diff --git a/test/intl/Intl__CollatorTest.res b/test/intl/Intl__CollatorTest.res new file mode 100644 index 00000000..45ea8d7b --- /dev/null +++ b/test/intl/Intl__CollatorTest.res @@ -0,0 +1,18 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.Collator") + +let _collator = Intl.Collator.make() +let _collator = Intl.Collator.makeWithLocale("en-US") +let _collator = Intl.Collator.makeWithLocales(["en-US", "en-GB"]) +let collator = Intl.Collator.makeWithLocaleAndOptions( + "en-US", + {caseFirst: #upper, sensitivity: #base, ignorePunctuation: true, numeric: true}, +) +Intl.Collator.supportedLocalesOf(["en-US", "en-GB"])->ignore +Intl.Collator.supportedLocalesOfWithOptions(["en-US", "en-GB"], {localeMatcher: #lookup})->ignore + +collator->Intl.Collator.resolvedOptions->Console.log +collator->Intl.Collator.compare("hi", "hï")->Console.log +Intl.Collator.supportedLocalesOf(["hi"])->Console.log diff --git a/test/intl/Intl__DateTimeFormatTest.res b/test/intl/Intl__DateTimeFormatTest.res new file mode 100644 index 00000000..f98b75c1 --- /dev/null +++ b/test/intl/Intl__DateTimeFormatTest.res @@ -0,0 +1,58 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.DateTimeFormat") + +Intl.DateTimeFormat.supportedLocalesOf(["en-US", "en-GB"])->ignore +Intl.DateTimeFormat.supportedLocalesOfWithOptions( + ["en-US", "en-GB"], + {localeMatcher: #lookup}, +)->ignore + +let formatter = Intl.DateTimeFormat.makeWithOptions({dateStyle: #full, timeStyle: #full}) + +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +Console.log( + formatter->Intl.DateTimeFormat.formatRange( + ~startDate=Date.makeWithYMD(~year=2023, ~date=1, ~month=1), + ~endDate=Date.makeWithYMD(~year=2023, ~date=31, ~month=12), + ), +) + +let options: Intl.DateTimeFormat.options = { + hour12: false, + hourCycle: #h24, + timeZone: "UTC", + weekday: #narrow, + era: #narrow, + year: #"2-digit", + month: #"2-digit", + day: #"2-digit", + hour: #"2-digit", + minute: #"2-digit", + second: #"2-digit", + fractionalSecondDigits: #3, + timeZoneName: #longGeneric, +} +let formatter = Intl.DateTimeFormat.makeWithOptions(options) + +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #long}) +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #longOffset}) +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #short}) +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #shortGeneric}) +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #shortOffset}) +Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now()))) + +let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions +let {Intl.DateTimeFormat.timeZone: timeZone, _} = resolvedOptions diff --git a/test/intl/Intl__ListFormatTest.res b/test/intl/Intl__ListFormatTest.res new file mode 100644 index 00000000..8bb9a479 --- /dev/null +++ b/test/intl/Intl__ListFormatTest.res @@ -0,0 +1,24 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.ListFormat") + +let _formatter = Intl.ListFormat.make() +let _formatter = Intl.ListFormat.makeWithLocale("en-US") +let _formatter = Intl.ListFormat.makeWithLocales(["en-US", "en-GB"]) +let _formatter = Intl.ListFormat.makeWithLocaleAndOptions( + "en-US", + {style: #long, \"type": #conjunction}, +) +let _formatter = Intl.ListFormat.makeWithLocalesAndOptions( + ["en-US", "en-GB"], + {style: #long, \"type": #conjunction}, +) +Intl.ListFormat.supportedLocalesOf(["en-US", "en-GB"])->ignore +Intl.ListFormat.supportedLocalesOfWithOptions(["en-US", "en-GB"], {localeMatcher: #lookup})->ignore + +let formatter = Intl.ListFormat.makeWithOptions({style: #long, \"type": #conjunction}) + +formatter->Intl.ListFormat.resolvedOptions->Console.log +formatter->Intl.ListFormat.format(["one", "two", "three"])->Console.log +formatter->Intl.ListFormat.formatToParts(["one", "two", "three"])->Console.log diff --git a/test/intl/Intl__LocaleTest.res b/test/intl/Intl__LocaleTest.res new file mode 100644 index 00000000..543d8004 --- /dev/null +++ b/test/intl/Intl__LocaleTest.res @@ -0,0 +1,29 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.Locale") + +let _locale = Intl.Locale.make("en-US") +let locale = Intl.Locale.makeWithOptions( + "en-US", + { + calendar: #hebrew, + collation: #compat, + hourCycle: #h24, + caseFirst: #upper, + numeric: true, + }, +) + +locale->Intl.Locale.baseName->Console.log +locale->Intl.Locale.calendar->Console.log +locale->Intl.Locale.caseFirst->Console.log +locale->Intl.Locale.collation->Console.log +locale->Intl.Locale.hourCycle->Console.log +locale->Intl.Locale.language->Console.log +locale->Intl.Locale.numberingSystem->Console.log +locale->Intl.Locale.numeric->Console.log +locale->Intl.Locale.region->Console.log +locale->Intl.Locale.script->Console.log +locale->Intl.Locale.maximize->Console.log +locale->Intl.Locale.minimize->Console.log diff --git a/test/intl/Intl__NumberFormatTest.res b/test/intl/Intl__NumberFormatTest.res new file mode 100644 index 00000000..51829077 --- /dev/null +++ b/test/intl/Intl__NumberFormatTest.res @@ -0,0 +1,37 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.NumberFormat") + +let currencyFormatter = Intl.NumberFormat.makeWithLocaleAndOptions( + "fr-FR", + {currency: "EUR", style: #currency}, +) + +Console.log(Intl.NumberFormat.supportedLocalesOf(["fr-FR", "en-US"])) +Console.log(currencyFormatter->Intl.NumberFormat.format(123.23)) + +let roundingFormatter = Intl.NumberFormat.makeWithOptions({ + roundingIncrement: #500, + minimumFractionDigits: #2, + maximumFractionDigits: #2, +}) + +let groupingFormatter1 = Intl.NumberFormat.makeWithOptions({ + useGrouping: Intl.NumberFormat.Grouping.fromBool(true), +}) +let groupingFormatter2 = Intl.NumberFormat.makeWithOptions({ + useGrouping: Intl.NumberFormat.Grouping.fromString(#auto), +}) + +let sigFormatter = Intl.NumberFormat.makeWithOptions({ + minimumIntegerDigits: #1, + minimumFractionDigits: #1, + maximumFractionDigits: #1, + minimumSignificantDigits: #1, + maximumSignificantDigits: #1, +}) + +let options = sigFormatter->Intl.NumberFormat.resolvedOptions +Console.log(options) +options.useGrouping->Core__Intl__NumberFormat.Grouping.parseJsValue->Console.log diff --git a/test/intl/Intl__PluralRulesTest.res b/test/intl/Intl__PluralRulesTest.res new file mode 100644 index 00000000..4f56404c --- /dev/null +++ b/test/intl/Intl__PluralRulesTest.res @@ -0,0 +1,42 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.PluralRules") + +let _formatter = Intl.PluralRules.make() +let _formatter = Intl.PluralRules.makeWithLocale("en-US") +let _formatter = Intl.PluralRules.makeWithLocales(["en-US", "en-GB"]) +let _formatter = Intl.PluralRules.makeWithOptions({ + \"type": #ordinal, + maximumSignificantDigits: #5, + minimumSignificantDigits: #3, +}) +let _formatter = Intl.PluralRules.makeWithLocaleAndOptions( + "en-US", + { + \"type": #ordinal, + maximumSignificantDigits: #5, + minimumSignificantDigits: #3, + }, +) +let formatter = Intl.PluralRules.makeWithLocalesAndOptions( + ["en-US"], + { + \"type": #ordinal, + maximumSignificantDigits: #5, + minimumSignificantDigits: #3, + }, +) + +formatter->Intl.PluralRules.resolvedOptions->Console.log + +formatter->Intl.PluralRules.select(2.0)->Console.log +formatter->Intl.PluralRules.selectInt(2)->Console.log + +// doesn't appear to work in node v16 +// formatter->Intl.PluralRules.selectBigInt(BigInt.fromInt(2))->Console.log +// formatter +// ->Intl.PluralRules.selectRangeBigInt(~start=BigInt.fromInt(1), ~end=BigInt.fromInt(3)) +// ->Console.log +// formatter->Intl.PluralRules.selectRange(~start=1., ~end=3.)->Console.log +// formatter->Intl.PluralRules.selectRangeInt(~start=1, ~end=3)->Console.log diff --git a/test/intl/Intl__RelativeTimeFormatTest.res b/test/intl/Intl__RelativeTimeFormatTest.res new file mode 100644 index 00000000..e8833d7c --- /dev/null +++ b/test/intl/Intl__RelativeTimeFormatTest.res @@ -0,0 +1,37 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.RelativeTimeFormat") + +Intl.RelativeTimeFormat.supportedLocalesOf(["en-US", "en-GB"])->ignore +Intl.RelativeTimeFormat.supportedLocalesOfWithOptions( + ["en-US", "en-GB"], + {localeMatcher: #lookup}, +)->ignore + +let _formatter = Intl.RelativeTimeFormat.make() +let _formatter = Intl.RelativeTimeFormat.makeWithLocale("en-US") +let _formatter = Intl.RelativeTimeFormat.makeWithLocales(["en-US", "en-GB"]) +let _formatter = Intl.RelativeTimeFormat.makeWithOptions({ + numeric: #always, + style: #narrow, +}) +let _formatter = Intl.RelativeTimeFormat.makeWithLocaleAndOptions( + "en-US", + { + numeric: #always, + style: #narrow, + }, +) +let formatter = Intl.RelativeTimeFormat.makeWithLocalesAndOptions( + ["en-US"], + { + numeric: #always, + style: #narrow, + }, +) + +formatter->Intl.RelativeTimeFormat.resolvedOptions->Console.log + +formatter->Intl.RelativeTimeFormat.format(3, #hour)->Console.log +formatter->Intl.RelativeTimeFormat.formatToParts(3, #hour)->Console.log diff --git a/test/intl/Intl__SegmenterTest.res b/test/intl/Intl__SegmenterTest.res new file mode 100644 index 00000000..aaca4153 --- /dev/null +++ b/test/intl/Intl__SegmenterTest.res @@ -0,0 +1,34 @@ +open RescriptCore + +Console.log("---") +Console.log("Intl.Segmenter") + +Intl.Segmenter.supportedLocalesOf(["en-US", "en-GB"])->ignore +Intl.Segmenter.supportedLocalesOfWithOptions(["en-US", "en-GB"], {localeMatcher: #lookup})->ignore + +let _formatter = Intl.Segmenter.make() +let _formatter = Intl.Segmenter.makeWithLocale("en-US") +let _formatter = Intl.Segmenter.makeWithLocales(["en-US", "en-GB"]) +let _formatter = Intl.Segmenter.makeWithOptions({ + granularity: #word, +}) +let _formatter = Intl.Segmenter.makeWithLocaleAndOptions( + "en-US", + { + granularity: #word, + }, +) +let formatter = Intl.Segmenter.makeWithLocalesAndOptions( + ["en-US"], + { + granularity: #word, + }, +) + +formatter->Intl.Segmenter.resolvedOptions->Console.log +let segments = formatter->Intl.Segmenter.segment("This is a sentence with several words") + +Console.log(segments) + +Intl.Segments.containing(segments)->Console.log +Intl.Segments.containingWithIndex(segments, 1)->Console.log