summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-28 23:49:20 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-29 01:30:59 +0200
commit6d6cd64689097260e376e5f7df0234dbcdb9fc0b (patch)
treea6ac006d6c8fdf7b90cc19cc713aa3fe192490aa /Libraries
parent86810a4b025a0f9ada51ad959963bee00082a3c9 (diff)
downloadserenity-6d6cd64689097260e376e5f7df0234dbcdb9fc0b.zip
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() === ","
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp5
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.join.js5
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.toString.js2
3 files changed, 10 insertions, 2 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index 5d30b07c20..eb40a51f47 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/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) {
if (i != 0)
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());
}
diff --git a/Libraries/LibJS/Tests/Array.prototype.join.js b/Libraries/LibJS/Tests/Array.prototype.join.js
index 24b7168550..0221aacfd3 100644
--- a/Libraries/LibJS/Tests/Array.prototype.join.js
+++ b/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([].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() === ",,");
console.log("PASS");
diff --git a/Libraries/LibJS/Tests/Array.prototype.toString.js b/Libraries/LibJS/Tests/Array.prototype.toString.js
index 3bd288f99a..c103d51281 100644
--- a/Libraries/LibJS/Tests/Array.prototype.toString.js
+++ b/Libraries/LibJS/Tests/Array.prototype.toString.js
@@ -8,6 +8,8 @@ try {
assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
+ assert([undefined, null].toString() === ",");
+
a = new Array(5);
assert(a.toString() === ",,,,");
a[2] = "foo";