2022-01-25 10:41:57 -05:00
/*
2025-02-28 08:54:42 -05:00
* Copyright ( c ) 2022 - 2025 , Tim Flynn < trflynn89 @ ladybird . org >
2022-01-25 10:41:57 -05:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <LibJS/Runtime/AbstractOperations.h>
2022-01-25 12:11:56 -05:00
# include <LibJS/Runtime/Array.h>
2022-01-25 10:41:57 -05:00
# include <LibJS/Runtime/GlobalObject.h>
2022-01-25 12:11:56 -05:00
# include <LibJS/Runtime/Intl/AbstractOperations.h>
2022-01-25 10:41:57 -05:00
# include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
# include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
2024-06-23 09:14:27 -04:00
# include <LibUnicode/Locale.h>
2022-01-25 10:41:57 -05:00
namespace JS : : Intl {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( RelativeTimeFormatConstructor ) ;
2023-11-19 09:45:05 +01:00
2025-02-28 08:54:42 -05:00
// 18.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
2022-08-16 00:20:49 +01:00
RelativeTimeFormatConstructor : : RelativeTimeFormatConstructor ( Realm & realm )
2023-04-13 00:47:15 +02:00
: NativeFunction ( realm . vm ( ) . names . RelativeTimeFormat . as_string ( ) , realm . intrinsics ( ) . function_prototype ( ) )
2022-01-25 10:41:57 -05:00
{
}
2023-08-07 08:41:28 +02:00
void RelativeTimeFormatConstructor : : initialize ( Realm & realm )
2022-01-25 10:41:57 -05:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2022-01-25 10:41:57 -05:00
auto & vm = this - > vm ( ) ;
2025-02-28 08:54:42 -05:00
// 18.2.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype
2022-08-27 00:54:55 +01:00
define_direct_property ( vm . names . prototype , realm . intrinsics ( ) . intl_relative_time_format_prototype ( ) , 0 ) ;
2022-01-25 10:41:57 -05:00
define_direct_property ( vm . names . length , Value ( 0 ) , Attribute : : Configurable ) ;
2022-01-25 12:11:56 -05:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2022-08-22 21:47:35 +01:00
define_native_function ( realm , vm . names . supportedLocalesOf , supported_locales_of , 1 , attr ) ;
2022-01-25 10:41:57 -05:00
}
2025-02-28 08:54:42 -05:00
// 18.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
2022-01-25 10:41:57 -05:00
ThrowCompletionOr < Value > RelativeTimeFormatConstructor : : call ( )
{
// 1. If NewTarget is undefined, throw a TypeError exception.
2022-08-16 20:33:17 +01:00
return vm ( ) . throw_completion < TypeError > ( ErrorType : : ConstructorWithoutNew , " Intl.RelativeTimeFormat " ) ;
2022-01-25 10:41:57 -05:00
}
2025-02-28 08:54:42 -05:00
// 18.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
2024-11-15 04:01:23 +13:00
ThrowCompletionOr < GC : : Ref < Object > > RelativeTimeFormatConstructor : : construct ( FunctionObject & new_target )
2022-01-25 10:41:57 -05:00
{
2022-01-25 11:50:14 -05:00
auto & vm = this - > vm ( ) ;
2022-01-25 10:41:57 -05:00
2024-06-14 11:54:37 -04:00
auto locales_value = vm . argument ( 0 ) ;
auto options_value = vm . argument ( 1 ) ;
2022-01-25 11:50:14 -05:00
2024-08-15 15:55:33 -04:00
// 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.RelativeTimeFormat.prototype%", « [[InitializedRelativeTimeFormat]], [[Locale]], [[LocaleData]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »).
2022-12-14 18:34:32 +00:00
auto relative_time_format = TRY ( ordinary_create_from_constructor < RelativeTimeFormat > ( vm , new_target , & Intrinsics : : intl_relative_time_format_prototype ) ) ;
2022-01-25 10:41:57 -05:00
2024-06-14 11:54:37 -04:00
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
2022-08-20 08:25:24 +01:00
auto requested_locales = TRY ( canonicalize_locale_list ( vm , locales_value ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 4. Set options to ? CoerceOptionsToObject(options).
2022-08-20 08:25:24 +01:00
auto * options = TRY ( coerce_options_to_object ( vm , options_value ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 5. Let opt be a new Record.
2022-03-15 11:09:11 -04:00
LocaleOptions opt { } ;
2024-12-06 12:15:55 -05:00
// 6. Let matcher be ? GetOption(options, "localeMatcher", STRING, « "lookup", "best fit" », "best fit").
2022-08-20 08:52:42 +01:00
auto matcher = TRY ( get_option ( vm , * options , vm . names . localeMatcher , OptionType : : String , AK : : Array { " lookup " sv , " best fit " sv } , " best fit " sv ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 7. Set opt.[[LocaleMatcher]] to matcher.
2022-03-15 11:09:11 -04:00
opt . locale_matcher = matcher ;
2024-12-06 12:15:55 -05:00
// 8. Let numberingSystem be ? GetOption(options, "numberingSystem", STRING, EMPTY, undefined).
2022-08-20 08:52:42 +01:00
auto numbering_system = TRY ( get_option ( vm , * options , vm . names . numberingSystem , OptionType : : String , { } , Empty { } ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 9. If numberingSystem is not undefined, then
2022-03-15 11:09:11 -04:00
if ( ! numbering_system . is_undefined ( ) ) {
2024-06-14 11:54:37 -04:00
// a. If numberingSystem cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
2024-06-23 09:14:27 -04:00
if ( ! Unicode : : is_type_identifier ( numbering_system . as_string ( ) . utf8_string_view ( ) ) )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < RangeError > ( ErrorType : : OptionIsNotValidValue , numbering_system , " numberingSystem " sv ) ;
2022-03-15 11:09:11 -04:00
}
2024-06-16 15:36:34 -04:00
// 10. Set opt.[[nu]] to numberingSystem.
opt . nu = locale_key_from_value ( numbering_system ) ;
2024-06-14 11:54:37 -04:00
// 11. Let r be ResolveLocale(%Intl.RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.RelativeTimeFormat%.[[RelevantExtensionKeys]], %Intl.RelativeTimeFormat%.[[LocaleData]]).
2025-04-07 15:56:31 -04:00
auto result = resolve_locale ( requested_locales , opt , relative_time_format - > relevant_extension_keys ( ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 12. Let locale be r.[[Locale]].
2022-03-15 11:09:11 -04:00
auto locale = move ( result . locale ) ;
2024-06-14 11:54:37 -04:00
// 13. Set relativeTimeFormat.[[Locale]] to locale.
relative_time_format - > set_locale ( locale ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 14. Set relativeTimeFormat.[[LocaleData]] to r.[[LocaleData]].
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 15. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
2024-06-16 15:36:34 -04:00
if ( auto * resolved_numbering_system = result . nu . get_pointer < String > ( ) )
relative_time_format - > set_numbering_system ( move ( * resolved_numbering_system ) ) ;
2022-03-15 11:09:11 -04:00
2024-12-06 12:15:55 -05:00
// 16. Let style be ? GetOption(options, "style", STRING, « "long", "short", "narrow" », "long").
2022-08-20 08:52:42 +01:00
auto style = TRY ( get_option ( vm , * options , vm . names . style , OptionType : : String , { " long " sv , " short " sv , " narrow " sv } , " long " sv ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 17. Set relativeTimeFormat.[[Style]] to style.
relative_time_format - > set_style ( style . as_string ( ) . utf8_string_view ( ) ) ;
2022-03-15 11:09:11 -04:00
2024-12-06 12:15:55 -05:00
// 18. Let numeric be ? GetOption(options, "numeric", STRING, « "always", "auto" », "always").
2022-08-20 08:52:42 +01:00
auto numeric = TRY ( get_option ( vm , * options , vm . names . numeric , OptionType : : String , { " always " sv , " auto " sv } , " always " sv ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 19. Set relativeTimeFormat.[[Numeric]] to numeric.
relative_time_format - > set_numeric ( numeric . as_string ( ) . utf8_string_view ( ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 20. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%Intl.NumberFormat%, « locale »).
// 21. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%Intl.PluralRules%, « locale »).
2024-06-23 09:14:27 -04:00
auto formatter = Unicode : : RelativeTimeFormat : : create (
2025-03-17 16:24:09 -04:00
result . icu_locale ,
2024-06-14 11:54:37 -04:00
relative_time_format - > style ( ) ) ;
relative_time_format - > set_formatter ( move ( formatter ) ) ;
2022-03-15 11:09:11 -04:00
2024-06-14 11:54:37 -04:00
// 22. Return relativeTimeFormat.
2023-08-30 12:41:11 -04:00
return relative_time_format ;
2022-03-15 11:09:11 -04:00
}
2025-02-28 08:54:42 -05:00
// 18.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
2024-06-14 11:54:37 -04:00
JS_DEFINE_NATIVE_FUNCTION ( RelativeTimeFormatConstructor : : supported_locales_of )
{
auto locales = vm . argument ( 0 ) ;
auto options = vm . argument ( 1 ) ;
// 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]].
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY ( canonicalize_locale_list ( vm , locales ) ) ;
2024-06-18 10:13:30 -04:00
// 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
return TRY ( filter_locales ( vm , requested_locales , options ) ) ;
2024-06-14 11:54:37 -04:00
}
2022-01-25 10:41:57 -05:00
}