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

LibJS: Implement non-ECMA-402 String.prototype.toLocale{Lower,Upper}Case

In implementations without ECMA-402, these methods are to behave like
their non-locale equivalents.
This commit is contained in:
Timothy Flynn 2021-07-27 16:35:25 -04:00 committed by Linus Groh
parent 358356758a
commit 2f8eb4f068
5 changed files with 88 additions and 0 deletions

View file

@ -368,8 +368,10 @@ namespace JS {
P(toISOString) \
P(toJSON) \
P(toLocaleDateString) \
P(toLocaleLowerCase) \
P(toLocaleString) \
P(toLocaleTimeString) \
P(toLocaleUpperCase) \
P(toLowerCase) \
P(toPlainDate) \
P(toString) \

View file

@ -117,6 +117,8 @@ void StringPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.startsWith, starts_with, 1, attr);
define_native_function(vm.names.endsWith, ends_with, 1, attr);
define_native_function(vm.names.indexOf, index_of, 1, attr);
define_native_function(vm.names.toLocaleLowerCase, to_locale_lowercase, 0, attr);
define_native_function(vm.names.toLocaleUpperCase, to_locale_uppercase, 0, attr);
define_native_function(vm.names.toLowerCase, to_lowercase, 0, attr);
define_native_function(vm.names.toUpperCase, to_uppercase, 0, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
@ -378,6 +380,30 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
return index.has_value() ? Value(*index) : Value(-1);
}
// 22.1.3.24 String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-string.prototype.tolocalelowercase
// NOTE: This is the minimum toLocaleLowerCase implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_locale_lowercase)
{
auto string = ak_string_from(vm, global_object);
if (!string.has_value())
return {};
auto lowercase = Unicode::to_unicode_lowercase_full(*string);
return js_string(vm, move(lowercase));
}
// 22.1.3.25 String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-string.prototype.tolocaleuppercase
// NOTE: This is the minimum toLocaleUpperCase implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_locale_uppercase)
{
auto string = ak_string_from(vm, global_object);
if (!string.has_value())
return {};
auto uppercase = Unicode::to_unicode_uppercase_full(*string);
return js_string(vm, move(uppercase));
}
// 22.1.3.26 String.prototype.toLowerCase ( ), https://tc39.es/ecma262/#sec-string.prototype.tolowercase
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_lowercase)
{

View file

@ -34,6 +34,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(starts_with);
JS_DECLARE_NATIVE_FUNCTION(ends_with);
JS_DECLARE_NATIVE_FUNCTION(index_of);
JS_DECLARE_NATIVE_FUNCTION(to_locale_lowercase);
JS_DECLARE_NATIVE_FUNCTION(to_locale_uppercase);
JS_DECLARE_NATIVE_FUNCTION(to_lowercase);
JS_DECLARE_NATIVE_FUNCTION(to_uppercase);
JS_DECLARE_NATIVE_FUNCTION(to_string);