2022-01-28 12:56:04 -05:00
|
|
|
|
/*
|
2025-02-28 08:54:42 -05:00
|
|
|
|
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
|
2022-01-28 12:56:04 -05:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Intl/PluralRules.h>
|
2025-02-28 12:15:49 -05:00
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2022-01-28 12:56:04 -05:00
|
|
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(PluralRules);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
|
// 17 PluralRules Objects, https://tc39.es/ecma402/#pluralrules-objects
|
2022-01-28 12:56:04 -05:00
|
|
|
|
PluralRules::PluralRules(Object& prototype)
|
|
|
|
|
: NumberFormatBase(prototype)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 15:56:31 -04:00
|
|
|
|
// 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl.pluralrules-internal-slots
|
|
|
|
|
ReadonlySpan<StringView> PluralRules::relevant_extension_keys() const
|
|
|
|
|
{
|
|
|
|
|
// The value of the [[RelevantExtensionKeys]] internal slot is « ».
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl.pluralrules-internal-slots
|
|
|
|
|
ReadonlySpan<ResolutionOptionDescriptor> PluralRules::resolution_option_descriptors(VM&) const
|
|
|
|
|
{
|
|
|
|
|
// The value of the [[ResolutionOptionDescriptors]] internal slot is « ».
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
|
// 17.5.2 ResolvePlural ( pluralRules, n ), https://tc39.es/ecma402/#sec-resolveplural
|
2024-06-23 09:14:27 -04:00
|
|
|
|
Unicode::PluralCategory resolve_plural(PluralRules const& plural_rules, Value number)
|
2022-07-07 10:01:00 -04:00
|
|
|
|
{
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 1. If n is not a finite Number, then
|
2022-07-07 10:01:00 -04:00
|
|
|
|
if (!number.is_finite_number()) {
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// a. Let s be ! ToString(n).
|
|
|
|
|
// b. Return the Record { [[PluralCategory]]: "other", [[FormattedString]]: s }.
|
2024-06-23 09:14:27 -04:00
|
|
|
|
return Unicode::PluralCategory::Other;
|
2022-07-07 10:01:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 13:52:05 -05:00
|
|
|
|
// 2. Let res be FormatNumericToString(pluralRules, ℝ(n)).
|
|
|
|
|
// 3. Let s be res.[[FormattedString]].
|
|
|
|
|
// 4. Let locale be pluralRules.[[Locale]].
|
|
|
|
|
// 5. Let type be pluralRules.[[Type]].
|
2025-05-27 08:05:19 -04:00
|
|
|
|
// 6. Let notation be pluralRules.[[Notation]].
|
|
|
|
|
// 7. Let p be PluralRuleSelect(locale, type, notation, s).
|
|
|
|
|
// 8. Return the Record { [[PluralCategory]]: p, [[FormattedString]]: s }.
|
2024-06-14 14:15:28 -04:00
|
|
|
|
return plural_rules.formatter().select_plural(number.as_double());
|
2022-07-07 10:01:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
|
// 17.5.4 ResolvePluralRange ( pluralRules, x, y ), https://tc39.es/ecma402/#sec-resolveplural
|
2024-06-23 09:14:27 -04:00
|
|
|
|
ThrowCompletionOr<Unicode::PluralCategory> resolve_plural_range(VM& vm, PluralRules const& plural_rules, Value start, Value end)
|
2022-07-11 11:28:10 -04:00
|
|
|
|
{
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 1. If x is NaN or y is NaN, throw a RangeError exception.
|
2022-07-11 11:28:10 -04:00
|
|
|
|
if (start.is_nan())
|
2023-06-25 10:11:37 -04:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::NumberIsNaN, "start"sv);
|
2022-07-11 11:28:10 -04:00
|
|
|
|
if (end.is_nan())
|
2023-06-25 10:11:37 -04:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::NumberIsNaN, "end"sv);
|
2022-07-11 11:28:10 -04:00
|
|
|
|
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 2. Let xp be ResolvePlural(pluralRules, x).
|
|
|
|
|
// 3. Let yp be ResolvePlural(pluralRules, y).
|
|
|
|
|
// 4. If xp.[[FormattedString]] is yp.[[FormattedString]], then
|
|
|
|
|
// a. Return xp.[[PluralCategory]].
|
|
|
|
|
// 5. Let locale be pluralRules.[[Locale]].
|
|
|
|
|
// 6. Let type be pluralRules.[[Type]].
|
2025-05-27 08:05:19 -04:00
|
|
|
|
// 7. Let notation be pluralRules.[[Notation]].
|
|
|
|
|
// 8. Return PluralRuleSelectRange(locale, type, notation, xp.[[PluralCategory]], yp.[[PluralCategory]]).
|
2024-06-14 14:15:28 -04:00
|
|
|
|
return plural_rules.formatter().select_plural_range(start.as_double(), end.as_double());
|
2022-07-11 11:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 12:56:04 -05:00
|
|
|
|
}
|