diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-15 10:06:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-15 10:06:01 +0200 |
commit | ad2aac5fde35c4d55e487f7911e74d3dfa0b2a4d (patch) | |
tree | 9d72afb4b886d0e315427a5b0703cbfffba78929 /Libraries | |
parent | fa303551946ed1729a22ebeee3202f31c81bbb1d (diff) | |
download | serenity-ad2aac5fde35c4d55e487f7911e74d3dfa0b2a4d.zip |
LibJS: Add Array.prototype.join()
And share the code with Array.prototype.toString() :^)
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 35 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.h | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.join.js | 13 |
3 files changed, 41 insertions, 8 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index f1c726269e..7eafef6173 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -47,6 +47,7 @@ ArrayPrototype::ArrayPrototype() put_native_function("shift", shift, 0); put_native_function("toString", to_string, 0); put_native_function("unshift", unshift, 1); + put_native_function("join", join, 1); put("length", Value(0)); } @@ -194,20 +195,38 @@ Value ArrayPrototype::shift(Interpreter& interpreter) return array->elements().take_first(); } +static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator) +{ + StringBuilder builder; + 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()); + } + return js_string(interpreter, builder.to_string()); +} + Value ArrayPrototype::to_string(Interpreter& interpreter) { auto* array = array_from(interpreter); if (!array) return {}; - StringBuilder builder; - for (size_t i = 0; i < array->elements().size(); ++i) { - if (i != 0) - builder.append(','); - if (!array->elements()[i].is_empty()) - builder.append(array->elements()[i].to_string()); - } - return js_string(interpreter, builder.to_string()); + return join_array_with_separator(interpreter, *array, ","); +} + +Value ArrayPrototype::join(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + + String separator = ","; + if (interpreter.argument_count() == 1) + separator = interpreter.argument(0).to_string(); + + return join_array_with_separator(interpreter, *array, separator); } } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index efe81b8821..e7d120428d 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -47,6 +47,7 @@ private: static Value shift(Interpreter&); static Value to_string(Interpreter&); static Value unshift(Interpreter&); + static Value join(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Array.prototype.join.js b/Libraries/LibJS/Tests/Array.prototype.join.js new file mode 100644 index 0000000000..ef214d308e --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.join.js @@ -0,0 +1,13 @@ +load("test-common.js"); + +try { + assert(Array.prototype.push.length === 1); + + assert(["hello", "friends"].join() === "hello,friends"); + assert(["hello", "friends"].join(" ") === "hello friends"); + assert(Array(3).join() === ",,"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |