2021-09-06 22:18:48 -04:00
/*
2024-08-14 14:46:19 -04:00
* Copyright ( c ) 2022 - 2024 , Tim Flynn < trflynn89 @ serenityos . org >
2021-09-06 22:18:48 -04: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>
2021-09-06 22:18:48 -04:00
# include <AK/StringView.h>
2022-02-20 21:00:37 +02:00
# include <LibJS/Runtime/Intl/CollatorCompareFunction.h>
2021-09-06 22:18:48 -04:00
# include <LibJS/Runtime/Object.h>
2024-08-14 14:46:19 -04:00
# include <LibUnicode/Collator.h>
2021-09-06 22:18:48 -04:00
namespace JS : : Intl {
class Collator final : public Object {
JS_OBJECT ( Collator , Object ) ;
2024-11-15 04:01:23 +13:00
GC_DECLARE_ALLOCATOR ( Collator ) ;
2021-09-06 22:18:48 -04:00
public :
static constexpr auto relevant_extension_keys ( )
{
2022-03-15 10:33:30 -04:00
// 10.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl-collator-internal-slots
2021-09-06 22:18:48 -04:00
// The value of the [[RelevantExtensionKeys]] internal slot is a List that must include the element "co", may include any or all of the elements "kf" and "kn", and must not include any other elements.
return AK : : Array { " co " sv , " kf " sv , " kn " sv } ;
}
virtual ~ Collator ( ) 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-06 22:18:48 -04:00
2024-08-14 14:46:19 -04:00
Unicode : : Usage usage ( ) const { return m_usage ; }
void set_usage ( StringView usage ) { m_usage = Unicode : : usage_from_string ( usage ) ; }
StringView usage_string ( ) const { return Unicode : : usage_to_string ( m_usage ) ; }
2021-09-06 22:18:48 -04:00
2024-08-14 14:46:19 -04:00
Unicode : : Sensitivity sensitivity ( ) const { return m_sensitivity ; }
2024-08-14 16:18:06 -04:00
void set_sensitivity ( Unicode : : Sensitivity sensitivity ) { m_sensitivity = sensitivity ; }
2024-08-14 14:46:19 -04:00
StringView sensitivity_string ( ) const { return Unicode : : sensitivity_to_string ( m_sensitivity ) ; }
2021-09-06 22:18:48 -04:00
2024-08-14 14:46:19 -04:00
Unicode : : CaseFirst case_first ( ) const { return m_case_first ; }
void set_case_first ( StringView case_first ) { m_case_first = Unicode : : case_first_from_string ( case_first ) ; }
StringView case_first_string ( ) const { return Unicode : : case_first_to_string ( m_case_first ) ; }
2021-09-06 22:18:48 -04:00
2023-01-19 13:13:57 -05:00
String const & collation ( ) const { return m_collation ; }
void set_collation ( String collation ) { m_collation = move ( collation ) ; }
2021-09-06 22:18:48 -04:00
bool ignore_punctuation ( ) const { return m_ignore_punctuation ; }
void set_ignore_punctuation ( bool ignore_punctuation ) { m_ignore_punctuation = ignore_punctuation ; }
bool numeric ( ) const { return m_numeric ; }
void set_numeric ( bool numeric ) { m_numeric = numeric ; }
2022-02-20 21:00:37 +02:00
CollatorCompareFunction * bound_compare ( ) const { return m_bound_compare ; }
void set_bound_compare ( CollatorCompareFunction * bound_compare ) { m_bound_compare = bound_compare ; }
2024-08-14 14:46:19 -04:00
Unicode : : Collator const & collator ( ) const { return * m_collator ; }
void set_collator ( NonnullOwnPtr < Unicode : : Collator > collator ) { m_collator = move ( collator ) ; }
2021-09-06 22:18:48 -04:00
private :
2022-08-28 23:51:28 +02:00
explicit Collator ( Object & prototype ) ;
2022-02-20 21:00:37 +02:00
virtual void visit_edges ( Visitor & ) override ;
2024-08-14 14:46:19 -04:00
String m_locale ; // [[Locale]]
Unicode : : Usage m_usage { Unicode : : Usage : : Sort } ; // [[Usage]]
Unicode : : Sensitivity m_sensitivity { Unicode : : Sensitivity : : Variant } ; // [[Sensitivity]]
Unicode : : CaseFirst m_case_first { Unicode : : CaseFirst : : False } ; // [[CaseFirst]]
String m_collation ; // [[Collation]]
bool m_ignore_punctuation { false } ; // [[IgnorePunctuation]]
bool m_numeric { false } ; // [[Numeric]]
2024-11-15 04:01:23 +13:00
GC : : Ptr < CollatorCompareFunction > m_bound_compare ; // [[BoundCompare]]
2024-08-14 14:46:19 -04:00
// Non-standard. Stores the ICU collator for the Intl object's collation options.
OwnPtr < Unicode : : Collator > m_collator ;
2021-09-06 22:18:48 -04:00
} ;
}