diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-11-10 12:34:14 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-12 09:17:08 +0000 |
commit | 89523f70cf4bc58098136b3ccb5f8542c316cd29 (patch) | |
tree | 7d6d000451cbf3d3bb259304f303be4f6fb0df09 /Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp | |
parent | 04690062639c47feff664b75476543f252d16d2e (diff) | |
download | serenity-89523f70cf4bc58098136b3ccb5f8542c316cd29.zip |
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").
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp new file mode 100644 index 0000000000..1d656d20a5 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/NumberFormat.h> +#include <LibJS/Runtime/Intl/NumberFormatFunction.h> + +namespace JS::Intl { + +// 15.1.4 Number Format Functions +NumberFormatFunction* NumberFormatFunction::create(GlobalObject& global_object, NumberFormat& number_format) +{ + return global_object.heap().allocate<NumberFormatFunction>(global_object, number_format, *global_object.function_prototype()); +} + +NumberFormatFunction::NumberFormatFunction(NumberFormat& number_format, Object& prototype) + : NativeFunction(prototype) + , m_number_format(number_format) +{ +} + +void NumberFormatFunction::initialize(GlobalObject& global_object) +{ + Base::initialize(global_object); + define_direct_property(vm().names.length, Value(1), Attribute::Configurable); +} + +ThrowCompletionOr<Value> NumberFormatFunction::call() +{ + auto& global_object = this->global_object(); + auto& vm = global_object.vm(); + + // 1. Let nf be F.[[NumberFormat]]. + // 2. Assert: Type(nf) is Object and nf has an [[InitializedNumberFormat]] internal slot. + // 3. If value is not provided, let value be undefined. + auto value = vm.argument(0); + + // 4. Let x be ? ToNumeric(value). + value = TRY(value.to_numeric(global_object)); + + // 5. Return ? FormatNumeric(nf, x). + // Note: Our implementation of FormatNumeric does not throw. + auto formatted = format_numeric(m_number_format, value.as_double()); + + return js_string(vm, move(formatted)); +} + +void NumberFormatFunction::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(&m_number_format); +} + +} |