diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-12-09 13:35:59 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-09 23:43:04 +0000 |
commit | 53df13fed7d645d01ea3412809908fbd4dfe5a8f (patch) | |
tree | 2c6da66c7daa35cd7b4b4c21ed339276bded2cd3 /Userland | |
parent | 04f8fb07e1add4b2b9856b73e8be770f6078f817 (diff) | |
download | serenity-53df13fed7d645d01ea3412809908fbd4dfe5a8f.zip |
LibJS: Implement Intl.DateTimeFormat.prototype.formatRangeToParts
Diffstat (limited to 'Userland')
6 files changed, 569 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 7237edcb80..0de67f96ed 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -180,6 +180,7 @@ namespace JS { P(format) \ P(formatMatcher) \ P(formatRange) \ + P(formatRangeToParts) \ P(formatToParts) \ P(fractionalSecondDigits) \ P(freeze) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp index 61dd68e4eb..8abf7e1135 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp @@ -1404,6 +1404,45 @@ ThrowCompletionOr<String> format_date_time_range(GlobalObject& global_object, Da return result.build(); } +// 11.1.13 FormatDateTimeRangeToParts ( dateTimeFormat, x, y ), https://tc39.es/ecma402/#sec-formatdatetimerangetoparts +ThrowCompletionOr<Array*> format_date_time_range_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end) +{ + auto& vm = global_object.vm(); + + // 1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y). + auto parts = TRY(partition_date_time_range_pattern(global_object, date_time_format, start, end)); + + // 2. Let result be ArrayCreate(0). + auto* result = MUST(Array::create(global_object, 0)); + + // 3. Let n be 0. + size_t n = 0; + + // 4. For each Record { [[Type]], [[Value]], [[Source]] } part in parts, do + for (auto& part : parts) { + // a. Let O be OrdinaryObjectCreate(%ObjectPrototype%). + auto* object = Object::create(global_object, global_object.object_prototype()); + + // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]). + MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type))); + + // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]). + MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value)))); + + // d. Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]). + MUST(object->create_data_property_or_throw(vm.names.source, js_string(vm, part.source))); + + // e. Perform ! CreateDataProperty(result, ! ToString(n), O). + MUST(result->create_data_property_or_throw(n, object)); + + // f. Increment n by 1. + ++n; + } + + // 5. Return result. + return result; +} + // 11.1.14 ToLocalTime ( t, calendar, timeZone ), https://tc39.es/ecma402/#sec-tolocaltime ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, [[maybe_unused]] StringView time_zone) { diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h index bc99585490..19baa1b248 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h @@ -205,6 +205,7 @@ ThrowCompletionOr<String> format_date_time(GlobalObject& global_object, DateTime ThrowCompletionOr<Array*> format_date_time_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value time); ThrowCompletionOr<Vector<PatternPartitionWithSource>> partition_date_time_range_pattern(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end); ThrowCompletionOr<String> format_date_time_range(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end); +ThrowCompletionOr<Array*> format_date_time_range_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end); ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, StringView time_zone); template<typename Callback> diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.cpp index 8361926f67..9f8a11fcdd 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.cpp @@ -33,6 +33,7 @@ void DateTimeFormatPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.formatToParts, format_to_parts, 1, attr); define_native_function(vm.names.formatRange, format_range, 2, attr); + define_native_function(vm.names.formatRangeToParts, format_range_to_parts, 2, attr); define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr); } @@ -110,6 +111,32 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range) return js_string(vm, move(formatted)); } +// 11.4.6 Intl.DateTimeFormat.prototype.formatRangeToParts ( startDate, endDate ), https://tc39.es/ecma402/#sec-Intl.DateTimeFormat.prototype.formatRangeToParts +JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range_to_parts) +{ + auto start_date = vm.argument(0); + auto end_date = vm.argument(1); + + // 1. Let dtf be this value. + // 2. Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]). + auto* date_time_format = TRY(typed_this_object(global_object)); + + // 3. If startDate is undefined or endDate is undefined, throw a TypeError exception. + if (start_date.is_undefined()) + return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "startDate"sv); + if (end_date.is_undefined()) + return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "endDate"sv); + + // 4. Let x be ? ToNumber(startDate). + start_date = TRY(start_date.to_number(global_object)); + + // 5. Let y be ? ToNumber(endDate). + end_date = TRY(end_date.to_number(global_object)); + + // 6. Return ? FormatDateTimeRangeToParts(dtf, x, y). + return TRY(format_date_time_range_to_parts(global_object, *date_time_format, start_date, end_date)); +} + // 11.4.7 Intl.DateTimeFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype.resolvedoptions JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::resolved_options) { diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.h index 35b71f3aec..b7b6cec0bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(format); JS_DECLARE_NATIVE_FUNCTION(format_to_parts); JS_DECLARE_NATIVE_FUNCTION(format_range); + JS_DECLARE_NATIVE_FUNCTION(format_range_to_parts); JS_DECLARE_NATIVE_FUNCTION(resolved_options); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.formatRangeToParts.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.formatRangeToParts.js new file mode 100644 index 0000000000..86406a826c --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.formatRangeToParts.js @@ -0,0 +1,500 @@ +describe("errors", () => { + test("called on non-DateTimeFormat object", () => { + expect(() => { + Intl.DateTimeFormat.prototype.formatRangeToParts(1, 2); + }).toThrowWithMessage(TypeError, "Not an object of type Intl.DateTimeFormat"); + }); + + test("called with undefined values", () => { + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(); + }).toThrowWithMessage(TypeError, "startDate is undefined"); + + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(1); + }).toThrowWithMessage(TypeError, "endDate is undefined"); + }); + + test("called with values that cannot be converted to numbers", () => { + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(1, Symbol.hasInstance); + }).toThrowWithMessage(TypeError, "Cannot convert symbol to number"); + + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(1n, 1); + }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number"); + }); + + test("time value cannot be clipped", () => { + [NaN, -8.65e15, 8.65e15].forEach(d => { + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(d, 1); + }).toThrowWithMessage(RangeError, "Time value must be between -8.64E15 and 8.64E15"); + + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(1, d); + }).toThrowWithMessage(RangeError, "Time value must be between -8.64E15 and 8.64E15"); + }); + }); + + test("called with values in bad order", () => { + expect(() => { + Intl.DateTimeFormat().formatRangeToParts(new Date(2021), new Date(1989)); + }).toThrowWithMessage(RangeError, "Start time 2021 is after end time 1989"); + }); +}); + +const d0 = Date.UTC(1989, 0, 23, 7, 8, 9, 45); +const d1 = Date.UTC(2021, 11, 7, 17, 40, 50, 456); + +describe("equal dates are squashed", () => { + test("with date fields", () => { + const en = new Intl.DateTimeFormat("en", { + year: "numeric", + month: "long", + day: "2-digit", + }); + expect(en.formatRangeToParts(d0, d0)).toEqual([ + { type: "month", value: "January", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + { type: "literal", value: ", ", source: "shared" }, + { type: "year", value: "1989", source: "shared" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { + year: "numeric", + month: "long", + day: "2-digit", + }); + expect(ja.formatRangeToParts(d0, d0)).toEqual([ + { type: "year", value: "1989", source: "shared" }, + { type: "literal", value: "/", source: "shared" }, + { type: "month", value: "1ๆ", source: "shared" }, + { type: "literal", value: "/", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + ]); + }); + + test("with time fields", () => { + const en = new Intl.DateTimeFormat("en", { + hour: "numeric", + minute: "2-digit", + second: "2-digit", + timeZone: "UTC", + }); + expect(en.formatRangeToParts(d0, d0)).toEqual([ + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "dayPeriod", value: "AM", source: "shared" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { + hour: "numeric", + minute: "2-digit", + second: "2-digit", + timeZone: "UTC", + }); + expect(ja.formatRangeToParts(d0, d0)).toEqual([ + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + ]); + }); + + test("with mixed fields", () => { + const en = new Intl.DateTimeFormat("en", { + year: "numeric", + month: "long", + day: "2-digit", + hour: "numeric", + minute: "2-digit", + second: "2-digit", + timeZone: "UTC", + }); + expect(en.formatRangeToParts(d0, d0)).toEqual([ + { type: "month", value: "January", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + { type: "literal", value: ", ", source: "shared" }, + { type: "year", value: "1989", source: "shared" }, + { type: "literal", value: " at ", source: "shared" }, + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "dayPeriod", value: "AM", source: "shared" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { + year: "numeric", + month: "long", + day: "2-digit", + hour: "numeric", + minute: "2-digit", + second: "2-digit", + timeZone: "UTC", + }); + expect(ja.formatRangeToParts(d0, d0)).toEqual([ + { type: "year", value: "1989", source: "shared" }, + { type: "literal", value: "/", source: "shared" }, + { type: "month", value: "1ๆ", source: "shared" }, + { type: "literal", value: "/", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + ]); + }); + + test("with date/time style fields", () => { + const en = new Intl.DateTimeFormat("en", { + dateStyle: "full", + timeStyle: "medium", + timeZone: "UTC", + }); + expect(en.formatRangeToParts(d0, d0)).toEqual([ + { type: "weekday", value: "Monday", source: "shared" }, + { type: "literal", value: ", ", source: "shared" }, + { type: "month", value: "January", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + { type: "literal", value: ", ", source: "shared" }, + { type: "year", value: "1989", source: "shared" }, + { type: "literal", value: " at ", source: "shared" }, + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "dayPeriod", value: "AM", source: "shared" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { + dateStyle: "full", + timeStyle: "medium", + timeZone: "UTC", + }); + expect(ja.formatRangeToParts(d0, d0)).toEqual([ + { type: "year", value: "1989", source: "shared" }, + { type: "literal", value: "ๅนด", source: "shared" }, + { type: "month", value: "1", source: "shared" }, + { type: "literal", value: "ๆ", source: "shared" }, + { type: "day", value: "23", source: "shared" }, + { type: "literal", value: "ๆฅ", source: "shared" }, + { type: "weekday", value: "ๆๆๆฅ", source: "shared" }, + { type: "literal", value: " ", source: "shared" }, + { type: "hour", value: "7", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "minute", value: "08", source: "shared" }, + { type: "literal", value: ":", source: "shared" }, + { type: "second", value: "09", source: "shared" }, + ]); + }); +}); + +describe("dateStyle", () => { + test("full", () => { + const en = new Intl.DateTimeFormat("en", { dateStyle: "full" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "weekday", value: "Monday", source: "startRange" }, + { type: "literal", value: ", ", source: "startRange" }, + { type: "month", value: "January", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: ", ", source: "startRange" }, + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "weekday", value: "Tuesday", source: "endRange" }, + { type: "literal", value: ", ", source: "endRange" }, + { type: "month", value: "December", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "day", value: "7", source: "endRange" }, + { type: "literal", value: ", ", source: "endRange" }, + { type: "year", value: "2021", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { dateStyle: "full" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: "ๅนด", source: "startRange" }, + { type: "month", value: "1", source: "startRange" }, + { type: "literal", value: "ๆ", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: "ๆฅ", source: "startRange" }, + { type: "weekday", value: "ๆๆๆฅ", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "year", value: "2021", source: "endRange" }, + { type: "literal", value: "ๅนด", source: "endRange" }, + { type: "month", value: "12", source: "endRange" }, + { type: "literal", value: "ๆ", source: "endRange" }, + { type: "day", value: "7", source: "endRange" }, + { type: "literal", value: "ๆฅ", source: "endRange" }, + { type: "weekday", value: "็ซๆๆฅ", source: "endRange" }, + ]); + }); + + test("long", () => { + const en = new Intl.DateTimeFormat("en", { dateStyle: "long" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "month", value: "January", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: ", ", source: "startRange" }, + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "month", value: "December", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "day", value: "7", source: "endRange" }, + { type: "literal", value: ", ", source: "endRange" }, + { type: "year", value: "2021", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { dateStyle: "long" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "month", value: "01", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "year", value: "2021", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "month", value: "12", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "day", value: "07", source: "endRange" }, + ]); + }); + + test("medium", () => { + const en = new Intl.DateTimeFormat("en", { dateStyle: "medium" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "month", value: "Jan", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: ", ", source: "startRange" }, + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "month", value: "Dec", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "day", value: "7", source: "endRange" }, + { type: "literal", value: ", ", source: "endRange" }, + { type: "year", value: "2021", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { dateStyle: "medium" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "month", value: "01", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "year", value: "2021", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "month", value: "12", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "day", value: "07", source: "endRange" }, + ]); + }); + + test("short", () => { + const en = new Intl.DateTimeFormat("en", { dateStyle: "short" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "month", value: "1", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "year", value: "89", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "month", value: "12", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "day", value: "7", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "year", value: "21", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { dateStyle: "short" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "year", value: "1989", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "month", value: "01", source: "startRange" }, + { type: "literal", value: "/", source: "startRange" }, + { type: "day", value: "23", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "year", value: "2021", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "month", value: "12", source: "endRange" }, + { type: "literal", value: "/", source: "endRange" }, + { type: "day", value: "07", source: "endRange" }, + ]); + }); +}); + +describe("timeStyle", () => { + // FIXME: These results should include the date, even though it isn't requested, because the start/end dates + // are more than just hours apart. See the FIXME in PartitionDateTimeRangePattern. + test("full", () => { + const en = new Intl.DateTimeFormat("en", { timeStyle: "full" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "dayPeriod", value: "AM", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "timeZoneName", value: "Coordinated Universal Time", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "hour", value: "5", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "dayPeriod", value: "PM", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "timeZoneName", value: "Coordinated Universal Time", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { timeStyle: "full" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: "ๆ", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: "ๅ", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: "็ง ", source: "startRange" }, + { type: "timeZoneName", value: "ๅๅฎไธ็ๆ", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "hour", value: "17", source: "endRange" }, + { type: "literal", value: "ๆ", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: "ๅ", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + { type: "literal", value: "็ง ", source: "endRange" }, + { type: "timeZoneName", value: "ๅๅฎไธ็ๆ", source: "endRange" }, + ]); + }); + + test("long", () => { + const en = new Intl.DateTimeFormat("en", { timeStyle: "long" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "dayPeriod", value: "AM", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "timeZoneName", value: "UTC", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "hour", value: "5", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "dayPeriod", value: "PM", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "timeZoneName", value: "UTC", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { timeStyle: "long" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "timeZoneName", value: "UTC", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "hour", value: "17", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "timeZoneName", value: "UTC", source: "endRange" }, + ]); + }); + + test("medium", () => { + const en = new Intl.DateTimeFormat("en", { timeStyle: "medium" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "dayPeriod", value: "AM", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "hour", value: "5", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "dayPeriod", value: "PM", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { timeStyle: "medium" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "second", value: "09", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "hour", value: "17", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "second", value: "50", source: "endRange" }, + ]); + }); + + test("short", () => { + const en = new Intl.DateTimeFormat("en", { timeStyle: "short" }); + expect(en.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: " ", source: "startRange" }, + { type: "dayPeriod", value: "AM", source: "startRange" }, + { type: "literal", value: " โ ", source: "shared" }, + { type: "hour", value: "5", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + { type: "literal", value: " ", source: "endRange" }, + { type: "dayPeriod", value: "PM", source: "endRange" }, + ]); + + const ja = new Intl.DateTimeFormat("ja", { timeStyle: "short" }); + expect(ja.formatRangeToParts(d0, d1)).toEqual([ + { type: "hour", value: "7", source: "startRange" }, + { type: "literal", value: ":", source: "startRange" }, + { type: "minute", value: "08", source: "startRange" }, + { type: "literal", value: "๏ฝ", source: "shared" }, + { type: "hour", value: "17", source: "endRange" }, + { type: "literal", value: ":", source: "endRange" }, + { type: "minute", value: "40", source: "endRange" }, + ]); + }); +}); |