diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index d9224416961..dfd40381a7f 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -681,17 +681,29 @@ static ThrowCompletionOr construct_date_time_format(Globa return static_cast(date_time_format); } -// 21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring +// 18.4.2 Date.prototype.toLocaleDateString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocaledatestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) { + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let x be ? thisTimeValue(this value). auto* this_object = TRY(typed_this_object(global_object)); + auto time = this_object->is_invalid() ? js_nan() : this_object->value_of(); - if (this_object->is_invalid()) - return js_string(vm, "Invalid Date"); + // 2. If x is NaN, return "Invalid Date". + if (time.is_nan()) + return js_string(vm, "Invalid Date"sv); - // FIXME: Optional locales, options params. - auto string = this_object->locale_date_string(); - return js_string(vm, move(string)); + // 3. Let options be ? ToDateTimeOptions(options, "date", "date"). + options = Value(TRY(Intl::to_date_time_options(global_object, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date))); + + // 4. Let dateFormat be ? Construct(%DateTimeFormat%, « locales, options »). + auto* date_format = TRY(construct_date_time_format(global_object, locales, options)); + + // 5. Return ? FormatDateTime(dateFormat, x). + auto formatted = TRY(Intl::format_date_time(global_object, *date_format, time)); + return js_string(vm, move(formatted)); } // 18.4.1 Date.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocalestring diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleDateString.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleDateString.js new file mode 100644 index 00000000000..8eb8c324b27 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleDateString.js @@ -0,0 +1,57 @@ +describe("errors", () => { + test("called on non-Date object", () => { + expect(() => { + Date.prototype.toLocaleDateString(); + }).toThrowWithMessage(TypeError, "Not an object of type Date"); + }); + + test("called with value that cannot be converted to a number", () => { + expect(() => { + new Date(Symbol.hasInstance).toLocaleDateString(); + }).toThrowWithMessage(TypeError, "Cannot convert symbol to number"); + + expect(() => { + new Date(1n).toLocaleDateString(); + }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number"); + }); + + test("time value cannot be clipped", () => { + expect(() => { + new Date(-8.65e15).toLocaleDateString(); + }).toThrowWithMessage(RangeError, "Time value must be between -8.64E15 and 8.64E15"); + }); + + test("timeStyle may not be specified", () => { + expect(() => { + new Date().toLocaleDateString([], { timeStyle: "short" }); + }).toThrowWithMessage(TypeError, "Option timeStyle cannot be set when also providing date"); + }); +}); + +describe("correct behavior", () => { + test("NaN", () => { + const d = new Date(NaN); + expect(d.toLocaleDateString()).toBe("Invalid Date"); + }); + + const d0 = new Date(Date.UTC(2021, 11, 7, 17, 40, 50, 456)); + const d1 = new Date(Date.UTC(1989, 0, 23, 7, 8, 9, 45)); + + test("defaults to date", () => { + expect(d0.toLocaleDateString("en")).toBe("12/7/2021"); + expect(d1.toLocaleDateString("en")).toBe("1/23/1989"); + + expect(d0.toLocaleDateString("ar")).toBe("٧‏/١٢‏/٢٠٢١"); + expect(d1.toLocaleDateString("ar")).toBe("٢٣‏/١‏/١٩٨٩"); + }); + + test("dateStyle may be set", () => { + expect(d0.toLocaleDateString("en", { dateStyle: "full" })).toBe( + "Tuesday, December 7, 2021" + ); + expect(d1.toLocaleDateString("en", { dateStyle: "full" })).toBe("Monday, January 23, 1989"); + + expect(d0.toLocaleDateString("ar", { dateStyle: "full" })).toBe("الثلاثاء، ٧ ديسمبر ٢٠٢١"); + expect(d1.toLocaleDateString("ar", { dateStyle: "full" })).toBe("الاثنين، ٢٣ يناير ١٩٨٩"); + }); +});