mirror of
https://github.com/RGBCube/serenity
synced 2025-06-28 23:02:07 +00:00
LibJS: Implement Temporal.PlainMonthDay.prototype.toString()
This commit is contained in:
parent
c1c8d7861c
commit
ea44f33d5b
5 changed files with 101 additions and 0 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <LibJS/Runtime/AbstractOperations.h>
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
|
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
|
#include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
|
||||||
|
@ -59,4 +60,37 @@ PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_mon
|
||||||
return object;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
|
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.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);
|
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | 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.valueOf, value_of, 0, attr);
|
||||||
define_native_function(vm.names.getISOFields, get_iso_fields, 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));
|
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
|
// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,6 +22,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
|
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(day_getter);
|
JS_DECLARE_NATIVE_FUNCTION(day_getter);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue