MathematicalValue.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Variant.h>
  9. #include <LibCrypto/BigInt/SignedBigInteger.h>
  10. #include <LibJS/Runtime/BigInt.h>
  11. #include <LibJS/Runtime/Value.h>
  12. #include <LibLocale/NumberFormat.h>
  13. namespace JS::Intl {
  14. // https://tc39.es/ecma402/#intl-mathematical-value
  15. class MathematicalValue {
  16. public:
  17. enum class Symbol {
  18. PositiveInfinity,
  19. NegativeInfinity,
  20. NegativeZero,
  21. NotANumber,
  22. };
  23. MathematicalValue() = default;
  24. explicit MathematicalValue(double value)
  25. : m_value(value_from_number(value))
  26. {
  27. }
  28. explicit MathematicalValue(String value)
  29. : m_value(move(value))
  30. {
  31. }
  32. explicit MathematicalValue(Symbol symbol)
  33. : m_value(symbol)
  34. {
  35. }
  36. MathematicalValue(Value value)
  37. : m_value(value.is_number()
  38. ? value_from_number(value.as_double())
  39. : ValueType(MUST(value.as_bigint().big_integer().to_base(10))))
  40. {
  41. }
  42. bool is_number() const;
  43. double as_number() const;
  44. bool is_string() const;
  45. String const& as_string() const;
  46. bool is_mathematical_value() const;
  47. bool is_positive_infinity() const;
  48. bool is_negative_infinity() const;
  49. bool is_negative_zero() const;
  50. bool is_nan() const;
  51. ::Locale::NumberFormat::Value to_value() const;
  52. private:
  53. using ValueType = Variant<double, String, Symbol>;
  54. static ValueType value_from_number(double number);
  55. ValueType m_value { 0.0 };
  56. };
  57. }