Skip to content

Commit

Permalink
LibJS: Implement Temporal.PlainYearMonth.prototype.toLocaleString()
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Aug 20, 2021
1 parent 421ad73 commit 70fb7bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string)
}

// 3.3.29 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
{
// 1. Let temporalDate be the this value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)

u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_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 @@ -208,6 +209,24 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
return js_string(vm, *string);
}

// 9.3.18 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 = typed_this(global_object);
if (vm.exception())
return {};

// 3. Return ? TemporalYearMonthToString(yearMonth, "auto").
auto string = temporal_year_month_to_string(global_object, *year_month, "auto"sv);
if (vm.exception())
return {};

return js_string(vm, *string);
}

// 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PlainYearMonthPrototype final : public Object {
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(value_of);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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, { toString: () => "foo" }, 6);
expect(plainYearMonth.toLocaleString()).toBe("2021-07-06[u-ca=foo]");
});
});

describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Temporal.PlainYearMonth.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
});
});

0 comments on commit 70fb7bf

Please sign in to comment.