RelativeTimeFormat.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Enumerate.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Intl/NumberFormat.h>
  12. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  13. #include <LibJS/Runtime/Intl/PluralRules.h>
  14. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  15. namespace JS::Intl {
  16. JS_DEFINE_ALLOCATOR(RelativeTimeFormat);
  17. // 17 RelativeTimeFormat Objects, https://tc39.es/ecma402/#relativetimeformat-objects
  18. RelativeTimeFormat::RelativeTimeFormat(Object& prototype)
  19. : Object(ConstructWithPrototypeTag::Tag, prototype)
  20. {
  21. }
  22. // 17.5.1 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit
  23. ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(VM& vm, StringView unit)
  24. {
  25. // 1. If unit is "seconds", return "second".
  26. if (unit == "seconds"sv)
  27. return Unicode::TimeUnit::Second;
  28. // 2. If unit is "minutes", return "minute".
  29. if (unit == "minutes"sv)
  30. return Unicode::TimeUnit::Minute;
  31. // 3. If unit is "hours", return "hour".
  32. if (unit == "hours"sv)
  33. return Unicode::TimeUnit::Hour;
  34. // 4. If unit is "days", return "day".
  35. if (unit == "days"sv)
  36. return Unicode::TimeUnit::Day;
  37. // 5. If unit is "weeks", return "week".
  38. if (unit == "weeks"sv)
  39. return Unicode::TimeUnit::Week;
  40. // 6. If unit is "months", return "month".
  41. if (unit == "months"sv)
  42. return Unicode::TimeUnit::Month;
  43. // 7. If unit is "quarters", return "quarter".
  44. if (unit == "quarters"sv)
  45. return Unicode::TimeUnit::Quarter;
  46. // 8. If unit is "years", return "year".
  47. if (unit == "years"sv)
  48. return Unicode::TimeUnit::Year;
  49. // 9. If unit is not one of "second", "minute", "hour", "day", "week", "month", "quarter", or "year", throw a RangeError exception.
  50. // 10. Return unit.
  51. if (auto time_unit = Unicode::time_unit_from_string(unit); time_unit.has_value())
  52. return *time_unit;
  53. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidUnit, unit);
  54. }
  55. // 17.5.2 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern
  56. ThrowCompletionOr<Vector<Unicode::RelativeTimeFormat::Partition>> partition_relative_time_pattern(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
  57. {
  58. // 1. If value is NaN, +∞𝔽, or -∞𝔽, throw a RangeError exception.
  59. if (!Value(value).is_finite_number())
  60. return vm.throw_completion<RangeError>(ErrorType::NumberIsNaNOrInfinity);
  61. // 2. Let unit be ? SingularRelativeTimeUnit(unit).
  62. auto time_unit = TRY(singular_relative_time_unit(vm, unit));
  63. return relative_time_format.formatter().format_to_parts(value, time_unit, relative_time_format.numeric());
  64. }
  65. // 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
  66. ThrowCompletionOr<String> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
  67. {
  68. // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
  69. auto time_unit = TRY([&]() -> ThrowCompletionOr<Unicode::TimeUnit> {
  70. // NOTE: We short-circuit PartitionRelativeTimePattern as we do not need individual partitions. But we must still
  71. // perform the NaN/Infinity sanity checks and unit parsing from its first steps.
  72. // 1. If value is NaN, +∞𝔽, or -∞𝔽, throw a RangeError exception.
  73. if (!Value(value).is_finite_number())
  74. return vm.throw_completion<RangeError>(ErrorType::NumberIsNaNOrInfinity);
  75. // 2. Let unit be ? SingularRelativeTimeUnit(unit).
  76. return TRY(singular_relative_time_unit(vm, unit));
  77. }());
  78. // 2. Let result be the empty String.
  79. // 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
  80. // a. Set result to the string-concatenation of result and part.[[Value]].
  81. // 4. Return result.
  82. return relative_time_format.formatter().format(value, time_unit, relative_time_format.numeric());
  83. }
  84. // 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts
  85. ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
  86. {
  87. auto& realm = *vm.current_realm();
  88. // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
  89. auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit));
  90. // 2. Let result be ! ArrayCreate(0).
  91. auto result = MUST(Array::create(realm, 0));
  92. // 3. Let n be 0.
  93. // 4. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
  94. for (auto [n, part] : enumerate(parts)) {
  95. // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
  96. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  97. // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
  98. MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type)));
  99. // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
  100. MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value))));
  101. // d. If part.[[Unit]] is not empty, then
  102. if (!part.unit.is_empty()) {
  103. // i. Perform ! CreateDataPropertyOrThrow(O, "unit", part.[[Unit]]).
  104. MUST(object->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, part.unit)));
  105. }
  106. // e. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
  107. MUST(result->create_data_property_or_throw(n, object));
  108. // f. Increment n by 1.
  109. }
  110. // 5. Return result.
  111. return result;
  112. }
  113. }