RelativeTimeFormat.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022, 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/RelativeTimeFormat.h>
  15. namespace JS::Intl {
  16. class RelativeTimeFormat final : public Object {
  17. JS_OBJECT(RelativeTimeFormat, Object);
  18. JS_DECLARE_ALLOCATOR(RelativeTimeFormat);
  19. public:
  20. enum class Numeric {
  21. Always,
  22. Auto,
  23. };
  24. static constexpr auto relevant_extension_keys()
  25. {
  26. // 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots
  27. // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
  28. return AK::Array { "nu"sv };
  29. }
  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. ::Locale::Style style() const { return m_style; }
  38. void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
  39. StringView style_string() const { return ::Locale::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. PluralRules& plural_rules() const { return *m_plural_rules; }
  46. void set_plural_rules(PluralRules* plural_rules) { m_plural_rules = plural_rules; }
  47. private:
  48. explicit RelativeTimeFormat(Object& prototype);
  49. virtual void visit_edges(Cell::Visitor&) override;
  50. String m_locale; // [[Locale]]
  51. String m_data_locale; // [[DataLocale]]
  52. String m_numbering_system; // [[NumberingSystem]]
  53. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  54. Numeric m_numeric { Numeric::Always }; // [[Numeric]]
  55. GCPtr<NumberFormat> m_number_format; // [[NumberFormat]]
  56. GCPtr<PluralRules> m_plural_rules; // [[PluralRules]]
  57. };
  58. struct PatternPartitionWithUnit : public PatternPartition {
  59. PatternPartitionWithUnit(StringView type, String value, StringView unit_string = {})
  60. : PatternPartition(type, move(value))
  61. , unit(unit_string)
  62. {
  63. }
  64. StringView unit;
  65. };
  66. ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
  67. ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
  68. Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts);
  69. ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
  70. ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
  71. }