Collator.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/Object.h>
  11. namespace JS::Intl {
  12. class Collator final : public Object {
  13. JS_OBJECT(Collator, Object);
  14. public:
  15. enum class Usage {
  16. Sort,
  17. Search,
  18. };
  19. enum class Sensitivity {
  20. Base,
  21. Accent,
  22. Case,
  23. Variant,
  24. };
  25. enum class CaseFirst {
  26. Upper,
  27. Lower,
  28. False,
  29. };
  30. static constexpr auto relevant_extension_keys()
  31. {
  32. // 10.2.3 Internal Slots, https://tc39.es/ecma402/#sec-intl-collator-internal-slots
  33. // 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.
  34. return AK::Array { "co"sv, "kf"sv, "kn"sv };
  35. }
  36. explicit Collator(Object& prototype);
  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. private:
  56. String m_locale; // [[Locale]]
  57. Usage m_usage { Usage::Sort }; // [[Usage]]
  58. Sensitivity m_sensitivity { Sensitivity::Variant }; // [[Sensitivity]]
  59. CaseFirst m_case_first { CaseFirst::False }; // [[CaseFirst]]
  60. String m_collation; // [[Collation]]
  61. bool m_ignore_punctuation { false }; // [[IgnorePunctuation]]
  62. bool m_numeric { false }; // [[Numeric]]
  63. };
  64. }