summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-07-20 15:29:00 -0400
committerLinus Groh <mail@linusgroh.de>2022-07-20 22:30:16 +0100
commite9e187d15c2e950be44fab081d62213730e4ae7d (patch)
tree38a1c6b4bc1ee1b44705b0d557fafffa2a5b4341 /Userland
parentb4a772cde232db5ad45b44e1862ae009497bbac7 (diff)
downloadserenity-e9e187d15c2e950be44fab081d62213730e4ae7d.zip
LibJS: Implement Intl.NumberFormat.prototype.formatRangeToParts
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp39
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp27
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Intl/NumberFormat/NumberFormat.prototype.formatRangeToParts.js168
5 files changed, 236 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
index 5e93aaf2ee..b71ce35c89 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
@@ -1862,4 +1862,43 @@ ThrowCompletionOr<String> format_numeric_range(GlobalObject& global_object, Numb
return result.build();
}
+// 1.1.25 FormatNumericRangeToParts( numberFormat, x, y ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-formatnumericrangetoparts
+ThrowCompletionOr<Array*> format_numeric_range_to_parts(GlobalObject& global_object, NumberFormat& number_format, MathematicalValue start, MathematicalValue end)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Let parts be ? PartitionNumberRangePattern(numberFormat, x, y).
+ auto parts = TRY(partition_number_range_pattern(global_object, number_format, move(start), move(end)));
+
+ // 2. Let result be ! ArrayCreate(0).
+ auto* result = MUST(Array::create(global_object, 0));
+
+ // 3. Let n be 0.
+ size_t n = 0;
+
+ // 4. For each Record { [[Type]], [[Value]] } part in parts, do
+ for (auto& part : parts) {
+ // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
+ auto* object = Object::create(global_object, global_object.object_prototype());
+
+ // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
+ MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
+
+ // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
+ MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value))));
+
+ // d. Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]).
+ MUST(object->create_data_property_or_throw(vm.names.source, js_string(vm, part.source)));
+
+ // e. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
+ MUST(result->create_data_property_or_throw(n, object));
+
+ // f. Increment n by 1.
+ ++n;
+ }
+
+ // 5. Return result.
+ return result;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
index 2e235c41f8..4348acf306 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
@@ -291,5 +291,6 @@ ThrowCompletionOr<Vector<PatternPartitionWithSource>> partition_number_range_pat
Vector<PatternPartitionWithSource> format_approximately(NumberFormat& number_format, Vector<PatternPartitionWithSource> result);
Vector<PatternPartitionWithSource> collapse_number_range(Vector<PatternPartitionWithSource> result);
ThrowCompletionOr<String> format_numeric_range(GlobalObject& global_object, NumberFormat& number_format, MathematicalValue start, MathematicalValue end);
+ThrowCompletionOr<Array*> format_numeric_range_to_parts(GlobalObject& global_object, NumberFormat& number_format, MathematicalValue start, MathematicalValue end);
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
index b6897522c1..298fa5ad7c 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
@@ -33,6 +33,7 @@ void NumberFormatPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.formatToParts, format_to_parts, 1, attr);
define_native_function(vm.names.formatRange, format_range, 2, attr);
+ define_native_function(vm.names.formatRangeToParts, format_range_to_parts, 2, attr);
define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr);
}
@@ -104,6 +105,32 @@ JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format_range)
return js_string(vm, move(formatted));
}
+// 1.4.6 Intl.NumberFormat.prototype.formatRangeToParts ( start, end ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-intl.numberformat.prototype.formatrangetoparts
+JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format_range_to_parts)
+{
+ auto start = vm.argument(0);
+ auto end = vm.argument(1);
+
+ // 1. Let nf be the this value.
+ // 2. Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).
+ auto* number_format = TRY(typed_this_object(global_object));
+
+ // 3. If start is undefined or end is undefined, throw a TypeError exception.
+ if (start.is_undefined())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "start"sv);
+ if (end.is_undefined())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "end"sv);
+
+ // 4. Let x be ? ToIntlMathematicalValue(start).
+ auto x = TRY(to_intl_mathematical_value(global_object, start));
+
+ // 5. Let y be ? ToIntlMathematicalValue(end).
+ auto y = TRY(to_intl_mathematical_value(global_object, end));
+
+ // 6. Return ? FormatNumericRangeToParts(nf, x, y).
+ return TRY(format_numeric_range_to_parts(global_object, *number_format, move(x), move(y)));
+}
+
// 15.3.5 Intl.NumberFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.numberformat.prototype.resolvedoptions
JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::resolved_options)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.h
index 9528038a62..0f73bd19a6 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.h
@@ -23,6 +23,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(format);
JS_DECLARE_NATIVE_FUNCTION(format_to_parts);
JS_DECLARE_NATIVE_FUNCTION(format_range);
+ JS_DECLARE_NATIVE_FUNCTION(format_range_to_parts);
JS_DECLARE_NATIVE_FUNCTION(resolved_options);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/NumberFormat/NumberFormat.prototype.formatRangeToParts.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/NumberFormat/NumberFormat.prototype.formatRangeToParts.js
new file mode 100644
index 0000000000..0da99fb828
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/NumberFormat/NumberFormat.prototype.formatRangeToParts.js
@@ -0,0 +1,168 @@
+describe("errors", () => {
+ test("called on non-NumberFormat object", () => {
+ expect(() => {
+ Intl.NumberFormat.prototype.formatRangeToParts();
+ }).toThrowWithMessage(TypeError, "Not an object of type Intl.NumberFormat");
+ });
+
+ test("called without enough values", () => {
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts();
+ }).toThrowWithMessage(TypeError, "start is undefined");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1);
+ }).toThrowWithMessage(TypeError, "end is undefined");
+ });
+
+ test("called with values that cannot be converted to numbers", () => {
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(Symbol.hasInstance, 1);
+ }).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1, Symbol.hasInstance);
+ }).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
+ });
+
+ test("called with invalid numbers", () => {
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(NaN, 1);
+ }).toThrowWithMessage(RangeError, "start must not be NaN");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1, NaN);
+ }).toThrowWithMessage(RangeError, "end must not be NaN");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1, 0);
+ }).toThrowWithMessage(
+ RangeError,
+ "start is a mathematical value, end is a mathematical value and end < start"
+ );
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1, -Infinity);
+ }).toThrowWithMessage(RangeError, "start is a mathematical value, end is -∞");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(1, -0);
+ }).toThrowWithMessage(RangeError, "start is a mathematical value, end is -0 and start ≥ 0");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(Infinity, 0);
+ }).toThrowWithMessage(RangeError, "start is +∞, end is a mathematical value");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(Infinity, -Infinity);
+ }).toThrowWithMessage(RangeError, "start is +∞, end is -∞");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(Infinity, -0);
+ }).toThrowWithMessage(RangeError, "start is +∞, end is -0");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(-0, -1);
+ }).toThrowWithMessage(RangeError, "start is -0, end is a mathematical value and end < 0");
+
+ expect(() => {
+ new Intl.NumberFormat().formatRangeToParts(-0, -Infinity);
+ }).toThrowWithMessage(RangeError, "start is -0, end is -∞");
+ });
+});
+
+describe("correct behavior", () => {
+ test("basic functionality", () => {
+ const en1 = new Intl.NumberFormat("en");
+ expect(en1.formatRangeToParts(100, 101)).toEqual([
+ { type: "integer", value: "100", source: "startRange" },
+ { type: "literal", value: "–", source: "shared" },
+ { type: "integer", value: "101", source: "endRange" },
+ ]);
+
+ const ja1 = new Intl.NumberFormat("ja");
+ expect(ja1.formatRangeToParts(100, 101)).toEqual([
+ { type: "integer", value: "100", source: "startRange" },
+ { type: "literal", value: "~", source: "shared" },
+ { type: "integer", value: "101", source: "endRange" },
+ ]);
+ });
+
+ test("approximately formatting", () => {
+ const en1 = new Intl.NumberFormat("en", { maximumFractionDigits: 0 });
+ expect(en1.formatRangeToParts(2.9, 3.1)).toEqual([
+ { type: "approximatelySign", value: "~", source: "" },
+ { type: "integer", value: "3", source: "" },
+ ]);
+
+ const en2 = new Intl.NumberFormat("en", {
+ style: "currency",
+ currency: "USD",
+ maximumFractionDigits: 0,
+ });
+ expect(en2.formatRangeToParts(2.9, 3.1)).toEqual([
+ { type: "approximatelySign", value: "~", source: "" },
+ { type: "currency", value: "$", source: "" },
+ { type: "integer", value: "3", source: "" },
+ ]);
+
+ const ja1 = new Intl.NumberFormat("ja", { maximumFractionDigits: 0 });
+ expect(ja1.formatRangeToParts(2.9, 3.1)).toEqual([
+ { type: "approximatelySign", value: "約", source: "" },
+ { type: "integer", value: "3", source: "" },
+ ]);
+
+ const ja2 = new Intl.NumberFormat("ja", {
+ style: "currency",
+ currency: "JPY",
+ maximumFractionDigits: 0,
+ });
+ expect(ja2.formatRangeToParts(2.9, 3.1)).toEqual([
+ { type: "approximatelySign", value: "約", source: "" },
+ { type: "currency", value: "¥", source: "" },
+ { type: "integer", value: "3", source: "" },
+ ]);
+ });
+
+ test("range pattern spacing", () => {
+ const en1 = new Intl.NumberFormat("en");
+ expect(en1.formatRangeToParts(3, 5)).toEqual([
+ { type: "integer", value: "3", source: "startRange" },
+ { type: "literal", value: "–", source: "shared" },
+ { type: "integer", value: "5", source: "endRange" },
+ ]);
+
+ const en2 = new Intl.NumberFormat("en", {
+ style: "currency",
+ currency: "USD",
+ maximumFractionDigits: 0,
+ });
+ expect(en2.formatRangeToParts(3, 5)).toEqual([
+ { type: "currency", value: "$", source: "startRange" },
+ { type: "integer", value: "3", source: "startRange" },
+ { type: "literal", value: " – ", source: "shared" },
+ { type: "currency", value: "$", source: "endRange" },
+ { type: "integer", value: "5", source: "endRange" },
+ ]);
+
+ const ja1 = new Intl.NumberFormat("ja");
+ expect(ja1.formatRangeToParts(3, 5)).toEqual([
+ { type: "integer", value: "3", source: "startRange" },
+ { type: "literal", value: "~", source: "shared" },
+ { type: "integer", value: "5", source: "endRange" },
+ ]);
+
+ const ja2 = new Intl.NumberFormat("ja", {
+ style: "currency",
+ currency: "JPY",
+ maximumFractionDigits: 0,
+ });
+ expect(ja2.formatRangeToParts(3, 5)).toEqual([
+ { type: "currency", value: "¥", source: "startRange" },
+ { type: "integer", value: "3", source: "startRange" },
+ { type: "literal", value: " ~ ", source: "shared" },
+ { type: "currency", value: "¥", source: "endRange" },
+ { type: "integer", value: "5", source: "endRange" },
+ ]);
+ });
+});