Skip to content

Commit

Permalink
LibJS: Implement Intl.ListFormat.prototype.formatToParts
Browse files Browse the repository at this point in the history
  • Loading branch information
trflynn89 authored and linusg committed Sep 6, 2021
1 parent cdba40f commit 5c06a91
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 0 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ namespace JS {
P(fontsize) \
P(forEach) \
P(format) \
P(formatToParts) \
P(fractionalSecondDigits) \
P(freeze) \
P(from) \
Expand Down
58 changes: 58 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ListFormatPrototype final : public Object {

private:
JS_DECLARE_NATIVE_FUNCTION(format);
JS_DECLARE_NATIVE_FUNCTION(format_to_parts);
};

}
Original file line number Diff line number Diff line change
@@ -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" },
]);
});
});

0 comments on commit 5c06a91

Please sign in to comment.