1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Implement Intl.PluralRules.prototype.selectRange

This commit is contained in:
Timothy Flynn 2022-07-11 11:28:10 -04:00 committed by Linus Groh
parent a337b059dd
commit f089c11b5b
7 changed files with 150 additions and 0 deletions

View file

@ -130,4 +130,46 @@ Unicode::PluralCategory resolve_plural(GlobalObject& global_object, NumberFormat
return plural_rule_select(locale, type, number, move(operands));
}
// 1.1.5 PluralRuleSelectRange ( locale, type, xp, yp ), https://tc39.es/proposal-intl-numberformat-v3/out/pluralrules/proposed.html#sec-pluralruleselectrange
Unicode::PluralCategory plural_rule_select_range(StringView locale, Unicode::PluralForm, Unicode::PluralCategory start, Unicode::PluralCategory end)
{
return Unicode::determine_plural_range(locale, start, end);
}
// 1.1.6 ResolvePluralRange ( pluralRules, x, y ), https://tc39.es/proposal-intl-numberformat-v3/out/pluralrules/proposed.html#sec-resolvepluralrange
ThrowCompletionOr<Unicode::PluralCategory> resolve_plural_range(GlobalObject& global_object, PluralRules const& plural_rules, Value start, Value end)
{
auto& vm = global_object.vm();
// 1. Assert: Type(pluralRules) is Object.
// 2. Assert: pluralRules has an [[InitializedPluralRules]] internal slot.
// 3. Assert: Type(x) is Number.
// 4. Assert: Type(y) is Number.
// 5. If x is NaN or y is NaN, throw a RangeError exception.
if (start.is_nan())
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNumberIsNaN, "start"sv);
if (end.is_nan())
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNumberIsNaN, "end"sv);
// 6. If x > y, throw a RangeError exception.
if (start.as_double() > end.as_double())
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlStartRangeAfterEndRange, start, end);
// 7. Let xp be ! ResolvePlural(pluralRules, x).
auto start_plurality = resolve_plural(global_object, plural_rules, start);
// 8. Let yp be ! ResolvePlural(pluralRules, y).
auto end_plurality = resolve_plural(global_object, plural_rules, end);
// 9. Let locale be pluralRules.[[Locale]].
auto const& locale = plural_rules.locale();
// 10. Let type be pluralRules.[[Type]].
auto type = plural_rules.type();
// 11. Return ! PluralRuleSelectRange(locale, type, xp, yp).
return plural_rule_select_range(locale, type, start_plurality, end_plurality);
}
}

View file

@ -8,6 +8,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Intl/NumberFormat.h>
#include <LibJS/Runtime/Object.h>
#include <LibUnicode/PluralRules.h>
@ -33,5 +34,7 @@ Unicode::PluralOperands get_operands(String const& string);
Unicode::PluralCategory plural_rule_select(StringView locale, Unicode::PluralForm type, Value number, Unicode::PluralOperands operands);
Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules const& plural_rules, Value number);
Unicode::PluralCategory resolve_plural(GlobalObject& global_object, NumberFormatBase const& number_format, Unicode::PluralForm type, Value number);
Unicode::PluralCategory plural_rule_select_range(StringView locale, Unicode::PluralForm, Unicode::PluralCategory start, Unicode::PluralCategory end);
ThrowCompletionOr<Unicode::PluralCategory> resolve_plural_range(GlobalObject& global_object, PluralRules const& plural_rules, Value start, Value end);
}

View file

@ -29,6 +29,7 @@ void PluralRulesPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.select, select, 1, attr);
define_native_function(vm.names.selectRange, select_range, 2, attr);
define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr);
}
@ -47,6 +48,33 @@ JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::select)
return js_string(vm, Unicode::plural_category_to_string(plurality));
}
// 1.4.4 Intl.PluralRules.prototype.selectRange ( start, end ), https://tc39.es/proposal-intl-numberformat-v3/out/pluralrules/proposed.html#sec-intl.pluralrules.prototype.selectrange
JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::select_range)
{
auto start = vm.argument(0);
auto end = vm.argument(1);
// 1. Let pr be the this value.
// 2. Perform ? RequireInternalSlot(pr, [[InitializedPluralRules]]).
auto* plural_rules = TRY(typed_this_object(global_object));
// 3. If start is undefined or end is undefined, throw a TypeError exception.
if (start.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "start"sv);
if (end.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "end"sv);
// 4. Let x be ? ToNumber(start).
auto x = TRY(start.to_number(global_object));
// 5. Let y be ? ToNumber(end).
auto y = TRY(end.to_number(global_object));
// 6. Return ? ResolvePluralRange(pr, x, y).
auto plurality = TRY(resolve_plural_range(global_object, *plural_rules, x, y));
return js_string(vm, Unicode::plural_category_to_string(plurality));
}
// 16.3.4 Intl.PluralRules.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.resolvedoptions
JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::resolved_options)
{

View file

@ -21,6 +21,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(select);
JS_DECLARE_NATIVE_FUNCTION(select_range);
JS_DECLARE_NATIVE_FUNCTION(resolved_options);
};