NumberFormatConstructor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 2021-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/NumberFormatConstructor.h>
  11. #include <LibLocale/Locale.h>
  12. namespace JS::Intl {
  13. // 15.1 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
  14. NumberFormatConstructor::NumberFormatConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.NumberFormat.as_string(), *realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void NumberFormatConstructor::initialize(Realm& realm)
  19. {
  20. NativeFunction::initialize(realm);
  21. auto& vm = this->vm();
  22. // 15.2.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().intl_number_format_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. }
  28. // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  29. ThrowCompletionOr<Value> NumberFormatConstructor::call()
  30. {
  31. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  32. return TRY(construct(*this));
  33. }
  34. // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  35. ThrowCompletionOr<Object*> NumberFormatConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto locales = vm.argument(0);
  39. auto options = vm.argument(1);
  40. // 2. Let numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%NumberFormat.prototype%", « [[InitializedNumberFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[Unit]], [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[BoundFormat]] »).
  41. auto* number_format = TRY(ordinary_create_from_constructor<NumberFormat>(vm, new_target, &Intrinsics::intl_number_format_prototype));
  42. // 3. Perform ? InitializeNumberFormat(numberFormat, locales, options).
  43. TRY(initialize_number_format(vm, *number_format, locales, options));
  44. // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  45. // a. Let this be the this value.
  46. // b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).
  47. // 5. Return numberFormat.
  48. return number_format;
  49. }
  50. // 15.2.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
  51. JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
  52. {
  53. auto locales = vm.argument(0);
  54. auto options = vm.argument(1);
  55. // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
  56. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  57. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  58. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  59. return TRY(supported_locales(vm, requested_locales, options));
  60. }
  61. // 15.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/ecma402/#sec-initializenumberformat
  62. // 1.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-initializenumberformat
  63. ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat& number_format, Value locales_value, Value options_value)
  64. {
  65. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  66. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  67. // 2. Set options to ? CoerceOptionsToObject(options).
  68. auto* options = TRY(coerce_options_to_object(vm, options_value));
  69. // 3. Let opt be a new Record.
  70. LocaleOptions opt {};
  71. // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  72. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  73. // 5. Set opt.[[localeMatcher]] to matcher.
  74. opt.locale_matcher = matcher;
  75. // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
  76. auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
  77. // 7. If numberingSystem is not undefined, then
  78. if (!numbering_system.is_undefined()) {
  79. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  80. if (!::Locale::is_type_identifier(numbering_system.as_string().string()))
  81. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  82. // 8. Set opt.[[nu]] to numberingSystem.
  83. opt.nu = numbering_system.as_string().string();
  84. }
  85. // 9. Let localeData be %NumberFormat%.[[LocaleData]].
  86. // 10. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]], requestedLocales, opt, %NumberFormat%.[[RelevantExtensionKeys]], localeData).
  87. auto result = resolve_locale(requested_locales, opt, NumberFormat::relevant_extension_keys());
  88. // 11. Set numberFormat.[[Locale]] to r.[[locale]].
  89. number_format.set_locale(move(result.locale));
  90. // 12. Set numberFormat.[[DataLocale]] to r.[[dataLocale]].
  91. number_format.set_data_locale(move(result.data_locale));
  92. // 13. Set numberFormat.[[NumberingSystem]] to r.[[nu]].
  93. if (result.nu.has_value())
  94. number_format.set_numbering_system(result.nu.release_value());
  95. // 14. Perform ? SetNumberFormatUnitOptions(numberFormat, options).
  96. TRY(set_number_format_unit_options(vm, number_format, *options));
  97. // 15. Let style be numberFormat.[[Style]].
  98. auto style = number_format.style();
  99. int default_min_fraction_digits = 0;
  100. int default_max_fraction_digits = 0;
  101. // 16. If style is "currency", then
  102. if (style == NumberFormat::Style::Currency) {
  103. // a. Let currency be numberFormat.[[Currency]].
  104. auto const& currency = number_format.currency();
  105. // b. Let cDigits be CurrencyDigits(currency).
  106. int digits = currency_digits(currency);
  107. // c. Let mnfdDefault be cDigits.
  108. default_min_fraction_digits = digits;
  109. // d. Let mxfdDefault be cDigits.
  110. default_max_fraction_digits = digits;
  111. }
  112. // 17. Else,
  113. else {
  114. // a. Let mnfdDefault be 0.
  115. default_min_fraction_digits = 0;
  116. // b. If style is "percent", then
  117. // i. Let mxfdDefault be 0.
  118. // c. Else,
  119. // i. Let mxfdDefault be 3.
  120. default_max_fraction_digits = style == NumberFormat::Style::Percent ? 0 : 3;
  121. }
  122. // 18. Let notation be ? GetOption(options, "notation", "string", « "standard", "scientific", "engineering", "compact" », "standard").
  123. auto notation = TRY(get_option(vm, *options, vm.names.notation, OptionType::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv));
  124. // 19. Set numberFormat.[[Notation]] to notation.
  125. number_format.set_notation(notation.as_string().string());
  126. // 20. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
  127. TRY(set_number_format_digit_options(vm, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format.notation()));
  128. // 21. Let roundingIncrement be ? GetNumberOption(options, "roundingIncrement", 1, 5000, 1).
  129. auto rounding_increment = TRY(get_number_option(vm, *options, vm.names.roundingIncrement, 1, 5000, 1));
  130. // 22. If roundingIncrement is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a RangeError exception.
  131. static constexpr auto sanctioned_rounding_increments = AK::Array { 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 };
  132. if (!sanctioned_rounding_increments.span().contains_slow(*rounding_increment))
  133. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrement, *rounding_increment);
  134. // 23. If roundingIncrement is not 1 and numberFormat.[[RoundingType]] is not fractionDigits, throw a TypeError exception.
  135. if ((rounding_increment != 1) && (number_format.rounding_type() != NumberFormatBase::RoundingType::FractionDigits))
  136. return vm.throw_completion<TypeError>(ErrorType::IntlInvalidRoundingIncrementForRoundingType, *rounding_increment, number_format.rounding_type_string());
  137. // 24. If roundingIncrement is not 1 and numberFormat.[[MaximumFractionDigits]] is not equal to numberFormat.[[MinimumFractionDigits]], throw a RangeError exception.
  138. if ((rounding_increment != 1) && (number_format.max_fraction_digits() != number_format.min_fraction_digits()))
  139. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrementForFractionDigits, *rounding_increment);
  140. // 25. Set numberFormat.[[RoundingIncrement]] to roundingIncrement.
  141. number_format.set_rounding_increment(*rounding_increment);
  142. // 26. Let trailingZeroDisplay be ? GetOption(options, "trailingZeroDisplay", "string", « "auto", "stripIfInteger" », "auto").
  143. auto trailing_zero_display = TRY(get_option(vm, *options, vm.names.trailingZeroDisplay, OptionType::String, { "auto"sv, "stripIfInteger"sv }, "auto"sv));
  144. // 27. Set numberFormat.[[TrailingZeroDisplay]] to trailingZeroDisplay.
  145. number_format.set_trailing_zero_display(trailing_zero_display.as_string().string());
  146. // 28. Let compactDisplay be ? GetOption(options, "compactDisplay", "string", « "short", "long" », "short").
  147. auto compact_display = TRY(get_option(vm, *options, vm.names.compactDisplay, OptionType::String, { "short"sv, "long"sv }, "short"sv));
  148. // 29. Let defaultUseGrouping be "auto".
  149. auto default_use_grouping = "auto"sv;
  150. // 30. If notation is "compact", then
  151. if (number_format.notation() == NumberFormat::Notation::Compact) {
  152. // a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
  153. number_format.set_compact_display(compact_display.as_string().string());
  154. // b. Set defaultUseGrouping to "min2".
  155. default_use_grouping = "min2"sv;
  156. }
  157. // 31. Let useGrouping be ? GetStringOrBooleanOption(options, "useGrouping", « "min2", "auto", "always" », "always", false, defaultUseGrouping).
  158. auto use_grouping = TRY(get_string_or_boolean_option(vm, *options, vm.names.useGrouping, { "min2"sv, "auto"sv, "always"sv }, "always"sv, false, default_use_grouping));
  159. // 32. Set numberFormat.[[UseGrouping]] to useGrouping.
  160. number_format.set_use_grouping(use_grouping);
  161. // 33. Let signDisplay be ? GetOption(options, "signDisplay", "string", « "auto", "never", "always", "exceptZero, "negative" », "auto").
  162. auto sign_display = TRY(get_option(vm, *options, vm.names.signDisplay, OptionType::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv, "negative"sv }, "auto"sv));
  163. // 34. Set numberFormat.[[SignDisplay]] to signDisplay.
  164. number_format.set_sign_display(sign_display.as_string().string());
  165. // 35. Let roundingMode be ? GetOption(options, "roundingMode", "string", « "ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven" », "halfExpand").
  166. auto rounding_mode = TRY(get_option(vm, *options, vm.names.roundingMode, OptionType::String, { "ceil"sv, "floor"sv, "expand"sv, "trunc"sv, "halfCeil"sv, "halfFloor"sv, "halfExpand"sv, "halfTrunc"sv, "halfEven"sv }, "halfExpand"sv));
  167. // 36. Set numberFormat.[[RoundingMode]] to roundingMode.
  168. number_format.set_rounding_mode(rounding_mode.as_string().string());
  169. // 37. Return numberFormat.
  170. return &number_format;
  171. }
  172. // 15.1.3 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
  173. // 1.1.1 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-setnfdigitoptions
  174. ThrowCompletionOr<void> set_number_format_digit_options(VM& vm, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation)
  175. {
  176. // 1. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21, 1).
  177. auto min_integer_digits = TRY(get_number_option(vm, options, vm.names.minimumIntegerDigits, 1, 21, 1));
  178. // 2. Let mnfd be ? Get(options, "minimumFractionDigits").
  179. auto min_fraction_digits = TRY(options.get(vm.names.minimumFractionDigits));
  180. // 3. Let mxfd be ? Get(options, "maximumFractionDigits").
  181. auto max_fraction_digits = TRY(options.get(vm.names.maximumFractionDigits));
  182. // 4. Let mnsd be ? Get(options, "minimumSignificantDigits").
  183. auto min_significant_digits = TRY(options.get(vm.names.minimumSignificantDigits));
  184. // 5. Let mxsd be ? Get(options, "maximumSignificantDigits").
  185. auto max_significant_digits = TRY(options.get(vm.names.maximumSignificantDigits));
  186. // 6. Set intlObj.[[MinimumIntegerDigits]] to mnid.
  187. intl_object.set_min_integer_digits(*min_integer_digits);
  188. // 7. Let roundingPriority be ? GetOption(options, "roundingPriority", "string", « "auto", "morePrecision", "lessPrecision" », "auto").
  189. auto rounding_priority = TRY(get_option(vm, options, vm.names.roundingPriority, OptionType::String, { "auto"sv, "morePrecision"sv, "lessPrecision"sv }, "auto"sv));
  190. // 8. If mnsd is not undefined or mxsd is not undefined, then
  191. // a. Let hasSd be true.
  192. // 9. Else,
  193. // a. Let hasSd be false.
  194. bool has_significant_digits = !min_significant_digits.is_undefined() || !max_significant_digits.is_undefined();
  195. // 10. If mnfd is not undefined or mxfd is not undefined, then
  196. // a. Let hasFd be true.
  197. // 11. Else,
  198. // a. Let hasFd be false.
  199. bool has_fraction_digits = !min_fraction_digits.is_undefined() || !max_fraction_digits.is_undefined();
  200. // 12. Let needSd be true.
  201. bool need_significant_digits = true;
  202. // 13. Let needFd be true.
  203. bool need_fraction_digits = true;
  204. // 14. If roundingPriority is "auto", then
  205. if (rounding_priority.as_string().string() == "auto"sv) {
  206. // a. Set needSd to hasSd.
  207. need_significant_digits = has_significant_digits;
  208. // b. If hasSd is true, or hasFd is false and notation is "compact", then
  209. if (has_significant_digits || (!has_fraction_digits && notation == NumberFormat::Notation::Compact)) {
  210. // i. Set needFd to false.
  211. need_fraction_digits = false;
  212. }
  213. }
  214. // 15. If needSd is true, then
  215. if (need_significant_digits) {
  216. // a. If hasSd is true, then
  217. if (has_significant_digits) {
  218. // i. Set mnsd to ? DefaultNumberOption(mnsd, 1, 21, 1).
  219. auto min_digits = TRY(default_number_option(vm, min_significant_digits, 1, 21, 1));
  220. // ii. Set mxsd to ? DefaultNumberOption(mxsd, mnsd, 21, 21).
  221. auto max_digits = TRY(default_number_option(vm, max_significant_digits, *min_digits, 21, 21));
  222. // iii. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
  223. intl_object.set_min_significant_digits(*min_digits);
  224. // iv. Set intlObj.[[MaximumSignificantDigits]] to mxsd.
  225. intl_object.set_max_significant_digits(*max_digits);
  226. }
  227. // b. Else,
  228. else {
  229. // i. Set intlObj.[[MinimumSignificantDigits]] to 1.
  230. intl_object.set_min_significant_digits(1);
  231. // ii. Set intlObj.[[MaximumSignificantDigits]] to 21.
  232. intl_object.set_max_significant_digits(21);
  233. }
  234. }
  235. // 16. If needFd is true, then
  236. if (need_fraction_digits) {
  237. // a. If hasFd is true, then
  238. if (has_fraction_digits) {
  239. // i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 20, undefined).
  240. auto min_digits = TRY(default_number_option(vm, min_fraction_digits, 0, 20, {}));
  241. // ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 20, undefined).
  242. auto max_digits = TRY(default_number_option(vm, max_fraction_digits, 0, 20, {}));
  243. // iii. If mnfd is undefined, set mnfd to min(mnfdDefault, mxfd).
  244. if (!min_digits.has_value())
  245. min_digits = min(default_min_fraction_digits, *max_digits);
  246. // iv. Else if mxfd is undefined, set mxfd to max(mxfdDefault, mnfd).
  247. else if (!max_digits.has_value())
  248. max_digits = max(default_max_fraction_digits, *min_digits);
  249. // v. Else if mnfd is greater than mxfd, throw a RangeError exception.
  250. else if (*min_digits > *max_digits)
  251. return vm.throw_completion<RangeError>(ErrorType::IntlMinimumExceedsMaximum, *min_digits, *max_digits);
  252. // vi. Set intlObj.[[MinimumFractionDigits]] to mnfd.
  253. intl_object.set_min_fraction_digits(*min_digits);
  254. // vii. Set intlObj.[[MaximumFractionDigits]] to mxfd.
  255. intl_object.set_max_fraction_digits(*max_digits);
  256. }
  257. // b. Else,
  258. else {
  259. // i. Set intlObj.[[MinimumFractionDigits]] to mnfdDefault.
  260. intl_object.set_min_fraction_digits(default_min_fraction_digits);
  261. // ii. Set intlObj.[[MaximumFractionDigits]] to mxfdDefault.
  262. intl_object.set_max_fraction_digits(default_max_fraction_digits);
  263. }
  264. }
  265. // 17. If needSd is true or needFd is true, then
  266. if (need_significant_digits || need_fraction_digits) {
  267. // a. If roundingPriority is "morePrecision", then
  268. if (rounding_priority.as_string().string() == "morePrecision"sv) {
  269. // i. Set intlObj.[[RoundingType]] to morePrecision.
  270. intl_object.set_rounding_type(NumberFormatBase::RoundingType::MorePrecision);
  271. }
  272. // b. Else if roundingPriority is "lessPrecision", then
  273. else if (rounding_priority.as_string().string() == "lessPrecision"sv) {
  274. // i. Set intlObj.[[RoundingType]] to lessPrecision.
  275. intl_object.set_rounding_type(NumberFormatBase::RoundingType::LessPrecision);
  276. }
  277. // c. Else if hasSd is true, then
  278. else if (has_significant_digits) {
  279. // i. Set intlObj.[[RoundingType]] to significantDigits.
  280. intl_object.set_rounding_type(NumberFormatBase::RoundingType::SignificantDigits);
  281. }
  282. // d. Else,
  283. else {
  284. // i. Set intlObj.[[RoundingType]] to fractionDigits.
  285. intl_object.set_rounding_type(NumberFormatBase::RoundingType::FractionDigits);
  286. }
  287. }
  288. // 18. Else,
  289. else {
  290. // a. Set intlObj.[[RoundingType]] to morePrecision.
  291. intl_object.set_rounding_type(NumberFormatBase::RoundingType::MorePrecision);
  292. // b. Set intlObj.[[MinimumFractionDigits]] to 0.
  293. intl_object.set_min_fraction_digits(0);
  294. // c. Set intlObj.[[MaximumFractionDigits]] to 0.
  295. intl_object.set_max_fraction_digits(0);
  296. // d. Set intlObj.[[MinimumSignificantDigits]] to 1.
  297. intl_object.set_min_significant_digits(1);
  298. // e. Set intlObj.[[MaximumSignificantDigits]] to 2.
  299. intl_object.set_max_significant_digits(2);
  300. }
  301. return {};
  302. }
  303. // 15.1.4 SetNumberFormatUnitOptions ( intlObj, options ), https://tc39.es/ecma402/#sec-setnumberformatunitoptions
  304. ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& intl_object, Object const& options)
  305. {
  306. // 1. Assert: Type(intlObj) is Object.
  307. // 2. Assert: Type(options) is Object.
  308. // 3. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal").
  309. auto style = TRY(get_option(vm, options, vm.names.style, OptionType::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv));
  310. // 4. Set intlObj.[[Style]] to style.
  311. intl_object.set_style(style.as_string().string());
  312. // 5. Let currency be ? GetOption(options, "currency", "string", undefined, undefined).
  313. auto currency = TRY(get_option(vm, options, vm.names.currency, OptionType::String, {}, Empty {}));
  314. // 6. If currency is undefined, then
  315. if (currency.is_undefined()) {
  316. // a. If style is "currency", throw a TypeError exception.
  317. if (intl_object.style() == NumberFormat::Style::Currency)
  318. return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "currency"sv, "style"sv, style);
  319. }
  320. // 7. Else,
  321. // a. If ! IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
  322. else if (!is_well_formed_currency_code(currency.as_string().string()))
  323. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, currency, "currency"sv);
  324. // 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol").
  325. auto currency_display = TRY(get_option(vm, options, vm.names.currencyDisplay, OptionType::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv));
  326. // 9. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard").
  327. auto currency_sign = TRY(get_option(vm, options, vm.names.currencySign, OptionType::String, { "standard"sv, "accounting"sv }, "standard"sv));
  328. // 10. Let unit be ? GetOption(options, "unit", "string", undefined, undefined).
  329. auto unit = TRY(get_option(vm, options, vm.names.unit, OptionType::String, {}, Empty {}));
  330. // 11. If unit is undefined, then
  331. if (unit.is_undefined()) {
  332. // a. If style is "unit", throw a TypeError exception.
  333. if (intl_object.style() == NumberFormat::Style::Unit)
  334. return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "unit"sv, "style"sv, style);
  335. }
  336. // 12. Else,
  337. // a. If ! IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
  338. else if (!is_well_formed_unit_identifier(unit.as_string().string()))
  339. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, unit, "unit"sv);
  340. // 13. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short").
  341. auto unit_display = TRY(get_option(vm, options, vm.names.unitDisplay, OptionType::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv));
  342. // 14. If style is "currency", then
  343. if (intl_object.style() == NumberFormat::Style::Currency) {
  344. // a. Set intlObj.[[Currency]] to the ASCII-uppercase of currency.
  345. intl_object.set_currency(currency.as_string().string().to_uppercase());
  346. // c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
  347. intl_object.set_currency_display(currency_display.as_string().string());
  348. // d. Set intlObj.[[CurrencySign]] to currencySign.
  349. intl_object.set_currency_sign(currency_sign.as_string().string());
  350. }
  351. // 15. If style is "unit", then
  352. if (intl_object.style() == NumberFormat::Style::Unit) {
  353. // a. Set intlObj.[[Unit]] to unit.
  354. intl_object.set_unit(unit.as_string().string());
  355. // b. Set intlObj.[[UnitDisplay]] to unitDisplay.
  356. intl_object.set_unit_display(unit_display.as_string().string());
  357. }
  358. return {};
  359. }
  360. }