Collator.h 2.9 KB

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