1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

LibJS: Use "," separator in Array.prototype.join() if first arg is undefined

This is how the spec describes it, not "if the first arg is missing".
Also swap length & separator steps to match spec.
This commit is contained in:
Linus Groh 2020-11-04 00:31:23 +00:00 committed by Andreas Kling
parent 3e0e84dcd1
commit e5845ba3a0
2 changed files with 5 additions and 4 deletions

View file

@ -316,15 +316,15 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
auto* this_object = vm.this_value(global_object).to_object(global_object); auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object) if (!this_object)
return {}; return {};
auto length = get_length(vm, *this_object);
if (vm.exception())
return {};
String separator = ","; String separator = ",";
if (vm.argument_count()) { if (!vm.argument(0).is_undefined()) {
separator = vm.argument(0).to_string(global_object); separator = vm.argument(0).to_string(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
} }
auto length = get_length(vm, *this_object);
if (vm.exception())
return {};
StringBuilder builder; StringBuilder builder;
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
if (i > 0) if (i > 0)

View file

@ -4,6 +4,7 @@ test("length is 1", () => {
test("basic functionality", () => { test("basic functionality", () => {
expect(["hello", "friends"].join()).toBe("hello,friends"); expect(["hello", "friends"].join()).toBe("hello,friends");
expect(["hello", "friends"].join(undefined)).toBe("hello,friends");
expect(["hello", "friends"].join(" ")).toBe("hello friends"); expect(["hello", "friends"].join(" ")).toBe("hello friends");
expect(["hello", "friends", "foo"].join("~", "#")).toBe("hello~friends~foo"); expect(["hello", "friends", "foo"].join("~", "#")).toBe("hello~friends~foo");
expect([].join()).toBe(""); expect([].join()).toBe("");