mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +00:00
LibJS: Implement Temporal.PlainTime.prototype.toLocaleString()
This commit is contained in:
parent
9f78a957d5
commit
6987e326d8
3 changed files with 35 additions and 0 deletions
|
@ -43,6 +43,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 1, attr);
|
define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 1, attr);
|
||||||
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
|
define_native_function(vm.names.getISOFields, get_iso_fields, 0, 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.valueOf, value_of, 0, attr);
|
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,6 +390,21 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
|
||||||
return js_string(vm, move(string));
|
return js_string(vm, move(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4.3.21 Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tolocalestring
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_locale_string)
|
||||||
|
{
|
||||||
|
// 1. Let temporalTime be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
|
||||||
|
auto* temporal_time = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// FIXME: Hey spec, this cannot fail...
|
||||||
|
// 3. Return ? TemporalTimeToString(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], "auto").
|
||||||
|
auto string = temporal_time_to_string(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), "auto"sv);
|
||||||
|
return js_string(vm, move(string));
|
||||||
|
}
|
||||||
|
|
||||||
// 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof
|
// 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of)
|
||||||
{
|
{
|
||||||
|
|
|
@ -31,6 +31,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
|
JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 0", () => {
|
||||||
|
expect(Temporal.PlainTime.prototype.toLocaleString).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const plainTime = new Temporal.PlainTime(18, 14, 47, 123, 456, 789);
|
||||||
|
expect(plainTime.toLocaleString()).toBe("18:14:47.123456789");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("this value must be a Temporal.PlainTime object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Temporal.PlainTime.prototype.toLocaleString.call("foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainTime");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue