CollatorConstructor.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/Collator.h>
  11. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  12. #include <LibUnicode/Locale.h>
  13. namespace JS::Intl {
  14. // 10.1.2 InitializeCollator ( collator, locales, options ), https://tc39.es/ecma402/#sec-initializecollator
  15. static ThrowCompletionOr<Collator*> initialize_collator(GlobalObject& global_object, Collator& collator, Value locales_value, Value options_value)
  16. {
  17. auto& vm = global_object.vm();
  18. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  19. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
  20. // 2. Set options to ? CoerceOptionsToObject(options).
  21. auto* options = TRY(coerce_options_to_object(global_object, options_value));
  22. // 3. Let usage be ? GetOption(options, "usage", "string", « "sort", "search" », "sort").
  23. auto usage = TRY(get_option(global_object, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv));
  24. // 4. Set collator.[[Usage]] to usage.
  25. collator.set_usage(usage.as_string().string());
  26. // 5. If usage is "sort", then
  27. // a. Let localeData be %Collator%.[[SortLocaleData]].
  28. // 6. Else,
  29. // a. Let localeData be %Collator%.[[SearchLocaleData]].
  30. // 7. Let opt be a new Record.
  31. LocaleOptions opt {};
  32. // 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  33. auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  34. // 9. Set opt.[[localeMatcher]] to matcher.
  35. opt.locale_matcher = matcher;
  36. // 10. Let collation be ? GetOption(options, "collation", "string", undefined, undefined).
  37. auto collation = TRY(get_option(global_object, *options, vm.names.collation, OptionType::String, {}, Empty {}));
  38. // 11. If collation is not undefined, then
  39. if (!collation.is_undefined()) {
  40. // a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  41. if (!Unicode::is_type_identifier(collation.as_string().string()))
  42. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, collation, "collation"sv);
  43. // 12. Set opt.[[co]] to collation.
  44. opt.co = collation.as_string().string();
  45. }
  46. // 13. Let numeric be ? GetOption(options, "numeric", "boolean", undefined, undefined).
  47. auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {}));
  48. // 14. If numeric is not undefined, then
  49. // a. Let numeric be ! ToString(numeric).
  50. // 15. Set opt.[[kn]] to numeric.
  51. if (!numeric.is_undefined())
  52. opt.kn = MUST(numeric.to_string(global_object));
  53. // 16. Let caseFirst be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined).
  54. // 17. Set opt.[[kf]] to caseFirst.
  55. auto case_first = TRY(get_option(global_object, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
  56. if (!case_first.is_undefined())
  57. opt.kf = case_first.as_string().string();
  58. // 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
  59. auto relevant_extension_keys = Collator::relevant_extension_keys();
  60. // 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
  61. auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
  62. // 20. Set collator.[[Locale]] to r.[[locale]].
  63. collator.set_locale(move(result.locale));
  64. // 21. Let collation be r.[[co]].
  65. // 22. If collation is null, let collation be "default".
  66. // 23. Set collator.[[Collation]] to collation.
  67. collator.set_collation(result.co.has_value() ? result.co.release_value() : "default");
  68. // 24. If relevantExtensionKeys contains "kn", then
  69. if (relevant_extension_keys.span().contains_slow("kn"sv) && result.kn.has_value()) {
  70. // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
  71. collator.set_numeric(same_value(js_string(vm, result.kn.release_value()), js_string(vm, "true"sv)));
  72. }
  73. // 25. If relevantExtensionKeys contains "kf", then
  74. if (relevant_extension_keys.span().contains_slow("kf"sv) && result.kf.has_value()) {
  75. // a. Set collator.[[CaseFirst]] to r.[[kf]].
  76. collator.set_case_first(result.kf.release_value());
  77. }
  78. // 26. Let sensitivity be ? GetOption(options, "sensitivity", "string", « "base", "accent", "case", "variant" », undefined).
  79. auto sensitivity = TRY(get_option(global_object, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
  80. // 27. If sensitivity is undefined, then
  81. if (sensitivity.is_undefined()) {
  82. // a. If usage is "sort", then
  83. if (collator.usage() == Collator::Usage::Sort) {
  84. // i. Let sensitivity be "variant".
  85. sensitivity = js_string(vm, "variant"sv);
  86. }
  87. // b. Else,
  88. else {
  89. // i. Let dataLocale be r.[[dataLocale]].
  90. // ii. Let dataLocaleData be localeData.[[<dataLocale>]].
  91. // iii. Let sensitivity be dataLocaleData.[[sensitivity]].
  92. sensitivity = js_string(vm, "base"sv);
  93. }
  94. }
  95. // 28. Set collator.[[Sensitivity]] to sensitivity.
  96. collator.set_sensitivity(sensitivity.as_string().string());
  97. // 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", "boolean", undefined, false).
  98. auto ignore_punctuation = TRY(get_option(global_object, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, false));
  99. // 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
  100. collator.set_ignore_punctuation(ignore_punctuation.as_bool());
  101. // 31. Return collator.
  102. return &collator;
  103. }
  104. // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
  105. CollatorConstructor::CollatorConstructor(Realm& realm)
  106. : NativeFunction(vm().names.Collator.as_string(), *realm.global_object().function_prototype())
  107. {
  108. }
  109. void CollatorConstructor::initialize(Realm& realm)
  110. {
  111. NativeFunction::initialize(realm);
  112. auto& vm = this->vm();
  113. // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype
  114. define_direct_property(vm.names.prototype, realm.global_object().intl_collator_prototype(), 0);
  115. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  116. u8 attr = Attribute::Writable | Attribute::Configurable;
  117. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  118. }
  119. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  120. ThrowCompletionOr<Value> CollatorConstructor::call()
  121. {
  122. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget
  123. return TRY(construct(*this));
  124. }
  125. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  126. ThrowCompletionOr<Object*> CollatorConstructor::construct(FunctionObject& new_target)
  127. {
  128. auto& vm = this->vm();
  129. auto& global_object = this->global_object();
  130. auto locales = vm.argument(0);
  131. auto options = vm.argument(1);
  132. // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
  133. // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
  134. // a. Append [[Numeric]] as the last element of internalSlotsList.
  135. // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
  136. // a. Append [[CaseFirst]] as the last element of internalSlotsList.
  137. // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList).
  138. auto* collator = TRY(ordinary_create_from_constructor<Collator>(global_object, new_target, &GlobalObject::intl_collator_prototype));
  139. // 6. Return ? InitializeCollator(collator, locales, options).
  140. return TRY(initialize_collator(global_object, *collator, locales, options));
  141. }
  142. // 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof
  143. JS_DEFINE_NATIVE_FUNCTION(CollatorConstructor::supported_locales_of)
  144. {
  145. auto locales = vm.argument(0);
  146. auto options = vm.argument(1);
  147. // 1. Let availableLocales be %Collator%.[[AvailableLocales]].
  148. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  149. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  150. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  151. return TRY(supported_locales(global_object, requested_locales, options));
  152. }
  153. }