2022-01-28 12:56:04 -05:00
|
|
|
|
/*
|
2024-06-09 14:36:48 -04:00
|
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
2022-01-28 12:56:04 -05:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Intl/PluralRules.h>
|
|
|
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
|
2023-11-19 09:45:05 +01:00
|
|
|
|
JS_DEFINE_ALLOCATOR(PluralRules);
|
|
|
|
|
|
2022-01-28 12:56:04 -05:00
|
|
|
|
// 16 PluralRules Objects, https://tc39.es/ecma402/#pluralrules-objects
|
|
|
|
|
PluralRules::PluralRules(Object& prototype)
|
|
|
|
|
: NumberFormatBase(prototype)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 16.5.4 ResolvePlural ( pluralRules, n ), https://tc39.es/ecma402/#sec-resolveplural
|
|
|
|
|
::Locale::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 }.
|
|
|
|
|
return ::Locale::PluralCategory::Other;
|
2022-07-07 10:01:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 2. Let locale be pluralRules.[[Locale]].
|
|
|
|
|
// 3. Let type be pluralRules.[[Type]].
|
|
|
|
|
// 4. Let res be FormatNumericToString(pluralRules, ℝ(n)).
|
|
|
|
|
// 5. Let s be res.[[FormattedString]].
|
|
|
|
|
// 6. Let operands be GetOperands(s).
|
|
|
|
|
// 7. Let p be PluralRuleSelect(locale, type, n, operands).
|
|
|
|
|
// 8. Return the Record { [[PluralCategory]]: p, [[FormattedString]]: s }.
|
|
|
|
|
return plural_rules.formatter().select_plural(number.as_double());
|
2022-07-07 10:01:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 14:15:28 -04:00
|
|
|
|
// 16.5.6 ResolvePluralRange ( pluralRules, x, y ), https://tc39.es/ecma402/#sec-resolveplural
|
2022-09-02 12:01:10 -04:00
|
|
|
|
ThrowCompletionOr<::Locale::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]].
|
|
|
|
|
// 7. Return PluralRuleSelectRange(locale, type, xp.[[PluralCategory]], yp.[[PluralCategory]]).
|
|
|
|
|
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
|
|
|
|
}
|