mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 05:32:44 +00:00 
			
		
		
		
	LibJS: Implement Temporal.Duration.prototype.toJSON()
This commit is contained in:
		
							parent
							
								
									b2548393d2
								
							
						
					
					
						commit
						90fa356b93
					
				
					 3 changed files with 32 additions and 0 deletions
				
			
		|  | @ -46,6 +46,7 @@ void DurationPrototype::initialize(GlobalObject& global_object) | |||
|     define_native_function(vm.names.negated, negated, 0, attr); | ||||
|     define_native_function(vm.names.abs, abs, 0, attr); | ||||
|     define_native_function(vm.names.toString, to_string, 0, attr); | ||||
|     define_native_function(vm.names.toJSON, to_json, 0, attr); | ||||
|     define_native_function(vm.names.valueOf, value_of, 0, attr); | ||||
| } | ||||
| 
 | ||||
|  | @ -311,6 +312,17 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_string) | |||
|     return js_string(vm, temporal_duration_to_string(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, precision.precision)); | ||||
| } | ||||
| 
 | ||||
| // 7.3.23 Temporal.Duration.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tojson
 | ||||
| JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_json) | ||||
| { | ||||
|     // 1. Let duration be the this value.
 | ||||
|     // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
 | ||||
|     auto* duration = TRY(typed_this_object(global_object)); | ||||
| 
 | ||||
|     // 3. Return ! TemporalDurationToString(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "auto").
 | ||||
|     return js_string(vm, temporal_duration_to_string(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv)); | ||||
| } | ||||
| 
 | ||||
| // 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
 | ||||
| JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::value_of) | ||||
| { | ||||
|  |  | |||
|  | @ -36,6 +36,7 @@ private: | |||
|     JS_DECLARE_NATIVE_FUNCTION(negated); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(abs); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(to_string); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(to_json); | ||||
|     JS_DECLARE_NATIVE_FUNCTION(value_of); | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,19 @@ | |||
| describe("correct behavior", () => { | ||||
|     test("length is 0", () => { | ||||
|         expect(Temporal.Duration.prototype.toJSON).toHaveLength(0); | ||||
|     }); | ||||
| 
 | ||||
|     test("basic functionality", () => { | ||||
|         expect(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toJSON()).toBe( | ||||
|             "P1Y2M3W4DT5H6M7.00800901S" | ||||
|         ); | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| describe("errors", () => { | ||||
|     test("this value must be a Temporal.Duration object", () => { | ||||
|         expect(() => { | ||||
|             Temporal.Duration.prototype.toJSON.call("foo"); | ||||
|         }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration"); | ||||
|     }); | ||||
| }); | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh