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