RelativeTimeFormat.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <LibUnicode/Locale.h>
  14. #include <LibUnicode/NumberFormat.h>
  15. #include <LibUnicode/RelativeTimeFormat.h>
  16. namespace JS::Intl {
  17. class RelativeTimeFormat final : public Object {
  18. JS_OBJECT(RelativeTimeFormat, Object);
  19. GC_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& numbering_system() const { return m_numbering_system; }
  31. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  32. Unicode::Style style() const { return m_style; }
  33. void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
  34. StringView style_string() const { return Unicode::style_to_string(m_style); }
  35. Unicode::NumericDisplay numeric() const { return m_numeric; }
  36. void set_numeric(StringView numeric) { m_numeric = Unicode::numeric_display_from_string(numeric); }
  37. StringView numeric_string() const { return Unicode::numeric_display_to_string(m_numeric); }
  38. Unicode::RelativeTimeFormat const& formatter() const { return *m_formatter; }
  39. void set_formatter(NonnullOwnPtr<Unicode::RelativeTimeFormat> formatter) { m_formatter = move(formatter); }
  40. private:
  41. explicit RelativeTimeFormat(Object& prototype);
  42. String m_locale; // [[Locale]]
  43. String m_numbering_system; // [[NumberingSystem]]
  44. Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
  45. Unicode::NumericDisplay m_numeric { Unicode::NumericDisplay::Always }; // [[Numeric]]
  46. // Non-standard. Stores the ICU relative-time formatter for the Intl object's formatting options.
  47. OwnPtr<Unicode::RelativeTimeFormat> m_formatter;
  48. };
  49. ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
  50. ThrowCompletionOr<Vector<Unicode::RelativeTimeFormat::Partition>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
  51. ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
  52. ThrowCompletionOr<GC::Ref<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
  53. }