AbstractOperations.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, 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/Span.h>
  9. #include <AK/String.h>
  10. #include <AK/Variant.h>
  11. #include <AK/Vector.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Runtime/Intl/DisplayNames.h>
  14. #include <LibJS/Runtime/Value.h>
  15. #include <LibUnicode/Forward.h>
  16. namespace JS::Intl {
  17. using Fallback = Variant<Empty, bool, StringView>;
  18. struct LocaleOptions {
  19. Value locale_matcher;
  20. Optional<String> ca; // [[Calendar]]
  21. Optional<String> co; // [[Collation]]
  22. Optional<String> hc; // [[HourCycle]]
  23. Optional<String> kf; // [[CaseFirst]]
  24. Optional<String> kn; // [[Numeric]]
  25. Optional<String> nu; // [[NumberingSystem]]
  26. };
  27. struct LocaleResult {
  28. String locale;
  29. String data_locale;
  30. Optional<String> ca; // [[Calendar]]
  31. Optional<String> co; // [[Collation]]
  32. Optional<String> hc; // [[HourCycle]]
  33. Optional<String> kf; // [[CaseFirst]]
  34. Optional<String> kn; // [[Numeric]]
  35. Optional<String> nu; // [[NumberingSystem]]
  36. };
  37. struct PatternPartition {
  38. PatternPartition() = default;
  39. PatternPartition(StringView type_string, String value_string)
  40. : type(type_string)
  41. , value(move(value_string))
  42. {
  43. }
  44. StringView type;
  45. String value;
  46. };
  47. constexpr auto sanctioned_simple_unit_identifiers()
  48. {
  49. return AK::Array { "acre"sv, "bit"sv, "byte"sv, "celsius"sv, "centimeter"sv, "day"sv, "degree"sv, "fahrenheit"sv, "fluid-ounce"sv, "foot"sv, "gallon"sv, "gigabit"sv, "gigabyte"sv, "gram"sv, "hectare"sv, "hour"sv, "inch"sv, "kilobit"sv, "kilobyte"sv, "kilogram"sv, "kilometer"sv, "liter"sv, "megabit"sv, "megabyte"sv, "meter"sv, "mile"sv, "mile-scandinavian"sv, "milliliter"sv, "millimeter"sv, "millisecond"sv, "minute"sv, "month"sv, "ounce"sv, "percent"sv, "petabyte"sv, "pound"sv, "second"sv, "stone"sv, "terabit"sv, "terabyte"sv, "week"sv, "yard"sv, "year"sv };
  50. }
  51. Optional<Unicode::LocaleID> is_structurally_valid_language_tag(StringView locale);
  52. String canonicalize_unicode_locale_id(Unicode::LocaleID& locale);
  53. bool is_well_formed_currency_code(StringView currency);
  54. bool is_well_formed_unit_identifier(StringView unit_identifier);
  55. ThrowCompletionOr<Vector<String>> canonicalize_locale_list(GlobalObject&, Value locales);
  56. Optional<String> best_available_locale(StringView locale);
  57. String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Unicode::LocaleExtension extension);
  58. LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptions const& options, Span<StringView const> relevant_extension_keys);
  59. Vector<String> lookup_supported_locales(Vector<String> const& requested_locales);
  60. Vector<String> best_fit_supported_locales(Vector<String> const& requested_locales);
  61. ThrowCompletionOr<Array*> supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options);
  62. ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options);
  63. ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, Span<StringView const> values, Fallback fallback);
  64. ThrowCompletionOr<Optional<int>> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback);
  65. ThrowCompletionOr<Optional<int>> get_number_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
  66. Vector<PatternPartition> partition_pattern(StringView pattern);
  67. template<size_t Size>
  68. ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, StringView const (&values)[Size], Fallback fallback)
  69. {
  70. return get_option(global_object, options, property, type, Span<StringView const> { values }, fallback);
  71. }
  72. }