AbstractOperations.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/Temporal/AbstractOperations.h>
  15. #include <LibJS/Runtime/Value.h>
  16. #include <LibUnicode/Forward.h>
  17. namespace JS::Intl {
  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. struct PatternPartitionWithSource : public PatternPartition {
  48. static Vector<PatternPartitionWithSource> create_from_parent_list(Vector<PatternPartition> partitions)
  49. {
  50. Vector<PatternPartitionWithSource> result;
  51. result.ensure_capacity(partitions.size());
  52. for (auto& partition : partitions) {
  53. PatternPartitionWithSource partition_with_source {};
  54. partition_with_source.type = partition.type;
  55. partition_with_source.value = move(partition.value);
  56. result.append(move(partition_with_source));
  57. }
  58. return result;
  59. }
  60. bool operator==(PatternPartitionWithSource const& other) const
  61. {
  62. return (type == other.type) && (value == other.value) && (source == other.source);
  63. }
  64. StringView source;
  65. };
  66. // Table 2: Single units sanctioned for use in ECMAScript, https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers
  67. constexpr auto sanctioned_single_unit_identifiers()
  68. {
  69. 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 };
  70. }
  71. // Additional single units used in ECMAScript required by the Intl.DurationFormat proposal
  72. constexpr auto extra_sanctioned_single_unit_identifiers()
  73. {
  74. return AK::Array { "microsecond"sv, "nanosecond"sv };
  75. }
  76. using StringOrBoolean = Variant<StringView, bool>;
  77. Optional<Unicode::LocaleID> is_structurally_valid_language_tag(StringView locale);
  78. String canonicalize_unicode_locale_id(Unicode::LocaleID& locale);
  79. bool is_well_formed_currency_code(StringView currency);
  80. bool is_well_formed_unit_identifier(StringView unit_identifier);
  81. ThrowCompletionOr<Vector<String>> canonicalize_locale_list(GlobalObject&, Value locales);
  82. Optional<String> best_available_locale(StringView locale);
  83. String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Unicode::LocaleExtension extension);
  84. LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptions const& options, Span<StringView const> relevant_extension_keys);
  85. Vector<String> lookup_supported_locales(Vector<String> const& requested_locales);
  86. Vector<String> best_fit_supported_locales(Vector<String> const& requested_locales);
  87. ThrowCompletionOr<Array*> supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options);
  88. ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options);
  89. ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Span<StringView const> values, StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback);
  90. ThrowCompletionOr<Optional<int>> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback);
  91. ThrowCompletionOr<Optional<int>> get_number_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
  92. Vector<PatternPartition> partition_pattern(StringView pattern);
  93. template<size_t Size>
  94. ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, StringView const (&values)[Size], StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback)
  95. {
  96. return get_string_or_boolean_option(global_object, options, property, Span<StringView const> { values }, move(true_value), move(falsy_value), move(fallback));
  97. }
  98. // NOTE: ECMA-402's GetOption is being removed in favor of a shared ECMA-262 GetOption in the Temporal proposal.
  99. // Until Temporal is merged into ECMA-262, our implementation lives in the Temporal-specific AO file & namespace.
  100. using Temporal::get_option;
  101. using Temporal::OptionType;
  102. }