1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:57:45 +00:00

LibJS: Implement Temporal.TimeZone.from()

This commit is contained in:
Linus Groh 2021-07-31 21:39:36 +01:00
parent 28b1e66b51
commit f987c11464
3 changed files with 29 additions and 0 deletions

View file

@ -25,6 +25,9 @@ void TimeZoneConstructor::initialize(GlobalObject& global_object)
// 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal-timezone-prototype // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal-timezone-prototype
define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0); define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
define_direct_property(vm.names.length, Value(1), Attribute::Configurable); define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
} }
@ -79,4 +82,13 @@ Value TimeZoneConstructor::construct(FunctionObject& new_target)
return create_temporal_time_zone(global_object, canonical, &new_target); return create_temporal_time_zone(global_object, canonical, &new_target);
} }
// 11.3.2 Temporal.TimeZone.from ( item )
JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from)
{
auto item = vm.argument(0);
// 1. Return ? ToTemporalTimeZone(item).
return to_temporal_time_zone(global_object, item);
}
} }

View file

@ -23,6 +23,8 @@ public:
private: private:
virtual bool has_constructor() const override { return true; } virtual bool has_constructor() const override { return true; }
JS_DECLARE_NATIVE_FUNCTION(from);
}; };
} }

View file

@ -0,0 +1,15 @@
describe("normal behavior", () => {
test("length is 1", () => {
expect(Temporal.TimeZone.from).toHaveLength(1);
});
test("basic functionality", () => {
const timeZone = new Temporal.TimeZone("UTC");
const timeZoneLike = {};
const zonedDateTimeLike = { timeZone: {} };
expect(Temporal.TimeZone.from(timeZone)).toBe(timeZone);
expect(Temporal.TimeZone.from(timeZoneLike)).toBe(timeZoneLike);
expect(Temporal.TimeZone.from(zonedDateTimeLike)).toBe(zonedDateTimeLike.timeZone);
// TODO: test from("string") once ParseTemporalTimeZoneString is working
});
});