Pārlūkot izejas kodu

LibJS: Skip undefined and null in join_array_with_separator()

This it being used in Array.prototype.{join,toString}() - and now
adhering to the spec: [undefined, null].join() === ","
Linus Groh 5 gadi atpakaļ
vecāks
revīzija
6d6cd64689

+ 3 - 2
Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -226,8 +226,9 @@ static Value join_array_with_separator(Interpreter& interpreter, const Array& ar
     for (size_t i = 0; i < array.elements().size(); ++i) {
     for (size_t i = 0; i < array.elements().size(); ++i) {
         if (i != 0)
         if (i != 0)
             builder.append(separator);
             builder.append(separator);
-        if (!array.elements()[i].is_empty())
-            builder.append(array.elements()[i].to_string());
+        auto value = array.elements()[i];
+        if (!value.is_empty() && !value.is_undefined() && !value.is_null())
+            builder.append(value.to_string());
     }
     }
     return js_string(interpreter, builder.to_string());
     return js_string(interpreter, builder.to_string());
 }
 }

+ 5 - 0
Libraries/LibJS/Tests/Array.prototype.join.js

@@ -5,6 +5,11 @@ try {
 
 
     assert(["hello", "friends"].join() === "hello,friends");
     assert(["hello", "friends"].join() === "hello,friends");
     assert(["hello", "friends"].join(" ") === "hello friends");
     assert(["hello", "friends"].join(" ") === "hello friends");
+    assert([].join() === "");
+    assert([null].join() === "");
+    assert([undefined].join() === "");
+    assert([undefined, null, ""].join() === ",,");
+    assert([1, null, 2, undefined, 3].join() === "1,,2,,3");
     assert(Array(3).join() === ",,");
     assert(Array(3).join() === ",,");
 
 
     console.log("PASS");
     console.log("PASS");

+ 2 - 0
Libraries/LibJS/Tests/Array.prototype.toString.js

@@ -8,6 +8,8 @@ try {
 
 
     assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
     assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
 
 
+    assert([undefined, null].toString() === ",");
+
     a = new Array(5);
     a = new Array(5);
     assert(a.toString() === ",,,,");
     assert(a.toString() === ",,,,");
     a[2] = "foo";
     a[2] = "foo";