diff options
Diffstat (limited to 'Userland')
4 files changed, 61 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index eae0dd3cb5..3e72e5ff55 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -135,6 +135,7 @@ namespace JS { P(epochNanoseconds) \ P(epochSeconds) \ P(equals) \ + P(era) \ P(error) \ P(errors) \ P(escape) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index fc1fcbb4e0..9672c2b749 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -54,6 +54,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.mergeFields, merge_fields, 2, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); + define_native_function(vm.names.era, era, 1, attr); } static Calendar* typed_this(GlobalObject& global_object) @@ -559,4 +560,36 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_json) return js_string(vm, calendar.to_string(global_object)); } +// 15.6.2.6 Temporal.Calendar.prototype.era ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.era +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era) +{ + auto temporal_date_like = vm.argument(0); + + // 1. Let calendar be the this value. + // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + auto* calendar = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], or [[InitializedTemporalYearMonth]] internal slot, then + if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(temporal_date_like.as_object()))) { + // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike). + temporal_date_like = to_temporal_date(global_object, temporal_date_like); + if (vm.exception()) + return {}; + } + + // 4. If calendar.[[Identifier]] is "iso8601", then + if (calendar->identifier() == "iso8601"sv) { + // a. Return undefined. + return js_undefined(); + } + + // 5. Let era be the result of implementation-defined processing of temporalDateLike and the value of calendar.[[Identifier]]. + // 6. Return era. + + // NOTE: No support for non-iso8601 calendars yet. + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index 6a0090b66e..94a8ceeaa5 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -39,6 +39,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(merge_fields); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); + JS_DECLARE_NATIVE_FUNCTION(era); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.era.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.era.js new file mode 100644 index 0000000000..a80ab602a2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.era.js @@ -0,0 +1,26 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.era).toHaveLength(1); + }); + + test("basic functionality", () => { + const plainDate = new Temporal.PlainDate(2021, 7, 6); + const calendar = new Temporal.Calendar("iso8601"); + expect(calendar.era(plainDate)).toBeUndefined(); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Calendar object", () => { + expect(() => { + Temporal.Calendar.prototype.era.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.Calendar"); + }); + + test("argument must be date-like", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(() => { + calendar.era({}); + }).toThrowWithMessage(TypeError, "Required property year is missing or undefined"); + }); +}); |