From 9a62c01ebc5e7cda690aaf550b5b418c32729874 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 9 Dec 2021 22:46:08 -0500 Subject: [PATCH] LibJS: Implement ECMA-402 Date.prototype.toLocaleString --- .../Libraries/LibJS/Runtime/DatePrototype.cpp | 38 ++++++++++-- .../Date/Date.prototype.toLocaleString.js | 61 +++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleString.js diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 947d8279ad..d922441696 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -11,10 +11,14 @@ #include #include #include +#include #include #include #include #include +#include +#include +#include #include #include @@ -667,6 +671,16 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_iso_string) return js_string(vm, move(string)); } +static ThrowCompletionOr construct_date_time_format(GlobalObject& global_object, Value locales, Value options) +{ + MarkedValueList arguments { global_object.vm().heap() }; + arguments.append(locales); + arguments.append(options); + + auto* date_time_format = TRY(construct(global_object, *global_object.intl_date_time_format_constructor(), move(arguments))); + return static_cast(date_time_format); +} + // 21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) { @@ -680,17 +694,29 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) return js_string(vm, move(string)); } -// 21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocalestring +// 18.4.1 Date.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocalestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_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_string(); - return js_string(vm, move(string)); + // 3. Let options be ? ToDateTimeOptions(options, "any", "all"). + options = Value(TRY(Intl::to_date_time_options(global_object, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All))); + + // 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)); } // 21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaletimestring diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleString.js new file mode 100644 index 0000000000..dc76c7f6a9 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.toLocaleString.js @@ -0,0 +1,61 @@ +describe("errors", () => { + test("called on non-Date object", () => { + expect(() => { + Date.prototype.toLocaleString(); + }).toThrowWithMessage(TypeError, "Not an object of type Date"); + }); + + test("called with value that cannot be converted to a number", () => { + expect(() => { + new Date(Symbol.hasInstance).toLocaleString(); + }).toThrowWithMessage(TypeError, "Cannot convert symbol to number"); + + expect(() => { + new Date(1n).toLocaleString(); + }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number"); + }); + + test("time value cannot be clipped", () => { + expect(() => { + new Date(-8.65e15).toLocaleString(); + }).toThrowWithMessage(RangeError, "Time value must be between -8.64E15 and 8.64E15"); + }); +}); + +describe("correct behavior", () => { + test("NaN", () => { + const d = new Date(NaN); + expect(d.toLocaleString()).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 and time", () => { + expect(d0.toLocaleString("en", { timeZone: "UTC" })).toBe("12/7/2021, 5:40:50 PM"); + expect(d1.toLocaleString("en", { timeZone: "UTC" })).toBe("1/23/1989, 7:08:09 AM"); + + expect(d0.toLocaleString("ar", { timeZone: "UTC" })).toBe("٧‏/١٢‏/٢٠٢١, ٥:٤٠:٥٠ م"); + expect(d1.toLocaleString("ar", { timeZone: "UTC" })).toBe("٢٣‏/١‏/١٩٨٩, ٧:٠٨:٠٩ ص"); + }); + + test("dateStyle may be set", () => { + expect(d0.toLocaleString("en", { dateStyle: "short", timeZone: "UTC" })).toBe("12/7/21"); + expect(d1.toLocaleString("en", { dateStyle: "short", timeZone: "UTC" })).toBe("1/23/89"); + + expect(d0.toLocaleString("ar", { dateStyle: "short", timeZone: "UTC" })).toBe( + "٧‏/١٢‏/٢٠٢١" + ); + expect(d1.toLocaleString("ar", { dateStyle: "short", timeZone: "UTC" })).toBe( + "٢٣‏/١‏/١٩٨٩" + ); + }); + + test("timeStyle may be set", () => { + expect(d0.toLocaleString("en", { timeStyle: "short", timeZone: "UTC" })).toBe("5:40 PM"); + expect(d1.toLocaleString("en", { timeStyle: "short", timeZone: "UTC" })).toBe("7:08 AM"); + + expect(d0.toLocaleString("ar", { timeStyle: "short", timeZone: "UTC" })).toBe("٥:٤٠ م"); + expect(d1.toLocaleString("ar", { timeStyle: "short", timeZone: "UTC" })).toBe("٧:٠٨ ص"); + }); +});