mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:27:45 +00:00
LibJS: Implement the Intl.Collator constructor
This commit is contained in:
parent
1607a05d4c
commit
06a6100b12
3 changed files with 223 additions and 1 deletions
|
@ -258,6 +258,7 @@ namespace JS {
|
|||
P(hypot) \
|
||||
P(id) \
|
||||
P(ignoreCase) \
|
||||
P(ignorePunctuation) \
|
||||
P(imul) \
|
||||
P(importValue) \
|
||||
P(includes) \
|
||||
|
@ -387,6 +388,7 @@ namespace JS {
|
|||
P(seal) \
|
||||
P(second) \
|
||||
P(seconds) \
|
||||
P(sensitivity) \
|
||||
P(set) \
|
||||
P(setBigInt64) \
|
||||
P(setBigUint64) \
|
||||
|
@ -498,6 +500,7 @@ namespace JS {
|
|||
P(until) \
|
||||
P(unregister) \
|
||||
P(unshift) \
|
||||
P(usage) \
|
||||
P(useGrouping) \
|
||||
P(value) \
|
||||
P(valueOf) \
|
||||
|
|
|
@ -6,11 +6,130 @@
|
|||
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Intl/Collator.h>
|
||||
#include <LibJS/Runtime/Intl/CollatorConstructor.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
// 10.1.1 InitializeCollator ( collator, locales, options ), https://tc39.es/ecma402/#sec-initializecollator
|
||||
static ThrowCompletionOr<Collator*> initialize_collator(GlobalObject& global_object, Collator& collator, Value locales_value, Value options_value)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
||||
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
|
||||
|
||||
// 2. Set options to ? CoerceOptionsToObject(options).
|
||||
auto* options = TRY(coerce_options_to_object(global_object, options_value));
|
||||
|
||||
// 3. Let usage be ? GetOption(options, "usage", "string", « "sort", "search" », "sort").
|
||||
auto usage = TRY(get_option(global_object, *options, vm.names.usage, Value::Type::String, { "sort"sv, "search"sv }, "sort"sv));
|
||||
|
||||
// 4. Set collator.[[Usage]] to usage.
|
||||
collator.set_usage(usage.as_string().string());
|
||||
|
||||
// 5. If usage is "sort", then
|
||||
// a. Let localeData be %Collator%.[[SortLocaleData]].
|
||||
// 6. Else,
|
||||
// a. Let localeData be %Collator%.[[SearchLocaleData]].
|
||||
|
||||
// 7. Let opt be a new Record.
|
||||
LocaleOptions opt {};
|
||||
|
||||
// 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
|
||||
auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
|
||||
|
||||
// 9. Set opt.[[localeMatcher]] to matcher.
|
||||
opt.locale_matcher = matcher;
|
||||
|
||||
// 10. Let collation be ? GetOption(options, "collation", "string", undefined, undefined).
|
||||
auto collation = TRY(get_option(global_object, *options, vm.names.collation, Value::Type::String, {}, Empty {}));
|
||||
|
||||
// 11. If collation is not undefined, then
|
||||
if (!collation.is_undefined()) {
|
||||
// a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
|
||||
if (!Unicode::is_type_identifier(collation.as_string().string()))
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, collation, "collation"sv);
|
||||
|
||||
// 12. Set opt.[[co]] to collation.
|
||||
opt.co = collation.as_string().string();
|
||||
}
|
||||
|
||||
// 13. Let numeric be ? GetOption(options, "numeric", "boolean", undefined, undefined).
|
||||
auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {}));
|
||||
|
||||
// 14. If numeric is not undefined, then
|
||||
// a. Let numeric be ! ToString(numeric).
|
||||
// 15. Set opt.[[kn]] to numeric.
|
||||
if (!numeric.is_undefined())
|
||||
opt.kn = MUST(numeric.to_string(global_object));
|
||||
|
||||
// 16. Let caseFirst be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined).
|
||||
// 17. Set opt.[[kf]] to caseFirst.
|
||||
auto case_first = TRY(get_option(global_object, *options, vm.names.caseFirst, Value::Type::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
|
||||
if (!case_first.is_undefined())
|
||||
opt.kf = case_first.as_string().string();
|
||||
|
||||
// 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
|
||||
auto relevant_extension_keys = Collator::relevant_extension_keys();
|
||||
|
||||
// 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
|
||||
auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
|
||||
|
||||
// 20. Set collator.[[Locale]] to r.[[locale]].
|
||||
collator.set_locale(move(result.locale));
|
||||
|
||||
// 21. Let collation be r.[[co]].
|
||||
// 22. If collation is null, let collation be "default".
|
||||
// 23. Set collator.[[Collation]] to collation.
|
||||
collator.set_collation(result.co.has_value() ? result.co.release_value() : "default");
|
||||
|
||||
// 24. If relevantExtensionKeys contains "kn", then
|
||||
if (relevant_extension_keys.span().contains_slow("kn"sv) && result.kn.has_value()) {
|
||||
// a. Set collator.[[Numeric]] to ! SameValue(r.[[kn]], "true").
|
||||
collator.set_numeric(same_value(js_string(vm, result.kn.release_value()), js_string(vm, "true"sv)));
|
||||
}
|
||||
|
||||
// 25. If relevantExtensionKeys contains "kf", then
|
||||
if (relevant_extension_keys.span().contains_slow("kf"sv) && result.kf.has_value()) {
|
||||
// a. Set collator.[[CaseFirst]] to r.[[kf]].
|
||||
collator.set_case_first(result.kf.release_value());
|
||||
}
|
||||
|
||||
// 26. Let sensitivity be ? GetOption(options, "sensitivity", "string", « "base", "accent", "case", "variant" », undefined).
|
||||
auto sensitivity = TRY(get_option(global_object, *options, vm.names.sensitivity, Value::Type::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
|
||||
|
||||
// 27. If sensitivity is undefined, then
|
||||
if (sensitivity.is_undefined()) {
|
||||
// a. If usage is "sort", then
|
||||
if (collator.usage() == Collator::Usage::Sort) {
|
||||
// i. Let sensitivity be "variant".
|
||||
sensitivity = js_string(vm, "variant"sv);
|
||||
}
|
||||
// b. Else,
|
||||
else {
|
||||
// i. Let dataLocale be r.[[dataLocale]].
|
||||
// ii. Let dataLocaleData be localeData.[[<dataLocale>]].
|
||||
// iii. Let sensitivity be dataLocaleData.[[sensitivity]].
|
||||
sensitivity = js_string(vm, "base"sv);
|
||||
}
|
||||
}
|
||||
|
||||
// 28. Set collator.[[Sensitivity]] to sensitivity.
|
||||
collator.set_sensitivity(sensitivity.as_string().string());
|
||||
|
||||
// 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", "boolean", undefined, false).
|
||||
auto ignore_punctuation = TRY(get_option(global_object, *options, vm.names.ignorePunctuation, Value::Type::Boolean, {}, false));
|
||||
|
||||
// 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
|
||||
collator.set_ignore_punctuation(ignore_punctuation.as_bool());
|
||||
|
||||
// 31. Return collator.
|
||||
return &collator;
|
||||
}
|
||||
|
||||
// 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
|
||||
CollatorConstructor::CollatorConstructor(GlobalObject& global_object)
|
||||
: NativeFunction(vm().names.Collator.as_string(), *global_object.function_prototype())
|
||||
|
@ -38,8 +157,12 @@ ThrowCompletionOr<Value> CollatorConstructor::call()
|
|||
// 10.1.2 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
|
||||
ThrowCompletionOr<Object*> CollatorConstructor::construct(FunctionObject& new_target)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto& global_object = this->global_object();
|
||||
|
||||
auto locales = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
|
||||
// 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
|
||||
// a. Append [[Numeric]] as the last element of internalSlotsList.
|
||||
|
@ -50,7 +173,7 @@ ThrowCompletionOr<Object*> CollatorConstructor::construct(FunctionObject& new_ta
|
|||
auto* collator = TRY(ordinary_create_from_constructor<Collator>(global_object, new_target, &GlobalObject::intl_collator_prototype));
|
||||
|
||||
// 6. Return ? InitializeCollator(collator, locales, options).
|
||||
return collator;
|
||||
return TRY(initialize_collator(global_object, *collator, locales, options));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,101 @@
|
|||
describe("errors", () => {
|
||||
test("structurally invalid tag", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("root");
|
||||
}).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.Collator("en-");
|
||||
}).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.Collator("Latn");
|
||||
}).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.Collator("en-u-aa-U-aa");
|
||||
}).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
|
||||
});
|
||||
|
||||
test("options is an invalid type", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
||||
test("localeMatcher option is invalid ", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { localeMatcher: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
|
||||
});
|
||||
|
||||
test("usage option is invalid ", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { usage: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option usage");
|
||||
});
|
||||
|
||||
test("collation option is invalid ", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { collation: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option collation");
|
||||
});
|
||||
|
||||
test("caseFirst option is invalid ", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { caseFirst: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option caseFirst");
|
||||
});
|
||||
|
||||
test("sensitivity option is invalid ", () => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { sensitivity: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option sensitivity");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Intl.Collator).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("all valid usage options", () => {
|
||||
["sort", "search"].forEach(usage => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { usage: usage });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid localeMatcher options", () => {
|
||||
["lookup", "best fit"].forEach(localeMatcher => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { localeMatcher: localeMatcher });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("valid collation options", () => {
|
||||
["default", "compat"].forEach(collation => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { collation: collation });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("valid caseFirst options", () => {
|
||||
["upper", "lower", "false"].forEach(caseFirst => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { caseFirst: caseFirst });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("valid sensitivity options", () => {
|
||||
["base", "accent", "case", "variant"].forEach(sensitivity => {
|
||||
expect(() => {
|
||||
new Intl.Collator("en", { sensitivity: sensitivity });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue