1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

LibJS: Begin implementing Intl.NumberFormat.prototype.format

There is quite a lot to be done here so this is just a first pass at
number formatting. Decimal and percent formatting are mostly working,
but only for standard and compact notation (engineering and scientific
notation are not implemented here). Currency formatting is parsed, but
there is more work to be done to handle e.g. using symbols instead of
currency codes ("$" instead of "USD"), and putting spaces around the
currency symbol ("USD 2.00" instead of "USD2.00").
This commit is contained in:
Timothy Flynn 2021-11-10 12:34:14 -05:00 committed by Linus Groh
parent 0469006263
commit 89523f70cf
8 changed files with 1433 additions and 1 deletions

View file

@ -8,6 +8,7 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
namespace JS::Intl {
@ -150,7 +151,12 @@ public:
StringView sign_display_string() const;
void set_sign_display(StringView sign_display);
NativeFunction* bound_format() const { return m_bound_format; }
void set_bound_format(NativeFunction* bound_format) { m_bound_format = bound_format; }
private:
virtual void visit_edges(Visitor&) override;
String m_locale; // [[Locale]]
String m_data_locale; // [[DataLocale]]
String m_numbering_system; // [[NumberingSystem]]
@ -170,11 +176,31 @@ private:
Notation m_notation { Notation::Invalid }; // [[Notation]]
Optional<CompactDisplay> m_compact_display {}; // [[CompactDisplay]]
SignDisplay m_sign_display { SignDisplay::Invalid }; // [[SignDisplay]]
NativeFunction* m_bound_format { nullptr }; // [[BoundFormat]]
};
struct FormatResult {
String formatted_string; // [[FormattedString]]
double rounded_number { 0.0 }; // [[RoundedNumber]]
};
struct RawFormatResult : public FormatResult {
int digits { 0 }; // [[IntegerDigitsCount]]
};
ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation);
ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value);
int currency_digits(StringView currency);
FormatResult format_numeric_to_string(NumberFormat& number_format, double number);
Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, double number);
Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_format, double number, String formatted_string, int exponent);
String format_numeric(NumberFormat& number_format, double number);
RawFormatResult to_raw_precision(double number, int min_precision, int max_precision);
RawFormatResult to_raw_fixed(double number, int min_fraction, int max_fraction);
ThrowCompletionOr<void> set_number_format_unit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options);
Optional<StringView> get_number_format_pattern(NumberFormat& number_format, double number);
StringView get_notation_sub_pattern(NumberFormat& number_format, int exponent);
int compute_exponent(NumberFormat& number_format, double number);
int compute_exponent_for_magniude(NumberFormat& number_format, int magnitude);
}