Skip to content

Commit

Permalink
LibJS: Implement Temporal.PlainMonthDay.prototype.toJSON()
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Aug 20, 2021
1 parent 5904c6b commit 5d11637
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void PlainMonthDayPrototype::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.toJSON, to_json, 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 @@ -142,6 +143,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string)
return js_string(vm, *string);
}

// 10.3.10 Temporal.PlainMonthDay.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tojson
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_json)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = typed_this(global_object);
if (vm.exception())
return {};

// 3. Return ? TemporalMonthDayToString(monthDay, "auto").
auto string = temporal_month_day_to_string(global_object, *month_day, "auto"sv);
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 @@ -24,6 +24,7 @@ class PlainMonthDayPrototype final : public Object {
JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
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.PlainMonthDay.prototype.toJSON).toHaveLength(0);
});

test("basic functionality", () => {
let plainMonthDay;

plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(plainMonthDay.toJSON()).toBe("07-06");

plainMonthDay = new Temporal.PlainMonthDay(7, 6, { toString: () => "foo" }, 2021);
expect(plainMonthDay.toJSON()).toBe("2021-07-06[u-ca=foo]");
});
});

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

0 comments on commit 5d11637

Please sign in to comment.