Forráskód Böngészése

LibJS: Add String.prototype.toUpperCase()

Linus Groh 5 éve
szülő
commit
22f20cd51d

+ 9 - 0
Libraries/LibJS/Runtime/StringPrototype.cpp

@@ -45,6 +45,7 @@ StringPrototype::StringPrototype()
     put_native_function("startsWith", starts_with, 1);
     put_native_function("indexOf", index_of, 1);
     put_native_function("toLowerCase", to_lowercase, 0);
+    put_native_function("toUpperCase", to_uppercase, 0);
 }
 
 StringPrototype::~StringPrototype()
@@ -154,6 +155,14 @@ Value StringPrototype::to_lowercase(Interpreter& interpreter)
     return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
 }
 
+Value StringPrototype::to_uppercase(Interpreter& interpreter)
+{
+    auto* string_object = string_object_from(interpreter);
+    if (!string_object)
+        return {};
+    return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
+}
+
 Value StringPrototype::length_getter(Interpreter& interpreter)
 {
     auto* this_object = interpreter.this_value().to_object(interpreter.heap());

+ 1 - 0
Libraries/LibJS/Runtime/StringPrototype.h

@@ -43,6 +43,7 @@ private:
     static Value starts_with(Interpreter&);
     static Value index_of(Interpreter&);
     static Value to_lowercase(Interpreter&);
+    static Value to_uppercase(Interpreter&);
 
     static Value length_getter(Interpreter&);
 };

+ 5 - 0
Libraries/LibJS/Tests/String.prototype.toLowerCase.js

@@ -1,4 +1,9 @@
 try {
+    // FIXME: Remove once we have the global String object
+    var String = { prototype: Object.getPrototypeOf("") };
+
+    assert(String.prototype.toLowerCase.length === 0);
+
     assert("foo".toLowerCase() === "foo");
     assert("Foo".toLowerCase() === "foo");
     assert("FOO".toLowerCase() === "foo");

+ 16 - 0
Libraries/LibJS/Tests/String.prototype.toUpperCase.js

@@ -0,0 +1,16 @@
+try {
+    // FIXME: Remove once we have the global String object
+    var String = { prototype: Object.getPrototypeOf("") };
+
+    assert(String.prototype.toUpperCase.length === 0);
+
+    assert("foo".toUpperCase() === "FOO");
+    assert("Foo".toUpperCase() === "FOO");
+    assert("FOO".toUpperCase() === "FOO");
+
+    assert(('b' + 'a' + + 'n' + 'a').toUpperCase() === "BANANA");
+
+    console.log("PASS");
+} catch (e) {
+    console.log("FAIL: " + e);
+}