summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-09-06 14:57:57 -0400
committerLinus Groh <mail@linusgroh.de>2021-09-06 23:49:56 +0100
commit5c06a91dfa44318825224ec98af365ef4be626dc (patch)
tree7b28ae98fd27c86dcfa37f29607594637179c163
parentcdba40f7ea12aa3c84832df7a761b5d0a53dfa4b (diff)
downloadserenity-5c06a91dfa44318825224ec98af365ef4be626dc.zip
LibJS: Implement Intl.ListFormat.prototype.formatToParts
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp58
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.prototype.formatToParts.js231
4 files changed, 291 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 54e3373825..1220ec25cb 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -168,6 +168,7 @@ namespace JS {
P(fontsize) \
P(forEach) \
P(format) \
+ P(formatToParts) \
P(fractionalSecondDigits) \
P(freeze) \
P(from) \
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp
index 769424e0f7..8e5e40cfbe 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp
@@ -9,6 +9,7 @@
#include <AK/TypeCasts.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
+#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Intl/ListFormat.h>
@@ -191,6 +192,42 @@ static String format_list(ListFormat const& list_format, Vector<String> const& l
return result.build();
}
+// 13.1.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts
+static Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Let parts be CreatePartsFromList(listFormat, list).
+ auto parts = create_parts_from_list(list_format, list);
+
+ // 2. Let result be ArrayCreate(0).
+ auto result = 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 const& 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]]).
+ object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type));
+
+ // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
+ object->create_data_property_or_throw(vm.names.value, js_string(vm, part.value));
+
+ // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
+ result->create_data_property_or_throw(n, object);
+
+ // e. Increment n by 1.
+ ++n;
+ }
+
+ // 5. Return result.
+ return result;
+}
+
// 13.1.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
static Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable)
{
@@ -263,6 +300,7 @@ void ListFormatPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.format, format, 1, attr);
+ define_native_function(vm.names.formatToParts, format_to_parts, 1, attr);
}
// 13.4.3 Intl.ListFormat.prototype.format ( list ), https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.format
@@ -286,4 +324,24 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format)
return js_string(vm, move(formatted));
}
+// 13.4.4 Intl.ListFormat.prototype.formatToParts ( list ), https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.formatToParts
+JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format_to_parts)
+{
+ auto list = vm.argument(0);
+
+ // 1. Let lf be the this value.
+ // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
+ auto* list_format = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Let stringList be ? StringListFromIterable(list).
+ auto string_list = string_list_from_iterable(global_object, list);
+ if (vm.exception())
+ return {};
+
+ // 4. Return FormatListToParts(lf, stringList).
+ return format_list_to_parts(global_object, *list_format, string_list);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h
index 605db99c11..9608acf4de 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h
@@ -20,6 +20,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(format);
+ JS_DECLARE_NATIVE_FUNCTION(format_to_parts);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.prototype.formatToParts.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.prototype.formatToParts.js
new file mode 100644
index 0000000000..42ab1f78c5
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.prototype.formatToParts.js
@@ -0,0 +1,231 @@
+describe("errors", () => {
+ function SomeError() {}
+
+ test("called on non-ListFormat object", () => {
+ expect(() => {
+ Intl.ListFormat.prototype.formatToParts([]);
+ }).toThrowWithMessage(TypeError, "Not a Intl.ListFormat object");
+ });
+
+ test("called with non-string iterable", () => {
+ expect(() => {
+ new Intl.ListFormat().formatToParts([1]);
+ }).toThrowWithMessage(TypeError, "1 is not a string");
+ });
+
+ test("called with iterable that throws immediately", () => {
+ let iterable = {
+ [Symbol.iterator]() {
+ throw new SomeError();
+ },
+ };
+
+ expect(() => {
+ new Intl.ListFormat().formatToParts(iterable);
+ }).toThrow(SomeError);
+ });
+
+ test("called with iterable that throws on step", () => {
+ let iterable = {
+ [Symbol.iterator]() {
+ return this;
+ },
+ next() {
+ throw new SomeError();
+ },
+ };
+
+ expect(() => {
+ new Intl.ListFormat().formatToParts(iterable);
+ }).toThrow(SomeError);
+ });
+
+ test("called with iterable that throws on value resolution", () => {
+ let iterable = {
+ [Symbol.iterator]() {
+ return this;
+ },
+ next() {
+ return {
+ done: false,
+ get value() {
+ throw new SomeError();
+ },
+ };
+ },
+ };
+
+ expect(() => {
+ new Intl.ListFormat().formatToParts(iterable);
+ }).toThrow(SomeError);
+ });
+});
+
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Intl.ListFormat.prototype.formatToParts).toHaveLength(1);
+ });
+
+ test("undefined list returns empty string", () => {
+ expect(new Intl.ListFormat().formatToParts(undefined)).toEqual([]);
+ });
+});
+
+describe("type=conjunction", () => {
+ test("style=long", () => {
+ let en = new Intl.ListFormat("en", { type: "conjunction", style: "long" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " and " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", and " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=short", () => {
+ let en = new Intl.ListFormat("en", { type: "conjunction", style: "short" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " & " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", & " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=narrow", () => {
+ let en = new Intl.ListFormat("en", { type: "conjunction", style: "narrow" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "c" },
+ ]);
+ });
+});
+
+describe("type=disjunction", () => {
+ test("style=long", () => {
+ let en = new Intl.ListFormat("en", { type: "disjunction", style: "long" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " or " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", or " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=short", () => {
+ let en = new Intl.ListFormat("en", { type: "disjunction", style: "short" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " or " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", or " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=narrow", () => {
+ let en = new Intl.ListFormat("en", { type: "disjunction", style: "narrow" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " or " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", or " },
+ { type: "element", value: "c" },
+ ]);
+ });
+});
+
+describe("type=unit", () => {
+ test("style=long", () => {
+ let en = new Intl.ListFormat("en", { type: "unit", style: "long" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=short", () => {
+ let en = new Intl.ListFormat("en", { type: "unit", style: "short" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "b" },
+ { type: "literal", value: ", " },
+ { type: "element", value: "c" },
+ ]);
+ });
+
+ test("style=narrow", () => {
+ let en = new Intl.ListFormat("en", { type: "unit", style: "narrow" });
+ expect(en.formatToParts(["a"])).toEqual([{ type: "element", value: "a" }]);
+ expect(en.formatToParts(["a", "b"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " " },
+ { type: "element", value: "b" },
+ ]);
+ expect(en.formatToParts(["a", "b", "c"])).toEqual([
+ { type: "element", value: "a" },
+ { type: "literal", value: " " },
+ { type: "element", value: "b" },
+ { type: "literal", value: " " },
+ { type: "element", value: "c" },
+ ]);
+ });
+});