mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 18:42:43 +00:00 
			
		
		
		
	LibJS: Implement Temporal.PlainTime.prototype.until()
This commit is contained in:
		
							parent
							
								
									f99af1bef0
								
							
						
					
					
						commit
						2ac1774fd3
					
				
					 3 changed files with 135 additions and 0 deletions
				
			
		|  | @ -46,6 +46,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object) | |||
|     define_native_function(vm.names.add, add, 1, attr); | ||||
|     define_native_function(vm.names.subtract, subtract, 1, attr); | ||||
|     define_native_function(vm.names.with, with, 1, attr); | ||||
|     define_native_function(vm.names.until, until, 1, attr); | ||||
|     define_native_function(vm.names.round, round, 1, attr); | ||||
|     define_native_function(vm.names.equals, equals, 1, attr); | ||||
|     define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 1, attr); | ||||
|  | @ -250,6 +251,50 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::with) | |||
|     return TRY(create_temporal_time(global_object, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond)); | ||||
| } | ||||
| 
 | ||||
| // 4.3.13 Temporal.PlainTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.until
 | ||||
| JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::until) | ||||
| { | ||||
|     // 1. Let temporalTime be the this value.
 | ||||
|     // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
 | ||||
|     auto* temporal_time = TRY(typed_this_object(global_object)); | ||||
| 
 | ||||
|     // 3. Set other to ? ToTemporalTime(other).
 | ||||
|     auto* other = TRY(to_temporal_time(global_object, vm.argument(0))); | ||||
| 
 | ||||
|     // 4. Set options to ? GetOptionsObject(options).
 | ||||
|     auto* options = TRY(get_options_object(global_object, vm.argument(1))); | ||||
| 
 | ||||
|     // 5. Let smallestUnit be ? ToSmallestTemporalUnit(options, « "year", "month", "week", "day" », "nanosecond").
 | ||||
|     auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "nanosecond"sv)); | ||||
| 
 | ||||
|     // 6. Let largestUnit be ? ToLargestTemporalUnit(options, « "year", "month", "week", "day" », "auto", "hour").
 | ||||
|     auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, "hour"sv)); | ||||
| 
 | ||||
|     // 7. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
 | ||||
|     TRY(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit)); | ||||
| 
 | ||||
|     // 8. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
 | ||||
|     auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv)); | ||||
| 
 | ||||
|     // 9. Let maximum be ! MaximumTemporalDurationRoundingIncrement(smallestUnit).
 | ||||
|     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); | ||||
| 
 | ||||
|     // 10. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
 | ||||
|     auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); | ||||
| 
 | ||||
|     // 11. Let result be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]).
 | ||||
|     auto result = difference_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond()); | ||||
| 
 | ||||
|     // 12. Set result to ? RoundDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode).
 | ||||
|     auto rounded_result = TRY(round_duration(global_object, 0, 0, 0, 0, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, rounding_increment, *smallest_unit, rounding_mode)); | ||||
| 
 | ||||
|     // 13. Set result to ! BalanceDuration(0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]], largestUnit).
 | ||||
|     result = MUST(balance_duration(global_object, 0, rounded_result.hours, rounded_result.minutes, rounded_result.seconds, rounded_result.milliseconds, rounded_result.microseconds, *js_bigint(vm, { (i32)rounded_result.nanoseconds }), largest_unit)); | ||||
| 
 | ||||
|     // 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
 | ||||
|     return TRY(create_temporal_duration(global_object, 0, 0, 0, 0, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); | ||||
| } | ||||
| 
 | ||||
| // 4.3.15 Temporal.PlainTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.round
 | ||||
| JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::round) | ||||
| { | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ private: | |||
|     JS_DECLARE_NATIVE_FUNCTION(add); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(subtract); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(with); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(until); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(round); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(equals); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time); | ||||
|  |  | |||
|  | @ -0,0 +1,89 @@ | |||
| describe("correct behavior", () => { | ||||
|     test("length is 1", () => { | ||||
|         expect(Temporal.PlainTime.prototype.until).toHaveLength(1); | ||||
|     }); | ||||
| 
 | ||||
|     test("basic functionality", () => { | ||||
|         const values = [ | ||||
|             [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], "PT0S"], | ||||
|             [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], "PT1H1M1.001001001S"], | ||||
|             [[0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6], "PT1H2M3.004005006S"], | ||||
|             [[1, 2, 3, 4, 5, 6], [0, 0, 0, 0, 0, 0], "-PT1H2M3.004005006S"], | ||||
|             [[0, 0, 0, 0, 0, 0], [23, 59, 59, 999, 999, 999], "PT23H59M59.999999999S"], | ||||
|             [[23, 59, 59, 999, 999, 999], [0, 0, 0, 0, 0, 0], "-PT23H59M59.999999999S"], | ||||
|         ]; | ||||
|         for (const [args, argsOther, expected] of values) { | ||||
|             const plainTime = new Temporal.PlainTime(...args); | ||||
|             const other = new Temporal.PlainTime(...argsOther); | ||||
|             expect(plainTime.until(other).toString()).toBe(expected); | ||||
|         } | ||||
|     }); | ||||
| 
 | ||||
|     test("smallestUnit option", () => { | ||||
|         const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 0); | ||||
|         const other = new Temporal.PlainTime(1, 2, 3, 4, 5, 6); | ||||
|         const values = [ | ||||
|             ["hour", "PT1H"], | ||||
|             ["minute", "PT1H2M"], | ||||
|             ["second", "PT1H2M3S"], | ||||
|             ["millisecond", "PT1H2M3.004S"], | ||||
|             ["microsecond", "PT1H2M3.004005S"], | ||||
|             ["nanosecond", "PT1H2M3.004005006S"], | ||||
|         ]; | ||||
|         for (const [smallestUnit, expected] of values) { | ||||
|             expect(plainTime.until(other, { smallestUnit }).toString()).toBe(expected); | ||||
|         } | ||||
|     }); | ||||
| 
 | ||||
|     test("largestUnit option", () => { | ||||
|         const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 0); | ||||
|         const other = new Temporal.PlainTime(1, 2, 3, 4, 5, 6); | ||||
|         const values = [ | ||||
|             ["hour", "PT1H2M3.004005006S"], | ||||
|             ["minute", "PT62M3.004005006S"], | ||||
|             ["second", "PT3723.004005006S"], | ||||
|             ["millisecond", "PT3723.004005006S"], | ||||
|             ["microsecond", "PT3723.004005006S"], | ||||
|             ["nanosecond", "PT3723.004005006S"], | ||||
|         ]; | ||||
|         for (const [largestUnit, expected] of values) { | ||||
|             expect(plainTime.until(other, { largestUnit }).toString()).toBe(expected); | ||||
|         } | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| describe("errors", () => { | ||||
|     test("this value must be a Temporal.PlainTime object", () => { | ||||
|         expect(() => { | ||||
|             Temporal.PlainTime.prototype.until.call("foo", {}); | ||||
|         }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime"); | ||||
|     }); | ||||
| 
 | ||||
|     test("disallowed smallestUnit option values", () => { | ||||
|         const values = ["year", "month", "week", "day"]; | ||||
|         for (const smallestUnit of values) { | ||||
|             const plainTime = new Temporal.PlainTime(); | ||||
|             const other = new Temporal.PlainTime(); | ||||
|             expect(() => { | ||||
|                 plainTime.until(other, { smallestUnit }); | ||||
|             }).toThrowWithMessage( | ||||
|                 RangeError, | ||||
|                 `${smallestUnit} is not a valid value for option smallestUnit` | ||||
|             ); | ||||
|         } | ||||
|     }); | ||||
| 
 | ||||
|     test("disallowed largestUnit option values", () => { | ||||
|         const values = ["year", "month", "week", "day"]; | ||||
|         for (const largestUnit of values) { | ||||
|             const plainTime = new Temporal.PlainTime(); | ||||
|             const other = new Temporal.PlainTime(); | ||||
|             expect(() => { | ||||
|                 plainTime.until(other, { largestUnit }); | ||||
|             }).toThrowWithMessage( | ||||
|                 RangeError, | ||||
|                 `${largestUnit} is not a valid value for option largestUnit` | ||||
|             ); | ||||
|         } | ||||
|     }); | ||||
| }); | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh