mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:57:35 +00:00
LibJS: Implement a naive String.prototype.localeCompare
This commit is contained in:
parent
e22539bdd9
commit
7a56ca1250
4 changed files with 63 additions and 0 deletions
|
@ -253,6 +253,7 @@ namespace JS {
|
|||
P(length) \
|
||||
P(link) \
|
||||
P(load) \
|
||||
P(localeCompare) \
|
||||
P(log) \
|
||||
P(log1p) \
|
||||
P(log2) \
|
||||
|
|
|
@ -153,6 +153,7 @@ void StringPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.strike, strike, 0, attr);
|
||||
define_native_function(vm.names.sub, sub, 0, attr);
|
||||
define_native_function(vm.names.sup, sup, 0, attr);
|
||||
define_native_function(vm.names.localeCompare, locale_compare, 1, attr);
|
||||
define_native_function(*vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
|
||||
}
|
||||
|
||||
|
@ -1202,4 +1203,25 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::sup)
|
|||
return create_html(global_object, vm.this_value(global_object), "sup", String::empty(), Value());
|
||||
}
|
||||
|
||||
// 22.1.3.10 String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-string.prototype.localecompare
|
||||
// NOTE: This is the minimum localeCompare implementation for engines without ECMA-402.
|
||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::locale_compare)
|
||||
{
|
||||
auto string = ak_string_from(vm, global_object);
|
||||
if (!string.has_value())
|
||||
return {};
|
||||
|
||||
auto that_string = vm.argument(0).to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// FIXME: Actually compare the string not just according to their bits.
|
||||
if (string == that_string)
|
||||
return Value(0);
|
||||
if (string.value() < that_string)
|
||||
return Value(-1);
|
||||
|
||||
return Value(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -69,6 +69,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(strike);
|
||||
JS_DECLARE_NATIVE_FUNCTION(sub);
|
||||
JS_DECLARE_NATIVE_FUNCTION(sup);
|
||||
JS_DECLARE_NATIVE_FUNCTION(locale_compare);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue