mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-10 18:19:03 +00:00
LibJS: Implement stringification Temporal.PlainYearMonth prototypes
This commit is contained in:
parent
b68d67693e
commit
2da526423f
Notes:
github-actions[bot]
2024-11-22 18:56:48 +00:00
Author: https://github.com/trflynn89
Commit: 2da526423f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2496
8 changed files with 193 additions and 6 deletions
|
@ -149,4 +149,31 @@ ThrowCompletionOr<GC::Ref<PlainYearMonth>> create_temporal_year_month(VM& vm, IS
|
|||
return object;
|
||||
}
|
||||
|
||||
// 9.5.6 TemporalYearMonthToString ( yearMonth, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring
|
||||
String temporal_year_month_to_string(PlainYearMonth const& year_month, ShowCalendar show_calendar)
|
||||
{
|
||||
// 1. Let year be PadISOYear(yearMonth.[[ISODate]].[[Year]]).
|
||||
auto year = pad_iso_year(year_month.iso_date().year);
|
||||
|
||||
// 2. Let month be ToZeroPaddedDecimalString(yearMonth.[[ISODate]].[[Month]], 2).
|
||||
// 3. Let result be the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and month.
|
||||
auto result = MUST(String::formatted("{}-{:02}", year, year_month.iso_date().month));
|
||||
|
||||
// 4. If showCalendar is one of always or critical, or if yearMonth.[[Calendar]] is not "iso8601", then
|
||||
if (show_calendar == ShowCalendar::Always || show_calendar == ShowCalendar::Critical || year_month.calendar() != "iso8601"sv) {
|
||||
// a. Let day be ToZeroPaddedDecimalString(yearMonth.[[ISODate]].[[Day]], 2).
|
||||
// b. Set result to the string-concatenation of result, the code unit 0x002D (HYPHEN-MINUS), and day.
|
||||
result = MUST(String::formatted("{}-{:02}", result, year_month.iso_date().day));
|
||||
}
|
||||
|
||||
// 5. Let calendarString be FormatCalendarAnnotation(yearMonth.[[Calendar]], showCalendar).
|
||||
auto calendar_string = format_calendar_annotation(year_month.calendar(), show_calendar);
|
||||
|
||||
// 6. Set result to the string-concatenation of result and calendarString.
|
||||
result = MUST(String::formatted("{}{}", result, calendar_string));
|
||||
|
||||
// 7. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,5 +35,6 @@ private:
|
|||
ThrowCompletionOr<GC::Ref<PlainYearMonth>> to_temporal_year_month(VM&, Value item, Value options = js_undefined());
|
||||
bool iso_year_month_within_limits(ISODate);
|
||||
ThrowCompletionOr<GC::Ref<PlainYearMonth>> create_temporal_year_month(VM&, ISODate, String calendar, GC::Ptr<FunctionObject> new_target = {});
|
||||
String temporal_year_month_to_string(PlainYearMonth const&, ShowCalendar);
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
|
||||
|
||||
|
@ -37,6 +38,11 @@ void PlainYearMonthPrototype::initialize(Realm& realm)
|
|||
define_native_accessor(realm, vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(realm, vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
|
||||
define_native_accessor(realm, vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(realm, vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
|
||||
}
|
||||
|
||||
// 9.3.3 get Temporal.PlainYearMonth.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendarid
|
||||
|
@ -123,4 +129,44 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_code_getter)
|
|||
return PrimitiveString::create(vm, move(month_code));
|
||||
}
|
||||
|
||||
// 9.3.19 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
|
||||
{
|
||||
// 1. Let yearMonth be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto year_month = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, vm.argument(0)));
|
||||
|
||||
// 4. Let showCalendar be ? GetTemporalShowCalendarNameOption(resolvedOptions).
|
||||
auto show_calendar = TRY(get_temporal_show_calendar_name_option(vm, resolved_options));
|
||||
|
||||
// 5. Return TemporalYearMonthToString(yearMonth, showCalendar).
|
||||
return PrimitiveString::create(vm, temporal_year_month_to_string(year_month, show_calendar));
|
||||
}
|
||||
|
||||
// 9.3.20 Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tolocalestring
|
||||
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string)
|
||||
{
|
||||
// 1. Let yearMonth be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto year_month = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return TemporalYearMonthToString(yearMonth, AUTO).
|
||||
return PrimitiveString::create(vm, temporal_year_month_to_string(year_month, ShowCalendar::Auto));
|
||||
}
|
||||
|
||||
// 9.3.21 Temporal.PlainYearMonth.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_json)
|
||||
{
|
||||
// 1. Let yearMonth be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto year_month = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return TemporalYearMonthToString(yearMonth, AUTO).
|
||||
return PrimitiveString::create(vm, temporal_year_month_to_string(year_month, ShowCalendar::Auto));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,9 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -51,10 +51,10 @@ describe("normal behavior", () => {
|
|||
expect(Object.getPrototypeOf(plainYearMonth)).toBe(Temporal.PlainYearMonth.prototype);
|
||||
});
|
||||
|
||||
// FIXME: Re-implement this test with Temporal.PlainYearMonth.prototype.toString({ calendarName: "always" }).
|
||||
// test("default reference day is 1", () => {
|
||||
// const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
// const fields = plainYearMonth.getISOFields();
|
||||
// expect(fields.isoDay).toBe(1);
|
||||
// });
|
||||
test("default reference day is 1", () => {
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
const fields = plainYearMonth.toString({ calendarName: "always" });
|
||||
const day = fields.split("-")[2].split("[")[0];
|
||||
expect(day).toBe("01");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.PlainYearMonth.prototype.toJSON).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
let plainYearMonth;
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(plainYearMonth.toJSON()).toBe("2021-07");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7, "gregory", 6);
|
||||
expect(plainYearMonth.toJSON()).toBe("2021-07-06[u-ca=gregory]");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainYearMonth object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainYearMonth.prototype.toJSON.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,23 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.PlainYearMonth.prototype.toLocaleString).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
let plainYearMonth;
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(plainYearMonth.toLocaleString()).toBe("2021-07");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7, "gregory", 6);
|
||||
expect(plainYearMonth.toLocaleString()).toBe("2021-07-06[u-ca=gregory]");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainYearMonth object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainYearMonth.prototype.toLocaleString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,64 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.PlainYearMonth.prototype.toString).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
let plainYearMonth;
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(plainYearMonth.toString()).toBe("2021-07");
|
||||
expect(plainYearMonth.toString({ calendarName: "auto" })).toBe("2021-07");
|
||||
expect(plainYearMonth.toString({ calendarName: "always" })).toBe(
|
||||
"2021-07-01[u-ca=iso8601]"
|
||||
);
|
||||
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07");
|
||||
expect(plainYearMonth.toString({ calendarName: "critical" })).toBe(
|
||||
"2021-07-01[!u-ca=iso8601]"
|
||||
);
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(2021, 7, "gregory", 6);
|
||||
expect(plainYearMonth.toString()).toBe("2021-07-06[u-ca=gregory]");
|
||||
expect(plainYearMonth.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=gregory]");
|
||||
expect(plainYearMonth.toString({ calendarName: "always" })).toBe(
|
||||
"2021-07-06[u-ca=gregory]"
|
||||
);
|
||||
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07-06");
|
||||
expect(plainYearMonth.toString({ calendarName: "critical" })).toBe(
|
||||
"2021-07-06[!u-ca=gregory]"
|
||||
);
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(0, 1);
|
||||
expect(plainYearMonth.toString()).toBe("0000-01");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(999, 1);
|
||||
expect(plainYearMonth.toString()).toBe("0999-01");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(9999, 1);
|
||||
expect(plainYearMonth.toString()).toBe("9999-01");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(12345, 1);
|
||||
expect(plainYearMonth.toString()).toBe("+012345-01");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(123456, 1);
|
||||
expect(plainYearMonth.toString()).toBe("+123456-01");
|
||||
|
||||
plainYearMonth = new Temporal.PlainYearMonth(-12345, 1);
|
||||
expect(plainYearMonth.toString()).toBe("-012345-01");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainYearMonth object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainYearMonth.prototype.toString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
|
||||
});
|
||||
|
||||
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(() => {
|
||||
plainYearMonth.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