AbstractOperations.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/Date.h>
  14. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  15. #include <LibJS/Runtime/Value.h>
  16. #include <LibUnicode/Locale.h>
  17. namespace JS::Intl {
  18. using LocaleKey = Variant<Empty, String>;
  19. Optional<LocaleKey> locale_key_from_value(Value);
  20. struct LocaleOptions {
  21. Value locale_matcher;
  22. Optional<LocaleKey> ca; // [[Calendar]]
  23. Optional<LocaleKey> co; // [[Collation]]
  24. Optional<LocaleKey> hc; // [[HourCycle]]
  25. Optional<LocaleKey> kf; // [[CaseFirst]]
  26. Optional<LocaleKey> kn; // [[Numeric]]
  27. Optional<LocaleKey> nu; // [[NumberingSystem]]
  28. };
  29. struct MatchedLocale {
  30. String locale;
  31. Optional<Unicode::Extension> extension;
  32. };
  33. struct ResolvedLocale {
  34. String locale;
  35. LocaleKey ca; // [[Calendar]]
  36. LocaleKey co; // [[Collation]]
  37. LocaleKey hc; // [[HourCycle]]
  38. LocaleKey kf; // [[CaseFirst]]
  39. LocaleKey kn; // [[Numeric]]
  40. LocaleKey nu; // [[NumberingSystem]]
  41. };
  42. using StringOrBoolean = Variant<StringView, bool>;
  43. bool is_structurally_valid_language_tag(StringView locale);
  44. String canonicalize_unicode_locale_id(StringView locale);
  45. bool is_well_formed_currency_code(StringView currency);
  46. Vector<TimeZoneIdentifier> const& available_named_time_zone_identifiers();
  47. Optional<TimeZoneIdentifier const&> get_available_named_time_zone_identifier(StringView time_zone_identifier);
  48. bool is_well_formed_unit_identifier(StringView unit_identifier);
  49. ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM&, Value locales);
  50. Optional<MatchedLocale> lookup_matching_locale_by_prefix(ReadonlySpan<String> requested_locales);
  51. Optional<MatchedLocale> lookup_matching_locale_by_best_fit(ReadonlySpan<String> requested_locales);
  52. String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Vector<String> attributes, Vector<Unicode::Keyword> keywords);
  53. ResolvedLocale resolve_locale(ReadonlySpan<String> requested_locales, LocaleOptions const& options, ReadonlySpan<StringView> relevant_extension_keys);
  54. ThrowCompletionOr<Array*> filter_locales(VM& vm, ReadonlySpan<String> requested_locales, Value options);
  55. ThrowCompletionOr<Object*> coerce_options_to_object(VM&, Value options);
  56. ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, ReadonlySpan<StringView> string_values, StringOrBoolean fallback);
  57. ThrowCompletionOr<Optional<int>> default_number_option(VM&, Value value, int minimum, int maximum, Optional<int> fallback);
  58. ThrowCompletionOr<Optional<int>> get_number_option(VM&, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
  59. template<size_t Size>
  60. ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, StringView const (&string_values)[Size], StringOrBoolean fallback)
  61. {
  62. return get_boolean_or_string_number_format_option(vm, options, property, ReadonlySpan<StringView> { string_values }, move(fallback));
  63. }
  64. // NOTE: ECMA-402's GetOption is being removed in favor of a shared ECMA-262 GetOption in the Temporal proposal.
  65. // Until Temporal is merged into ECMA-262, our implementation lives in the Temporal-specific AO file & namespace.
  66. using Temporal::get_option;
  67. using Temporal::OptionType;
  68. }