123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /*
- * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <LibJS/Runtime/Intl/NumberFormat.h>
- namespace JS::Intl {
- // 15 NumberFormat Objects, https://tc39.es/ecma402/#numberformat-objects
- NumberFormat::NumberFormat(Object& prototype)
- : Object(prototype)
- {
- }
- void NumberFormat::set_style(StringView style)
- {
- if (style == "decimal"sv)
- m_style = Style::Decimal;
- else if (style == "percent"sv)
- m_style = Style::Percent;
- else if (style == "currency"sv)
- m_style = Style::Currency;
- else if (style == "unit"sv)
- m_style = Style::Unit;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::style_string() const
- {
- switch (m_style) {
- case Style::Decimal:
- return "decimal"sv;
- case Style::Percent:
- return "percent"sv;
- case Style::Currency:
- return "currency"sv;
- case Style::Unit:
- return "unit"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_currency_display(StringView currency_display)
- {
- if (currency_display == "code"sv)
- m_currency_display = CurrencyDisplay::Code;
- else if (currency_display == "symbol"sv)
- m_currency_display = CurrencyDisplay::Symbol;
- else if (currency_display == "narrowSymbol"sv)
- m_currency_display = CurrencyDisplay::NarrowSymbol;
- else if (currency_display == "name"sv)
- m_currency_display = CurrencyDisplay::Name;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::currency_display_string() const
- {
- VERIFY(m_currency_display.has_value());
- switch (*m_currency_display) {
- case CurrencyDisplay::Code:
- return "code"sv;
- case CurrencyDisplay::Symbol:
- return "symbol"sv;
- case CurrencyDisplay::NarrowSymbol:
- return "narrowSymbol"sv;
- case CurrencyDisplay::Name:
- return "name"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_currency_sign(StringView currency_sign)
- {
- if (currency_sign == "standard"sv)
- m_currency_sign = CurrencySign::Standard;
- else if (currency_sign == "accounting"sv)
- m_currency_sign = CurrencySign::Accounting;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::currency_sign_string() const
- {
- VERIFY(m_currency_sign.has_value());
- switch (*m_currency_sign) {
- case CurrencySign::Standard:
- return "standard"sv;
- case CurrencySign::Accounting:
- return "accounting"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_unit_display(StringView unit_display)
- {
- if (unit_display == "short"sv)
- m_unit_display = UnitDisplay::Short;
- else if (unit_display == "narrow"sv)
- m_unit_display = UnitDisplay::Narrow;
- else if (unit_display == "long"sv)
- m_unit_display = UnitDisplay::Long;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::unit_display_string() const
- {
- VERIFY(m_unit_display.has_value());
- switch (*m_unit_display) {
- case UnitDisplay::Short:
- return "short"sv;
- case UnitDisplay::Narrow:
- return "narrow"sv;
- case UnitDisplay::Long:
- return "long"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- StringView NumberFormat::rounding_type_string() const
- {
- switch (m_rounding_type) {
- case RoundingType::SignificantDigits:
- return "significantDigits"sv;
- case RoundingType::FractionDigits:
- return "fractionDigits"sv;
- case RoundingType::CompactRounding:
- return "compactRounding"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_notation(StringView notation)
- {
- if (notation == "standard"sv)
- m_notation = Notation::Standard;
- else if (notation == "scientific"sv)
- m_notation = Notation::Scientific;
- else if (notation == "engineering"sv)
- m_notation = Notation::Engineering;
- else if (notation == "compact"sv)
- m_notation = Notation::Compact;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::notation_string() const
- {
- switch (m_notation) {
- case Notation::Standard:
- return "standard"sv;
- case Notation::Scientific:
- return "scientific"sv;
- case Notation::Engineering:
- return "engineering"sv;
- case Notation::Compact:
- return "compact"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_compact_display(StringView compact_display)
- {
- if (compact_display == "short"sv)
- m_compact_display = CompactDisplay::Short;
- else if (compact_display == "long"sv)
- m_compact_display = CompactDisplay::Long;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::compact_display_string() const
- {
- VERIFY(m_compact_display.has_value());
- switch (*m_compact_display) {
- case CompactDisplay::Short:
- return "short"sv;
- case CompactDisplay::Long:
- return "long"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void NumberFormat::set_sign_display(StringView sign_display)
- {
- if (sign_display == "auto"sv)
- m_sign_display = SignDisplay::Auto;
- else if (sign_display == "never"sv)
- m_sign_display = SignDisplay::Never;
- else if (sign_display == "always"sv)
- m_sign_display = SignDisplay::Always;
- else if (sign_display == "exceptZero"sv)
- m_sign_display = SignDisplay::ExceptZero;
- else
- VERIFY_NOT_REACHED();
- }
- StringView NumberFormat::sign_display_string() const
- {
- switch (m_sign_display) {
- case SignDisplay::Auto:
- return "auto"sv;
- case SignDisplay::Never:
- return "never"sv;
- case SignDisplay::Always:
- return "always"sv;
- case SignDisplay::ExceptZero:
- return "exceptZero"sv;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- }
|