| 
									
										
										
										
											2022-01-25 10:41:57 -05:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:07:22 -05:00
										 |  |  |  |  * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-01-25 10:41:57 -05:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-25 11:50:14 -05:00
										 |  |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-25 11:50:14 -05:00
										 |  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/Intl/NumberFormat.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-07 13:59:46 -04:00
										 |  |  |  | #include <LibJS/Runtime/Intl/PluralRules.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-25 10:41:57 -05:00
										 |  |  |  | #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace JS::Intl { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 17 RelativeTimeFormat Objects, https://tc39.es/ecma402/#relativetimeformat-objects
 | 
					
						
							|  |  |  |  | RelativeTimeFormat::RelativeTimeFormat(Object& prototype) | 
					
						
							|  |  |  |  |     : Object(prototype) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void RelativeTimeFormat::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |  |     if (m_number_format) | 
					
						
							|  |  |  |  |         visitor.visit(m_number_format); | 
					
						
							| 
									
										
										
										
											2022-07-07 13:59:46 -04:00
										 |  |  |  |     if (m_plural_rules) | 
					
						
							|  |  |  |  |         visitor.visit(m_plural_rules); | 
					
						
							| 
									
										
										
										
											2022-01-25 10:41:57 -05:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void RelativeTimeFormat::set_numeric(StringView numeric) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     if (numeric == "always"sv) { | 
					
						
							|  |  |  |  |         m_numeric = Numeric::Always; | 
					
						
							|  |  |  |  |     } else if (numeric == "auto"sv) { | 
					
						
							|  |  |  |  |         m_numeric = Numeric::Auto; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | StringView RelativeTimeFormat::numeric_string() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     switch (m_numeric) { | 
					
						
							|  |  |  |  |     case Numeric::Always: | 
					
						
							|  |  |  |  |         return "always"sv; | 
					
						
							|  |  |  |  |     case Numeric::Auto: | 
					
						
							|  |  |  |  |         return "auto"sv; | 
					
						
							|  |  |  |  |     default: | 
					
						
							|  |  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 11:09:11 -04:00
										 |  |  |  | // 17.5.1 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  | ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM& vm, StringView unit) | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Assert: Type(unit) is String.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If unit is "seconds", return "second".
 | 
					
						
							|  |  |  |  |     if (unit == "seconds"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Second; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 3. If unit is "minutes", return "minute".
 | 
					
						
							|  |  |  |  |     if (unit == "minutes"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Minute; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 4. If unit is "hours", return "hour".
 | 
					
						
							|  |  |  |  |     if (unit == "hours"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Hour; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 5. If unit is "days", return "day".
 | 
					
						
							|  |  |  |  |     if (unit == "days"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Day; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 6. If unit is "weeks", return "week".
 | 
					
						
							|  |  |  |  |     if (unit == "weeks"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Week; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 7. If unit is "months", return "month".
 | 
					
						
							|  |  |  |  |     if (unit == "months"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Month; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 8. If unit is "quarters", return "quarter".
 | 
					
						
							|  |  |  |  |     if (unit == "quarters"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Quarter; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 9. If unit is "years", return "year".
 | 
					
						
							|  |  |  |  |     if (unit == "years"sv) | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         return ::Locale::TimeUnit::Year; | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 10. If unit is not one of "second", "minute", "hour", "day", "week", "month", "quarter", or "year", throw a RangeError exception.
 | 
					
						
							|  |  |  |  |     // 11. Return unit.
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |     if (auto time_unit = ::Locale::time_unit_from_string(unit); time_unit.has_value()) | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |         return *time_unit; | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |  |     return vm.throw_completion<RangeError>(ErrorType::IntlInvalidUnit, unit); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 11:09:11 -04:00
										 |  |  |  | // 17.5.2 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  | ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit) | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Assert: relativeTimeFormat has an [[InitializedRelativeTimeFormat]] internal slot.
 | 
					
						
							|  |  |  |  |     // 2. Assert: Type(value) is Number.
 | 
					
						
							|  |  |  |  |     // 3. Assert: Type(unit) is String.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. If value is NaN, +∞𝔽, or -∞𝔽, throw a RangeError exception.
 | 
					
						
							|  |  |  |  |     if (!Value(value).is_finite_number()) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |  |         return vm.throw_completion<RangeError>(ErrorType::IntlNumberIsNaNOrInfinity); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Let unit be ? SingularRelativeTimeUnit(unit).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  |     auto time_unit = TRY(singular_relative_time_unit(vm, unit)); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 6. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
 | 
					
						
							|  |  |  |  |     // 7. Let dataLocale be relativeTimeFormat.[[DataLocale]].
 | 
					
						
							|  |  |  |  |     auto const& data_locale = relative_time_format.data_locale(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 8. Let fields be localeData.[[<dataLocale>]].
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 9. Let style be relativeTimeFormat.[[Style]].
 | 
					
						
							|  |  |  |  |     auto style = relative_time_format.style(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // NOTE: The next steps form a "key" based on combining various formatting options into a string,
 | 
					
						
							|  |  |  |  |     //       then filtering the large set of locale data down to the pattern we are looking for. Instead,
 | 
					
						
							|  |  |  |  |     //       LibUnicode expects the individual options as enumeration values, and returns the couple of
 | 
					
						
							|  |  |  |  |     //       patterns that match those options.
 | 
					
						
							|  |  |  |  |     auto find_patterns_for_tense_or_number = [&](StringView tense_or_number) { | 
					
						
							|  |  |  |  |         // 10. If style is equal to "short", then
 | 
					
						
							|  |  |  |  |         //     a. Let entry be the string-concatenation of unit and "-short".
 | 
					
						
							|  |  |  |  |         // 11. Else if style is equal to "narrow", then
 | 
					
						
							|  |  |  |  |         //     a. Let entry be the string-concatenation of unit and "-narrow".
 | 
					
						
							|  |  |  |  |         // 12. Else,
 | 
					
						
							|  |  |  |  |         //     a. Let entry be unit.
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |         auto patterns = ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, style); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 13. If fields doesn't have a field [[<entry>]], then
 | 
					
						
							|  |  |  |  |         if (patterns.is_empty()) { | 
					
						
							|  |  |  |  |             // a. Let entry be unit.
 | 
					
						
							|  |  |  |  |             // NOTE: In the CLDR, the lack of "short" or "narrow" in the key implies "long".
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |             patterns = ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, ::Locale::Style::Long); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 14. Let patterns be fields.[[<entry>]].
 | 
					
						
							|  |  |  |  |         return patterns; | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 15. Let numeric be relativeTimeFormat.[[Numeric]].
 | 
					
						
							|  |  |  |  |     // 16. If numeric is equal to "auto", then
 | 
					
						
							|  |  |  |  |     if (relative_time_format.numeric() == RelativeTimeFormat::Numeric::Auto) { | 
					
						
							|  |  |  |  |         // a. Let valueString be ToString(value).
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |  |         auto value_string = MUST(Value(value).to_string(vm)); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // b. If patterns has a field [[<valueString>]], then
 | 
					
						
							|  |  |  |  |         if (auto patterns = find_patterns_for_tense_or_number(value_string); !patterns.is_empty()) { | 
					
						
							|  |  |  |  |             VERIFY(patterns.size() == 1); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             // i. Let result be patterns.[[<valueString>]].
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |  |             auto result = patterns[0].pattern.to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             // ii. Return a List containing the Record { [[Type]]: "literal", [[Value]]: result }.
 | 
					
						
							|  |  |  |  |             return Vector<PatternPartitionWithUnit> { { "literal"sv, move(result) } }; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 17. If value is -0𝔽 or if value is less than 0, then
 | 
					
						
							|  |  |  |  |     StringView tense; | 
					
						
							|  |  |  |  |     if (Value(value).is_negative_zero() || (value < 0)) { | 
					
						
							|  |  |  |  |         // a. Let tl be "past".
 | 
					
						
							|  |  |  |  |         tense = "past"sv; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: The spec does not say to do this, but nothing makes sense after this with a negative value.
 | 
					
						
							|  |  |  |  |         value = fabs(value); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     // 18. Else,
 | 
					
						
							|  |  |  |  |     else { | 
					
						
							|  |  |  |  |         // a. Let tl be "future".
 | 
					
						
							|  |  |  |  |         tense = "future"sv; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 19. Let po be patterns.[[<tl>]].
 | 
					
						
							|  |  |  |  |     auto patterns = find_patterns_for_tense_or_number(tense); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 20. Let fv be ! PartitionNumberPattern(relativeTimeFormat.[[NumberFormat]], value).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  |     auto value_partitions = partition_number_pattern(vm, relative_time_format.number_format(), Value(value)); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 21. Let pr be ! ResolvePlural(relativeTimeFormat.[[PluralRules]], value).
 | 
					
						
							| 
									
										
										
										
											2022-07-19 12:11:34 -04:00
										 |  |  |  |     auto plurality = resolve_plural(relative_time_format.plural_rules(), Value(value)); | 
					
						
							| 
									
										
										
										
											2022-07-07 13:59:46 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |     // 22. Let pattern be po.[[<pr>]].
 | 
					
						
							| 
									
										
										
										
											2022-07-07 13:59:46 -04:00
										 |  |  |  |     auto pattern = patterns.find_if([&](auto& p) { return p.plurality == plurality; }); | 
					
						
							|  |  |  |  |     if (pattern == patterns.end()) | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  |         return Vector<PatternPartitionWithUnit> {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 23. Return ! MakePartsList(pattern, unit, fv).
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:01:10 -04:00
										 |  |  |  |     return make_parts_list(pattern->pattern, ::Locale::time_unit_to_string(time_unit), move(value_partitions)); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 11:09:11 -04:00
										 |  |  |  | // 17.5.3 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist
 | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Let patternParts be PartitionPattern(pattern).
 | 
					
						
							|  |  |  |  |     auto pattern_parts = partition_pattern(pattern); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Let result be a new empty List.
 | 
					
						
							|  |  |  |  |     Vector<PatternPartitionWithUnit> result; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. For each Record { [[Type]], [[Value]] } patternPart in patternParts, do
 | 
					
						
							|  |  |  |  |     for (auto& pattern_part : pattern_parts) { | 
					
						
							|  |  |  |  |         // a. If patternPart.[[Type]] is "literal", then
 | 
					
						
							|  |  |  |  |         if (pattern_part.type == "literal"sv) { | 
					
						
							|  |  |  |  |             // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]], [[Unit]]: empty } to result.
 | 
					
						
							|  |  |  |  |             result.empend("literal"sv, move(pattern_part.value)); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         // b. Else,
 | 
					
						
							|  |  |  |  |         else { | 
					
						
							|  |  |  |  |             // i. Assert: patternPart.[[Type]] is "0".
 | 
					
						
							|  |  |  |  |             VERIFY(pattern_part.type == "0"sv); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             // ii. For each Record { [[Type]], [[Value]] } part in parts, do
 | 
					
						
							|  |  |  |  |             for (auto& part : parts) { | 
					
						
							|  |  |  |  |                 // 1. Append Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: unit } to result.
 | 
					
						
							|  |  |  |  |                 result.empend(part.type, move(part.value), unit); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Return result.
 | 
					
						
							|  |  |  |  |     return result; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 11:09:11 -04:00
										 |  |  |  | // 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | ThrowCompletionOr<DeprecatedString> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit) | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  |     auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit)); | 
					
						
							| 
									
										
										
										
											2022-01-26 13:37:22 -10:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Let result be an empty String.
 | 
					
						
							|  |  |  |  |     StringBuilder result; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
 | 
					
						
							|  |  |  |  |     for (auto& part : parts) { | 
					
						
							|  |  |  |  |         // a. Set result to the string-concatenation of result and part.[[Value]].
 | 
					
						
							|  |  |  |  |         result.append(move(part.value)); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Return result.
 | 
					
						
							|  |  |  |  |     return result.build(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 11:09:11 -04:00
										 |  |  |  | // 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  | ThrowCompletionOr<Array*> format_relative_time_to_parts(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit) | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |  |     auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit)); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 08:30:48 -04:00
										 |  |  |  |     // 2. Let result be ! ArrayCreate(0).
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:49:49 +00:00
										 |  |  |  |     auto result = MUST(Array::create(realm, 0)); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Let n be 0.
 | 
					
						
							|  |  |  |  |     size_t n = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
 | 
					
						
							|  |  |  |  |     for (auto& part : parts) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |  |         // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |  |         auto* object = Object::create(realm, realm.intrinsics().object_prototype()); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
 | 
					
						
							| 
									
										
										
										
											2022-12-06 22:17:27 +00:00
										 |  |  |  |         MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type))); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
 | 
					
						
							| 
									
										
										
										
											2022-12-06 22:17:27 +00:00
										 |  |  |  |         MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value)))); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // d. If part.[[Unit]] is not empty, then
 | 
					
						
							|  |  |  |  |         if (!part.unit.is_empty()) { | 
					
						
							|  |  |  |  |             // i. Perform ! CreateDataPropertyOrThrow(O, "unit", part.[[Unit]]).
 | 
					
						
							| 
									
										
										
										
											2022-12-06 22:17:27 +00:00
										 |  |  |  |             MUST(object->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, part.unit))); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // e. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
 | 
					
						
							|  |  |  |  |         MUST(result->create_data_property_or_throw(n, object)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // f. Increment n by 1.
 | 
					
						
							|  |  |  |  |         ++n; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Return result.
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:49:49 +00:00
										 |  |  |  |     return result.ptr(); | 
					
						
							| 
									
										
										
										
											2022-01-26 22:44:01 -05:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 10:41:57 -05:00
										 |  |  |  | } |