Collator.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Intl/CollatorCompareFunction.h>
  11. #include <LibJS/Runtime/Object.h>
  12. namespace JS::Intl {
  13. class Collator final : public Object {
  14. JS_OBJECT(Collator, Object);
  15. public:
  16. enum class Usage {
  17. Sort,
  18. Search,
  19. };
  20. enum class Sensitivity {
  21. Base,
  22. Accent,
  23. Case,
  24. Variant,
  25. };
  26. enum class CaseFirst {
  27. Upper,
  28. Lower,
  29. False,
  30. };
  31. static constexpr auto relevant_extension_keys()
  32. {
  33. // 10.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl-collator-internal-slots
  34. // 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.
  35. return AK::Array { "co"sv, "kf"sv, "kn"sv };
  36. }
  37. virtual ~Collator() override = default;
  38. String const& locale() const { return m_locale; }
  39. void set_locale(String locale) { m_locale = move(locale); }
  40. Usage usage() const { return m_usage; }
  41. void set_usage(StringView usage);
  42. StringView usage_string() const;
  43. Sensitivity sensitivity() const { return m_sensitivity; }
  44. void set_sensitivity(StringView sensitivity);
  45. StringView sensitivity_string() const;
  46. CaseFirst case_first() const { return m_case_first; }
  47. void set_case_first(StringView case_first);
  48. StringView case_first_string() const;
  49. String const& collation() const { return m_collation; }
  50. void set_collation(String collation) { m_collation = move(collation); }
  51. bool ignore_punctuation() const { return m_ignore_punctuation; }
  52. void set_ignore_punctuation(bool ignore_punctuation) { m_ignore_punctuation = ignore_punctuation; }
  53. bool numeric() const { return m_numeric; }
  54. void set_numeric(bool numeric) { m_numeric = numeric; }
  55. CollatorCompareFunction* bound_compare() const { return m_bound_compare; }
  56. void set_bound_compare(CollatorCompareFunction* bound_compare) { m_bound_compare = bound_compare; }
  57. private:
  58. explicit Collator(Object& prototype);
  59. virtual void visit_edges(Visitor&) override;
  60. String m_locale; // [[Locale]]
  61. Usage m_usage { Usage::Sort }; // [[Usage]]
  62. Sensitivity m_sensitivity { Sensitivity::Variant }; // [[Sensitivity]]
  63. CaseFirst m_case_first { CaseFirst::False }; // [[CaseFirst]]
  64. String m_collation; // [[Collation]]
  65. bool m_ignore_punctuation { false }; // [[IgnorePunctuation]]
  66. bool m_numeric { false }; // [[Numeric]]
  67. GCPtr<CollatorCompareFunction> m_bound_compare; // [[BoundCompare]]
  68. };
  69. }