AbstractOperations.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/DeprecatedString.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/Intl/SingleUnitIdentifiers.h>
  15. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  16. #include <LibJS/Runtime/Value.h>
  17. #include <LibLocale/Forward.h>
  18. namespace JS::Intl {
  19. struct LocaleOptions {
  20. Value locale_matcher;
  21. Optional<String> ca; // [[Calendar]]
  22. Optional<String> co; // [[Collation]]
  23. Optional<String> hc; // [[HourCycle]]
  24. Optional<String> kf; // [[CaseFirst]]
  25. Optional<String> kn; // [[Numeric]]
  26. Optional<String> nu; // [[NumberingSystem]]
  27. };
  28. struct LocaleResult {
  29. String locale;
  30. String data_locale;
  31. Optional<String> ca; // [[Calendar]]
  32. Optional<String> co; // [[Collation]]
  33. Optional<String> hc; // [[HourCycle]]
  34. Optional<String> kf; // [[CaseFirst]]
  35. Optional<String> kn; // [[Numeric]]
  36. Optional<String> nu; // [[NumberingSystem]]
  37. };
  38. struct PatternPartition {
  39. PatternPartition() = default;
  40. PatternPartition(StringView type_string, DeprecatedString value_string)
  41. : type(type_string)
  42. , value(move(value_string))
  43. {
  44. }
  45. StringView type;
  46. DeprecatedString value;
  47. };
  48. struct PatternPartitionWithSource : public PatternPartition {
  49. static Vector<PatternPartitionWithSource> create_from_parent_list(Vector<PatternPartition> partitions)
  50. {
  51. Vector<PatternPartitionWithSource> result;
  52. result.ensure_capacity(partitions.size());
  53. for (auto& partition : partitions) {
  54. PatternPartitionWithSource partition_with_source {};
  55. partition_with_source.type = partition.type;
  56. partition_with_source.value = move(partition.value);
  57. result.append(move(partition_with_source));
  58. }
  59. return result;
  60. }
  61. bool operator==(PatternPartitionWithSource const& other) const
  62. {
  63. return (type == other.type) && (value == other.value) && (source == other.source);
  64. }
  65. StringView source;
  66. };
  67. using StringOrBoolean = Variant<StringView, bool>;
  68. ThrowCompletionOr<Optional<::Locale::LocaleID>> is_structurally_valid_language_tag(VM&, StringView locale);
  69. ThrowCompletionOr<String> canonicalize_unicode_locale_id(VM&, ::Locale::LocaleID& locale);
  70. bool is_well_formed_currency_code(StringView currency);
  71. bool is_well_formed_unit_identifier(StringView unit_identifier);
  72. ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM&, Value locales);
  73. Optional<StringView> best_available_locale(StringView locale);
  74. ThrowCompletionOr<String> insert_unicode_extension_and_canonicalize(VM&, ::Locale::LocaleID locale_id, ::Locale::LocaleExtension extension);
  75. ThrowCompletionOr<LocaleResult> resolve_locale(VM&, Vector<String> const& requested_locales, LocaleOptions const& options, Span<StringView const> relevant_extension_keys);
  76. ThrowCompletionOr<Array*> supported_locales(VM&, Vector<String> const& requested_locales, Value options);
  77. ThrowCompletionOr<Object*> coerce_options_to_object(VM&, Value options);
  78. ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM&, Object const& options, PropertyKey const& property, Span<StringView const> values, StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback);
  79. ThrowCompletionOr<Optional<int>> default_number_option(VM&, Value value, int minimum, int maximum, Optional<int> fallback);
  80. ThrowCompletionOr<Optional<int>> get_number_option(VM&, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
  81. ThrowCompletionOr<Vector<PatternPartition>> partition_pattern(VM&, StringView pattern);
  82. template<size_t Size>
  83. ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object const& options, PropertyKey const& property, StringView const (&values)[Size], StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback)
  84. {
  85. return get_string_or_boolean_option(vm, options, property, Span<StringView const> { values }, move(true_value), move(falsy_value), move(fallback));
  86. }
  87. // NOTE: ECMA-402's GetOption is being removed in favor of a shared ECMA-262 GetOption in the Temporal proposal.
  88. // Until Temporal is merged into ECMA-262, our implementation lives in the Temporal-specific AO file & namespace.
  89. using Temporal::get_option;
  90. using Temporal::OptionType;
  91. }