AbstractOperations.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Span.h>
  8. #include <AK/String.h>
  9. #include <AK/Variant.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Runtime/Completion.h>
  13. #include <LibJS/Runtime/Intl/DisplayNames.h>
  14. #include <LibJS/Runtime/Intl/SingleUnitIdentifiers.h>
  15. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  16. #include <LibJS/Runtime/VM.h>
  17. #include <LibJS/Runtime/Value.h>
  18. #include <LibLocale/Forward.h>
  19. namespace JS::Intl {
  20. using LocaleKey = Variant<Empty, String>;
  21. Optional<LocaleKey> locale_key_from_value(Value);
  22. struct LocaleOptions {
  23. Value locale_matcher;
  24. Optional<LocaleKey> ca; // [[Calendar]]
  25. Optional<LocaleKey> co; // [[Collation]]
  26. Optional<LocaleKey> hc; // [[HourCycle]]
  27. Optional<LocaleKey> kf; // [[CaseFirst]]
  28. Optional<LocaleKey> kn; // [[Numeric]]
  29. Optional<LocaleKey> nu; // [[NumberingSystem]]
  30. };
  31. struct LocaleResult {
  32. String locale;
  33. String data_locale;
  34. LocaleKey ca; // [[Calendar]]
  35. LocaleKey co; // [[Collation]]
  36. LocaleKey hc; // [[HourCycle]]
  37. LocaleKey kf; // [[CaseFirst]]
  38. LocaleKey kn; // [[Numeric]]
  39. LocaleKey nu; // [[NumberingSystem]]
  40. };
  41. struct PatternPartition {
  42. PatternPartition() = default;
  43. PatternPartition(StringView type_string, String value_string)
  44. : type(type_string)
  45. , value(move(value_string))
  46. {
  47. }
  48. StringView type;
  49. String value;
  50. };
  51. struct PatternPartitionWithSource : public PatternPartition {
  52. template<typename ParentList>
  53. static Vector<PatternPartitionWithSource> create_from_parent_list(ParentList partitions)
  54. {
  55. Vector<PatternPartitionWithSource> result;
  56. result.ensure_capacity(partitions.size());
  57. for (auto& partition : partitions) {
  58. PatternPartitionWithSource partition_with_source {};
  59. partition_with_source.type = partition.type;
  60. partition_with_source.value = move(partition.value);
  61. result.unchecked_append(move(partition_with_source));
  62. }
  63. return result;
  64. }
  65. bool operator==(PatternPartitionWithSource const& other) const
  66. {
  67. return (type == other.type) && (value == other.value) && (source == other.source);
  68. }
  69. StringView source;
  70. };
  71. using StringOrBoolean = Variant<StringView, bool>;
  72. bool is_structurally_valid_language_tag(StringView locale);
  73. String canonicalize_unicode_locale_id(StringView locale);
  74. bool is_well_formed_currency_code(StringView currency);
  75. bool is_well_formed_unit_identifier(StringView unit_identifier);
  76. ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM&, Value locales);
  77. Optional<StringView> best_available_locale(StringView locale);
  78. String insert_unicode_extension_and_canonicalize(::Locale::LocaleID locale_id, ::Locale::LocaleExtension extension);
  79. LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptions const& options, ReadonlySpan<StringView> relevant_extension_keys);
  80. ThrowCompletionOr<Array*> supported_locales(VM&, Vector<String> const& requested_locales, Value options);
  81. ThrowCompletionOr<Object*> coerce_options_to_object(VM&, Value options);
  82. ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, ReadonlySpan<StringView> string_values, StringOrBoolean fallback);
  83. ThrowCompletionOr<Optional<int>> default_number_option(VM&, Value value, int minimum, int maximum, Optional<int> fallback);
  84. ThrowCompletionOr<Optional<int>> get_number_option(VM&, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
  85. Vector<PatternPartition> partition_pattern(StringView pattern);
  86. template<size_t Size>
  87. ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, StringView const (&string_values)[Size], StringOrBoolean fallback)
  88. {
  89. return get_boolean_or_string_number_format_option(vm, options, property, ReadonlySpan<StringView> { string_values }, move(fallback));
  90. }
  91. // NOTE: ECMA-402's GetOption is being removed in favor of a shared ECMA-262 GetOption in the Temporal proposal.
  92. // Until Temporal is merged into ECMA-262, our implementation lives in the Temporal-specific AO file & namespace.
  93. using Temporal::get_option;
  94. using Temporal::OptionType;
  95. }