RelativeTimeFormat.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  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/RelativeTimeFormat.h>
  15. namespace JS::Intl {
  16. class RelativeTimeFormat final : public Object {
  17. JS_OBJECT(RelativeTimeFormat, Object);
  18. public:
  19. enum class Numeric {
  20. Always,
  21. Auto,
  22. };
  23. static constexpr auto relevant_extension_keys()
  24. {
  25. // 17.3.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots
  26. // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
  27. return AK::Array { "nu"sv };
  28. }
  29. RelativeTimeFormat(Object& prototype);
  30. virtual ~RelativeTimeFormat() override = default;
  31. String const& locale() const { return m_locale; }
  32. void set_locale(String locale) { m_locale = move(locale); }
  33. String const& data_locale() const { return m_data_locale; }
  34. void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
  35. String const& numbering_system() const { return m_numbering_system; }
  36. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  37. Unicode::Style style() const { return m_style; }
  38. void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
  39. StringView style_string() const { return Unicode::style_to_string(m_style); }
  40. Numeric numeric() const { return m_numeric; }
  41. void set_numeric(StringView numeric);
  42. StringView numeric_string() const;
  43. NumberFormat& number_format() const { return *m_number_format; }
  44. void set_number_format(NumberFormat* number_format) { m_number_format = number_format; }
  45. private:
  46. virtual void visit_edges(Cell::Visitor&) override;
  47. String m_locale; // [[Locale]]
  48. String m_data_locale; // [[DataLocale]]
  49. String m_numbering_system; // [[NumberingSystem]]
  50. Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
  51. Numeric m_numeric { Numeric::Always }; // [[Numeric]]
  52. NumberFormat* m_number_format { nullptr }; // [[NumberFormat]]
  53. };
  54. struct PatternPartitionWithUnit : public PatternPartition {
  55. PatternPartitionWithUnit(StringView type, String value, StringView unit_string = {})
  56. : PatternPartition(type, move(value))
  57. , unit(unit_string)
  58. {
  59. }
  60. StringView unit;
  61. };
  62. ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value);
  63. ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& global_object, StringView unit);
  64. ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit);
  65. Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts);
  66. ThrowCompletionOr<String> format_relative_time(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit);
  67. }