Skip to content

Commit

Permalink
LibJS: Implement Temporal.PlainMonthDay.prototype.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Aug 20, 2021
1 parent c1c8d78 commit ea44f33
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}

}
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PlainMonthDayPrototype final : public Object {
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);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit ea44f33

Please sign in to comment.