ladybird/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
Timothy Flynn 07f12b108b LibJS: Implement a nearly empty Intl.NumberFormat object
This adds plumbing for the Intl.NumberFormat object, constructor, and
prototype.
2021-09-11 11:05:50 +01:00

229 lines
5.7 KiB
C++

/*
* 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();
}
}
}