2021-09-05 23:05:16 -04:00
|
|
|
/*
|
2024-06-08 14:57:25 -04:00
|
|
|
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
|
2021-09-05 23:05:16 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-01-19 13:13:57 -05:00
|
|
|
#include <AK/String.h>
|
2021-09-05 23:05:16 -04:00
|
|
|
#include <AK/StringView.h>
|
2021-09-11 10:57:09 -04:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
2021-09-05 23:05:16 -04:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2024-06-08 14:57:25 -04:00
|
|
|
#include <LibLocale/ListFormat.h>
|
2022-09-02 12:11:30 -04:00
|
|
|
#include <LibLocale/Locale.h>
|
2021-09-05 23:05:16 -04:00
|
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
|
|
|
|
|
class ListFormat final : public Object {
|
|
|
|
|
JS_OBJECT(ListFormat, Object);
|
2023-11-19 09:45:05 +01:00
|
|
|
JS_DECLARE_ALLOCATOR(ListFormat);
|
2021-09-05 23:05:16 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
enum class Type {
|
|
|
|
|
Invalid,
|
|
|
|
|
Conjunction,
|
|
|
|
|
Disjunction,
|
|
|
|
|
Unit,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual ~ListFormat() 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); }
|
2021-09-05 23:05:16 -04:00
|
|
|
|
2024-06-08 14:57:25 -04:00
|
|
|
::Locale::ListFormatType type() const { return m_type; }
|
|
|
|
|
void set_type(StringView type) { m_type = ::Locale::list_format_type_from_string(type); }
|
|
|
|
|
StringView type_string() const { return ::Locale::list_format_type_to_string(m_type); }
|
2021-09-05 23:05:16 -04: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); }
|
2021-09-05 23:05:16 -04:00
|
|
|
|
2024-06-17 16:46:59 -04:00
|
|
|
::Locale::ListFormat const& formatter() const { return *m_formatter; }
|
|
|
|
|
void set_formatter(NonnullOwnPtr<::Locale::ListFormat> formatter) { m_formatter = move(formatter); }
|
|
|
|
|
|
2021-09-05 23:05:16 -04:00
|
|
|
private:
|
2022-08-28 23:51:28 +02:00
|
|
|
explicit ListFormat(Object& prototype);
|
|
|
|
|
|
2024-06-08 14:57:25 -04:00
|
|
|
String m_locale; // [[Locale]]
|
|
|
|
|
::Locale::ListFormatType m_type { ::Locale::ListFormatType::Conjunction }; // [[Type]]
|
|
|
|
|
::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
|
2024-06-17 16:46:59 -04:00
|
|
|
|
|
|
|
|
// Non-standard. Stores the ICU list formatter for the Intl object's formatting options.
|
|
|
|
|
OwnPtr<::Locale::ListFormat> m_formatter;
|
2021-09-05 23:05:16 -04:00
|
|
|
};
|
|
|
|
|
|
2024-06-17 16:46:59 -04:00
|
|
|
Vector<::Locale::ListFormat::Partition> create_parts_from_list(ListFormat const&, ReadonlySpan<String> list);
|
|
|
|
|
String format_list(ListFormat const&, ReadonlySpan<String> list);
|
|
|
|
|
NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, ReadonlySpan<String> list);
|
2023-01-21 10:34:07 -05:00
|
|
|
ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2021-09-05 23:05:16 -04:00
|
|
|
}
|