mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
LibJS: Implement Temporal.PlainDate.prototype.toJSON()
This commit is contained in:
parent
73d888e9e6
commit
0e201fbb42
3 changed files with 42 additions and 0 deletions
|
@ -53,6 +53,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.equals, equals, 1, attr);
|
define_native_function(vm.names.equals, equals, 1, attr);
|
||||||
define_native_function(vm.names.toString, to_string, 0, attr);
|
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.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.valueOf, value_of, 0, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,6 +444,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
|
||||||
return js_string(vm, *string);
|
return js_string(vm, *string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3.3.30 Temporal.PlainDate.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tojson
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_json)
|
||||||
|
{
|
||||||
|
// 1. Let temporalDate be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||||
|
auto* temporal_date = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Return ? TemporalDateToString(temporalDate, "auto").
|
||||||
|
auto string = temporal_date_to_string(global_object, *temporal_date, "auto"sv);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return js_string(vm, *string);
|
||||||
|
}
|
||||||
|
|
||||||
// 3.3.31 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
|
// 3.3.31 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
|
||||||
{
|
{
|
||||||
|
|
|
@ -39,6 +39,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 0", () => {
|
||||||
|
expect(Temporal.PlainDate.prototype.toJSON).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
let plainDate;
|
||||||
|
|
||||||
|
plainDate = new Temporal.PlainDate(2021, 7, 6);
|
||||||
|
expect(plainDate.toJSON()).toBe("2021-07-06");
|
||||||
|
|
||||||
|
plainDate = new Temporal.PlainDate(2021, 7, 6, { toString: () => "foo" });
|
||||||
|
expect(plainDate.toJSON()).toBe("2021-07-06[u-ca=foo]");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("this value must be a Temporal.PlainDate object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Temporal.PlainDate.prototype.toJSON.call("foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue