summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-19 08:39:34 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-20 18:12:15 +0100
commitea44f33d5b1ef9f8972b469184cd5e879da661cc (patch)
tree1d0f5501197a08918e3f4b42f7848580a37d2957 /Userland
parentc1c8d7861c42b63de7c757bd19bbb9bed81d75f1 (diff)
downloadserenity-ea44f33d5b1ef9f8972b469184cd5e879da661cc.zip
LibJS: Implement Temporal.PlainMonthDay.prototype.toString()
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp34
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp29
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toString.js36
5 files changed, 101 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
index 76be370a88..42d764c224 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
@@ -6,6 +6,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
#include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
@@ -59,4 +60,37 @@ PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_mon
return object;
}
+// 10.5.3 TemporalMonthDayToString ( monthDay, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring
+Optional<String> temporal_month_day_to_string(GlobalObject& global_object, PlainMonthDay& month_day, StringView show_calendar)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Assert: Type(monthDay) is Object.
+ // 2. Assert: monthDay has an [[InitializedTemporalMonthDay]] internal slot.
+
+ // 3. Let month be monthDay.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
+ // 4. Let day be monthDay.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
+ // 5. Let result be the string-concatenation of month, the code unit 0x002D (HYPHEN-MINUS), and day.
+ auto result = String::formatted("{:02}-{:02}", month_day.iso_month(), month_day.iso_day());
+
+ // 6. Let calendarID be ? ToString(monthDay.[[Calendar]]).
+ auto calendar_id = Value(&month_day.calendar()).to_string(global_object);
+ if (vm.exception())
+ return {};
+
+ // 7. If calendarID is not "iso8601", then
+ if (calendar_id != "iso8601"sv) {
+ // a. Let year be ! PadISOYear(monthDay.[[ISOYear]]).
+ // b. Set result to the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and result.
+ result = String::formatted("{}-{}", pad_iso_year(month_day.iso_year()), result);
+ }
+
+ // 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
+ auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
+
+ // 9. Set result to the string-concatenation of result and calendarString.
+ // 10. Return result.
+ return String::formatted("{}{}", result, calendar_string);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h
index 0b4d6acec2..3280bf68a1 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h
@@ -40,5 +40,6 @@ struct ISOMonthDay {
};
PlainMonthDay* create_temporal_month_day(GlobalObject&, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject* new_target = nullptr);
+Optional<String> temporal_month_day_to_string(GlobalObject&, PlainMonthDay&, StringView show_calendar);
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp
index b42d017c56..fae46668fe 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp
@@ -6,6 +6,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
#include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h>
@@ -32,6 +33,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
+ define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
@@ -94,6 +96,33 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
return Value(calendar_day(global_object, calendar, *month_day));
}
+// 10.3.8 Temporal.PlainMonthDay.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tostring
+JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
+{
+ // 1. Let monthDay be the this value.
+ // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
+ auto* month_day = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Set options to ? GetOptionsObject(options).
+ auto* options = get_options_object(global_object, vm.argument(0));
+ if (vm.exception())
+ return {};
+
+ // 4. Let showCalendar be ? ToShowCalendarOption(options).
+ auto show_calendar = to_show_calendar_option(global_object, *options);
+ if (vm.exception())
+ return {};
+
+ // 5. Return ? TemporalMonthDayToString(monthDay, showCalendar).
+ auto string = temporal_month_day_to_string(global_object, *month_day, *show_calendar);
+ if (vm.exception())
+ return {};
+
+ return js_string(vm, *string);
+}
+
// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h
index d20c1ea6b2..698bd297b7 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h
@@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
+ JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toString.js
new file mode 100644
index 0000000000..df09430402
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toString.js
@@ -0,0 +1,36 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.PlainMonthDay.prototype.toString).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ let plainMonthDay;
+
+ plainMonthDay = new Temporal.PlainMonthDay(7, 6);
+ expect(plainMonthDay.toString()).toBe("07-06");
+ expect(plainMonthDay.toString({ calendarName: "auto" })).toBe("07-06");
+ expect(plainMonthDay.toString({ calendarName: "always" })).toBe("07-06[u-ca=iso8601]");
+ expect(plainMonthDay.toString({ calendarName: "never" })).toBe("07-06");
+
+ plainMonthDay = new Temporal.PlainMonthDay(7, 6, { toString: () => "foo" }, 2021);
+ expect(plainMonthDay.toString()).toBe("2021-07-06[u-ca=foo]");
+ expect(plainMonthDay.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=foo]");
+ expect(plainMonthDay.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=foo]");
+ expect(plainMonthDay.toString({ calendarName: "never" })).toBe("2021-07-06");
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.PlainMonthDay object", () => {
+ expect(() => {
+ Temporal.PlainMonthDay.prototype.toString.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay");
+ });
+
+ test("calendarName option must be one of 'auto', 'always', 'never'", () => {
+ const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
+ expect(() => {
+ plainMonthDay.toString({ calendarName: "foo" });
+ }).toThrowWithMessage(RangeError, "foo is not a valid value for option calendarName");
+ });
+});