2022-01-25 10:41:57 -05:00
|
|
|
/*
|
2024-06-09 14:36:48 -04:00
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
2022-01-25 10:41:57 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Array.h>
|
2023-01-19 13:13:57 -05:00
|
|
|
#include <AK/String.h>
|
2022-01-25 10:41:57 -05:00
|
|
|
#include <AK/StringView.h>
|
2022-01-25 11:50:14 -05:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2022-01-26 13:37:22 -10:00
|
|
|
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
2022-01-25 10:41:57 -05:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2022-09-02 12:11:30 -04:00
|
|
|
#include <LibLocale/Locale.h>
|
2024-06-09 14:36:48 -04:00
|
|
|
#include <LibLocale/NumberFormat.h>
|
2022-09-02 12:11:30 -04:00
|
|
|
#include <LibLocale/RelativeTimeFormat.h>
|
2022-01-25 10:41:57 -05:00
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
|
|
|
class RelativeTimeFormat final : public Object {
|
|
|
|
JS_OBJECT(RelativeTimeFormat, Object);
|
2023-11-19 09:45:05 +01:00
|
|
|
JS_DECLARE_ALLOCATOR(RelativeTimeFormat);
|
2022-01-25 10:41:57 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum class Numeric {
|
|
|
|
Always,
|
|
|
|
Auto,
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr auto relevant_extension_keys()
|
|
|
|
{
|
2022-03-15 11:09:11 -04:00
|
|
|
// 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots
|
2022-01-25 10:41:57 -05:00
|
|
|
// The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
|
|
|
|
return AK::Array { "nu"sv };
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RelativeTimeFormat() override = default;
|
|
|
|
|
2023-01-19 13:13:57 -05:00
|
|
|
String const& locale() const { return m_locale; }
|
|
|
|
void set_locale(String locale) { m_locale = move(locale); }
|
2022-01-25 10:41:57 -05:00
|
|
|
|
2023-01-19 13:13:57 -05:00
|
|
|
String const& data_locale() const { return m_data_locale; }
|
|
|
|
void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
|
2022-01-25 10:41:57 -05:00
|
|
|
|
2023-01-19 13:13:57 -05:00
|
|
|
String const& numbering_system() const { return m_numbering_system; }
|
|
|
|
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
|
2022-01-25 10:41:57 -05:00
|
|
|
|
2022-09-02 12:01:10 -04:00
|
|
|
::Locale::Style style() const { return m_style; }
|
|
|
|
void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
|
|
|
|
StringView style_string() const { return ::Locale::style_to_string(m_style); }
|
2022-01-25 10:41:57 -05:00
|
|
|
|
|
|
|
Numeric numeric() const { return m_numeric; }
|
|
|
|
void set_numeric(StringView numeric);
|
|
|
|
StringView numeric_string() const;
|
|
|
|
|
2022-01-26 12:03:35 -10:00
|
|
|
NumberFormat& number_format() const { return *m_number_format; }
|
2022-01-25 10:41:57 -05:00
|
|
|
void set_number_format(NumberFormat* number_format) { m_number_format = number_format; }
|
|
|
|
|
2022-07-07 13:59:46 -04:00
|
|
|
PluralRules& plural_rules() const { return *m_plural_rules; }
|
|
|
|
void set_plural_rules(PluralRules* plural_rules) { m_plural_rules = plural_rules; }
|
|
|
|
|
2022-01-25 10:41:57 -05:00
|
|
|
private:
|
2022-08-28 23:51:28 +02:00
|
|
|
explicit RelativeTimeFormat(Object& prototype);
|
|
|
|
|
2022-01-25 10:41:57 -05:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2023-01-19 13:13:57 -05:00
|
|
|
String m_locale; // [[Locale]]
|
|
|
|
String m_data_locale; // [[DataLocale]]
|
|
|
|
String m_numbering_system; // [[NumberingSystem]]
|
2022-09-02 12:01:10 -04:00
|
|
|
::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
|
|
|
|
Numeric m_numeric { Numeric::Always }; // [[Numeric]]
|
2023-02-26 16:09:02 -07:00
|
|
|
GCPtr<NumberFormat> m_number_format; // [[NumberFormat]]
|
|
|
|
GCPtr<PluralRules> m_plural_rules; // [[PluralRules]]
|
2022-01-25 10:41:57 -05:00
|
|
|
};
|
|
|
|
|
2022-01-26 13:37:22 -10:00
|
|
|
struct PatternPartitionWithUnit : public PatternPartition {
|
2023-01-21 10:34:07 -05:00
|
|
|
PatternPartitionWithUnit(StringView type, String value, StringView unit_string = {})
|
2022-01-26 13:37:22 -10:00
|
|
|
: PatternPartition(type, move(value))
|
|
|
|
, unit(unit_string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView unit;
|
|
|
|
};
|
|
|
|
|
2022-09-02 12:01:10 -04:00
|
|
|
ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
|
2022-08-20 08:25:24 +01:00
|
|
|
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
|
2024-06-09 14:36:48 -04:00
|
|
|
Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<::Locale::NumberFormat::Partition> parts);
|
2023-01-27 15:48:21 -05:00
|
|
|
ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
|
2023-08-30 12:41:11 -04:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
|
2022-01-25 11:50:14 -05:00
|
|
|
|
2022-01-25 10:41:57 -05:00
|
|
|
}
|