Skip to content

Commit 1f90a9d

Browse files
committed
LibJS: Implement Array.prototype.groupByToMap
1 parent a0e43a4 commit 1f90a9d

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <LibJS/Runtime/Error.h>
2020
#include <LibJS/Runtime/FunctionObject.h>
2121
#include <LibJS/Runtime/GlobalObject.h>
22+
#include <LibJS/Runtime/Map.h>
2223
#include <LibJS/Runtime/ObjectPrototype.h>
2324
#include <LibJS/Runtime/Realm.h>
2425
#include <LibJS/Runtime/Value.h>
@@ -73,6 +74,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
7374
define_native_function(vm.names.entries, entries, 0, attr);
7475
define_native_function(vm.names.copyWithin, copy_within, 2, attr);
7576
define_native_function(vm.names.groupBy, group_by, 1, attr);
77+
define_native_function(vm.names.groupByToMap, group_by_to_map, 1, attr);
7678

7779
// Use define_direct_property here instead of define_native_function so that
7880
// Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
@@ -1748,4 +1750,75 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by)
17481750
return object;
17491751
}
17501752

1753+
// 2.2 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
1754+
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by_to_map)
1755+
{
1756+
auto callback_function = vm.argument(0);
1757+
auto this_arg = vm.argument(1);
1758+
1759+
// 1. Let O be ? ToObject(this value).
1760+
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
1761+
1762+
// 2. Let len be ? LengthOfArrayLike(O).
1763+
auto length = TRY(length_of_array_like(global_object, *this_object));
1764+
1765+
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
1766+
if (!callback_function.is_function())
1767+
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback_function.to_string_without_side_effects());
1768+
1769+
struct KeyedGroupTraits : public Traits<Handle<Value>> {
1770+
static unsigned hash(Handle<Value> const& value_handle)
1771+
{
1772+
return ValueTraits::hash(value_handle.value());
1773+
}
1774+
1775+
static bool equals(Handle<Value> const& a, Handle<Value> const& b)
1776+
{
1777+
// AddValueToKeyedGroup uses SameValue on the keys on Step 1.a.
1778+
return same_value(a.value(), b.value());
1779+
}
1780+
};
1781+
1782+
// 5. Let groups be a new empty List.
1783+
OrderedHashMap<Handle<Value>, MarkedValueList, KeyedGroupTraits> groups;
1784+
1785+
// 4. Let k be 0.
1786+
// 6. Repeat, while k < len
1787+
for (size_t index = 0; index < length; ++index) {
1788+
// a. Let Pk be ! ToString(𝔽(k)).
1789+
auto index_property = PropertyKey { index };
1790+
1791+
// b. Let kValue be ? Get(O, Pk).
1792+
auto k_value = TRY(this_object->get(index_property));
1793+
1794+
// c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
1795+
auto key = TRY(vm.call(callback_function.as_function(), this_arg, k_value, Value(index), this_object));
1796+
1797+
// d. If key is -0𝔽, set key to +0𝔽.
1798+
if (key.is_negative_zero())
1799+
key = Value(0);
1800+
1801+
// e. Perform ! AddValueToKeyedGroup(groups, key, kValue).
1802+
add_value_to_keyed_group(global_object, groups, make_handle(key), k_value);
1803+
1804+
// f. Set k to k + 1.
1805+
}
1806+
1807+
// 7. Let map be ! Construct(%Map%).
1808+
auto* map = Map::create(global_object);
1809+
1810+
// 8. For each Record { [[Key]], [[Elements]] } g of groups, do
1811+
for (auto& group : groups) {
1812+
// a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
1813+
auto* elements = Array::create_from(global_object, group.value);
1814+
1815+
// b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
1816+
// c. Append entry as the last element of map.[[MapData]].
1817+
map->entries().set(group.key.value(), elements);
1818+
}
1819+
1820+
// 9. Return map.
1821+
return map;
1822+
}
1823+
17511824
}

Userland/Libraries/LibJS/Runtime/ArrayPrototype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ArrayPrototype final : public Array {
5555
JS_DECLARE_NATIVE_FUNCTION(entries);
5656
JS_DECLARE_NATIVE_FUNCTION(copy_within);
5757
JS_DECLARE_NATIVE_FUNCTION(group_by);
58+
JS_DECLARE_NATIVE_FUNCTION(group_by_to_map);
5859
};
5960

6061
}

Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ namespace JS {
242242
P(globalThis) \
243243
P(group) \
244244
P(groupBy) \
245+
P(groupByToMap) \
245246
P(groupCollapsed) \
246247
P(groupEnd) \
247248
P(groups) \

Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,28 @@ describe("ability to work with generic non-array objects", () => {
317317
expect(result.false).toEqual(["foo", undefined, undefined]);
318318
expect(result.true).toEqual(["bar", "baz"]);
319319
});
320+
321+
test("groupByToMap", () => {
322+
const visited = [];
323+
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
324+
const falseObject = { false: false };
325+
const trueObject = { true: true };
326+
const result = Array.prototype.groupByToMap.call(o, (value, _, object) => {
327+
expect(object).toBe(o);
328+
visited.push(value);
329+
return value !== undefined
330+
? value.startsWith("b")
331+
? trueObject
332+
: falseObject
333+
: falseObject;
334+
});
335+
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
336+
expect(result).toBeInstanceOf(Map);
337+
338+
const falseResult = result.get(falseObject);
339+
expect(falseResult).toEqual(["foo", undefined, undefined]);
340+
341+
const trueResult = result.get(trueObject);
342+
expect(trueResult).toEqual(["bar", "baz"]);
343+
});
320344
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
test("length is 1", () => {
2+
expect(Array.prototype.groupByToMap).toHaveLength(1);
3+
});
4+
5+
describe("errors", () => {
6+
test("callback must be a function", () => {
7+
expect(() => {
8+
[].groupByToMap(undefined);
9+
}).toThrowWithMessage(TypeError, "undefined is not a function");
10+
});
11+
12+
test("null or undefined this value", () => {
13+
expect(() => {
14+
Array.prototype.groupByToMap.call();
15+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
16+
17+
expect(() => {
18+
Array.prototype.groupByToMap.call(undefined);
19+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
20+
21+
expect(() => {
22+
Array.prototype.groupByToMap.call(null);
23+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
24+
});
25+
});
26+
27+
describe("normal behavior", () => {
28+
test("basic functionality", () => {
29+
const array = [1, 2, 3, 4, 5, 6];
30+
const visited = [];
31+
const trueObject = { true: true };
32+
const falseObject = { false: false };
33+
34+
const firstResult = array.groupByToMap(value => {
35+
visited.push(value);
36+
return value % 2 === 0 ? trueObject : falseObject;
37+
});
38+
39+
expect(visited).toEqual([1, 2, 3, 4, 5, 6]);
40+
expect(firstResult).toBeInstanceOf(Map);
41+
expect(firstResult.size).toBe(2);
42+
expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
43+
expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
44+
45+
const secondResult = array.groupByToMap((_, index) => {
46+
return index < array.length / 2 ? trueObject : falseObject;
47+
});
48+
49+
expect(secondResult).toBeInstanceOf(Map);
50+
expect(secondResult.size).toBe(2);
51+
expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
52+
expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
53+
54+
const thisArg = [7, 8, 9, 10, 11, 12];
55+
const thirdResult = array.groupByToMap(function (_, __, arrayVisited) {
56+
expect(arrayVisited).toBe(array);
57+
expect(this).toBe(thisArg);
58+
}, thisArg);
59+
60+
expect(thirdResult).toBeInstanceOf(Map);
61+
expect(thirdResult.size).toBe(1);
62+
expect(thirdResult.get(undefined)).not.toBe(array);
63+
expect(thirdResult.get(undefined)).not.toBe(thisArg);
64+
expect(thirdResult.get(undefined)).toEqual(array);
65+
});
66+
67+
test("never calls callback with empty array", () => {
68+
var callbackCalled = 0;
69+
const result = [].groupByToMap(() => {
70+
callbackCalled++;
71+
});
72+
expect(result).toBeInstanceOf(Map);
73+
expect(result.size).toBe(0);
74+
expect(callbackCalled).toBe(0);
75+
});
76+
77+
test("calls callback once for every item", () => {
78+
var callbackCalled = 0;
79+
const result = [1, 2, 3].groupByToMap(() => {
80+
callbackCalled++;
81+
});
82+
expect(result).toBeInstanceOf(Map);
83+
expect(result.size).toBe(1);
84+
expect(result.get(undefined)).toEqual([1, 2, 3]);
85+
expect(callbackCalled).toBe(3);
86+
});
87+
88+
test("still returns a Map even if the global Map constructor was changed", () => {
89+
globalThis.Map = null;
90+
const result = [1, 2].groupByToMap(value => {
91+
return value % 2 === 0;
92+
});
93+
expect(result.size).toBe(2);
94+
expect(result.get(true)).toEqual([2]);
95+
expect(result.get(false)).toEqual([1]);
96+
});
97+
});

0 commit comments

Comments
 (0)