RelativeTimeFormat.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  12. #include <LibJS/Runtime/Object.h>
  13. #include <LibLocale/Locale.h>
  14. #include <LibLocale/NumberFormat.h>
  15. #include <LibLocale/RelativeTimeFormat.h>
  16. namespace JS::Intl {
  17. class RelativeTimeFormat final : public Object {
  18. JS_OBJECT(RelativeTimeFormat, Object);
  19. JS_DECLARE_ALLOCATOR(RelativeTimeFormat);
  20. public:
  21. static constexpr auto relevant_extension_keys()
  22. {
  23. // 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots
  24. // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
  25. return AK::Array { "nu"sv };
  26. }
  27. virtual ~RelativeTimeFormat() override = default;
  28. String const& locale() const { return m_locale; }
  29. void set_locale(String locale) { m_locale = move(locale); }
  30. String const& data_locale() const { return m_data_locale; }
  31. void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
  32. String const& numbering_system() const { return m_numbering_system; }
  33. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  34. ::Locale::Style style() const { return m_style; }
  35. void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
  36. StringView style_string() const { return ::Locale::style_to_string(m_style); }
  37. ::Locale::NumericDisplay numeric() const { return m_numeric; }
  38. void set_numeric(StringView numeric) { m_numeric = ::Locale::numeric_display_from_string(numeric); }
  39. StringView numeric_string() const { return ::Locale::numeric_display_to_string(m_numeric); }
  40. ::Locale::RelativeTimeFormat const& formatter() const { return *m_formatter; }
  41. void set_formatter(NonnullOwnPtr<::Locale::RelativeTimeFormat> formatter) { m_formatter = move(formatter); }
  42. private:
  43. explicit RelativeTimeFormat(Object& prototype);
  44. String m_locale; // [[Locale]]
  45. String m_data_locale; // [[DataLocale]]
  46. String m_numbering_system; // [[NumberingSystem]]
  47. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  48. ::Locale::NumericDisplay m_numeric { ::Locale::NumericDisplay::Always }; // [[Numeric]]
  49. // Non-standard. Stores the ICU relative-time formatter for the Intl object's formatting options.
  50. OwnPtr<::Locale::RelativeTimeFormat> m_formatter;
  51. };
  52. ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
  53. ThrowCompletionOr<Vector<::Locale::RelativeTimeFormat::Partition>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
  54. ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
  55. ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
  56. }