Przeglądaj źródła

LibJS: Implement Array.prototype.groupByToMap

Luke Wilde 3 lat temu
rodzic
commit
0b9ea712be

+ 74 - 0
Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -19,6 +19,7 @@
 #include <LibJS/Runtime/Error.h>
 #include <LibJS/Runtime/FunctionObject.h>
 #include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Map.h>
 #include <LibJS/Runtime/ObjectPrototype.h>
 #include <LibJS/Runtime/Realm.h>
 #include <LibJS/Runtime/Value.h>
@@ -73,6 +74,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.entries, entries, 0, attr);
     define_native_function(vm.names.copyWithin, copy_within, 2, attr);
     define_native_function(vm.names.groupBy, group_by, 1, attr);
+    define_native_function(vm.names.groupByToMap, group_by_to_map, 1, attr);
 
     // Use define_direct_property here instead of define_native_function so that
     // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
@@ -95,6 +97,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
     MUST(unscopable_list->create_data_property_or_throw(vm.names.flat, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.flatMap, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.groupBy, Value(true)));
+    MUST(unscopable_list->create_data_property_or_throw(vm.names.groupByToMap, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.includes, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.keys, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.values, Value(true)));
@@ -1750,4 +1753,75 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by)
     return object;
 }
 
+// 2.2 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
+JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_by_to_map)
+{
+    auto callback_function = vm.argument(0);
+    auto this_arg = vm.argument(1);
+
+    // 1. Let O be ? ToObject(this value).
+    auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
+
+    // 2. Let len be ? LengthOfArrayLike(O).
+    auto length = TRY(length_of_array_like(global_object, *this_object));
+
+    // 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
+    if (!callback_function.is_function())
+        return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback_function.to_string_without_side_effects());
+
+    struct KeyedGroupTraits : public Traits<Handle<Value>> {
+        static unsigned hash(Handle<Value> const& value_handle)
+        {
+            return ValueTraits::hash(value_handle.value());
+        }
+
+        static bool equals(Handle<Value> const& a, Handle<Value> const& b)
+        {
+            // AddValueToKeyedGroup uses SameValue on the keys on Step 1.a.
+            return same_value(a.value(), b.value());
+        }
+    };
+
+    // 5. Let groups be a new empty List.
+    OrderedHashMap<Handle<Value>, MarkedValueList, KeyedGroupTraits> groups;
+
+    // 4. Let k be 0.
+    // 6. Repeat, while k < len
+    for (size_t index = 0; index < length; ++index) {
+        // a. Let Pk be ! ToString(𝔽(k)).
+        auto index_property = PropertyKey { index };
+
+        // b. Let kValue be ? Get(O, Pk).
+        auto k_value = TRY(this_object->get(index_property));
+
+        // c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
+        auto key = TRY(vm.call(callback_function.as_function(), this_arg, k_value, Value(index), this_object));
+
+        // d. If key is -0𝔽, set key to +0𝔽.
+        if (key.is_negative_zero())
+            key = Value(0);
+
+        // e. Perform ! AddValueToKeyedGroup(groups, key, kValue).
+        add_value_to_keyed_group(global_object, groups, make_handle(key), k_value);
+
+        // f. Set k to k + 1.
+    }
+
+    // 7. Let map be ! Construct(%Map%).
+    auto* map = Map::create(global_object);
+
+    // 8. For each Record { [[Key]], [[Elements]] } g of groups, do
+    for (auto& group : groups) {
+        // a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
+        auto* elements = Array::create_from(global_object, group.value);
+
+        // b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
+        // c. Append entry as the last element of map.[[MapData]].
+        map->entries().set(group.key.value(), elements);
+    }
+
+    // 9. Return map.
+    return map;
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/ArrayPrototype.h

@@ -55,6 +55,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(entries);
     JS_DECLARE_NATIVE_FUNCTION(copy_within);
     JS_DECLARE_NATIVE_FUNCTION(group_by);
+    JS_DECLARE_NATIVE_FUNCTION(group_by_to_map);
 };
 
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -242,6 +242,7 @@ namespace JS {
     P(globalThis)                            \
     P(group)                                 \
     P(groupBy)                               \
+    P(groupByToMap)                          \
     P(groupCollapsed)                        \
     P(groupEnd)                              \
     P(groups)                                \

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

@@ -317,4 +317,28 @@ describe("ability to work with generic non-array objects", () => {
         expect(result.false).toEqual(["foo", undefined, undefined]);
         expect(result.true).toEqual(["bar", "baz"]);
     });
+
+    test("groupByToMap", () => {
+        const visited = [];
+        const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
+        const falseObject = { false: false };
+        const trueObject = { true: true };
+        const result = Array.prototype.groupByToMap.call(o, (value, _, object) => {
+            expect(object).toBe(o);
+            visited.push(value);
+            return value !== undefined
+                ? value.startsWith("b")
+                    ? trueObject
+                    : falseObject
+                : falseObject;
+        });
+        expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
+        expect(result).toBeInstanceOf(Map);
+
+        const falseResult = result.get(falseObject);
+        expect(falseResult).toEqual(["foo", undefined, undefined]);
+
+        const trueResult = result.get(trueObject);
+        expect(trueResult).toEqual(["bar", "baz"]);
+    });
 });

+ 107 - 0
Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.groupByToMap.js

@@ -0,0 +1,107 @@
+test("length is 1", () => {
+    expect(Array.prototype.groupByToMap).toHaveLength(1);
+});
+
+describe("errors", () => {
+    test("callback must be a function", () => {
+        expect(() => {
+            [].groupByToMap(undefined);
+        }).toThrowWithMessage(TypeError, "undefined is not a function");
+    });
+
+    test("null or undefined this value", () => {
+        expect(() => {
+            Array.prototype.groupByToMap.call();
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+        expect(() => {
+            Array.prototype.groupByToMap.call(undefined);
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+        expect(() => {
+            Array.prototype.groupByToMap.call(null);
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+    });
+});
+
+describe("normal behavior", () => {
+    test("basic functionality", () => {
+        const array = [1, 2, 3, 4, 5, 6];
+        const visited = [];
+        const trueObject = { true: true };
+        const falseObject = { false: false };
+
+        const firstResult = array.groupByToMap(value => {
+            visited.push(value);
+            return value % 2 === 0 ? trueObject : falseObject;
+        });
+
+        expect(visited).toEqual([1, 2, 3, 4, 5, 6]);
+        expect(firstResult).toBeInstanceOf(Map);
+        expect(firstResult.size).toBe(2);
+        expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
+        expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
+
+        const secondResult = array.groupByToMap((_, index) => {
+            return index < array.length / 2 ? trueObject : falseObject;
+        });
+
+        expect(secondResult).toBeInstanceOf(Map);
+        expect(secondResult.size).toBe(2);
+        expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
+        expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
+
+        const thisArg = [7, 8, 9, 10, 11, 12];
+        const thirdResult = array.groupByToMap(function (_, __, arrayVisited) {
+            expect(arrayVisited).toBe(array);
+            expect(this).toBe(thisArg);
+        }, thisArg);
+
+        expect(thirdResult).toBeInstanceOf(Map);
+        expect(thirdResult.size).toBe(1);
+        expect(thirdResult.get(undefined)).not.toBe(array);
+        expect(thirdResult.get(undefined)).not.toBe(thisArg);
+        expect(thirdResult.get(undefined)).toEqual(array);
+    });
+
+    test("is unscopable", () => {
+        expect(Array.prototype[Symbol.unscopables].groupByToMap).toBeTrue();
+        const array = [];
+        with (array) {
+            expect(() => {
+                groupByToMap;
+            }).toThrowWithMessage(ReferenceError, "'groupByToMap' is not defined");
+        }
+    });
+
+    test("never calls callback with empty array", () => {
+        var callbackCalled = 0;
+        const result = [].groupByToMap(() => {
+            callbackCalled++;
+        });
+        expect(result).toBeInstanceOf(Map);
+        expect(result.size).toBe(0);
+        expect(callbackCalled).toBe(0);
+    });
+
+    test("calls callback once for every item", () => {
+        var callbackCalled = 0;
+        const result = [1, 2, 3].groupByToMap(() => {
+            callbackCalled++;
+        });
+        expect(result).toBeInstanceOf(Map);
+        expect(result.size).toBe(1);
+        expect(result.get(undefined)).toEqual([1, 2, 3]);
+        expect(callbackCalled).toBe(3);
+    });
+
+    test("still returns a Map even if the global Map constructor was changed", () => {
+        globalThis.Map = null;
+        const result = [1, 2].groupByToMap(value => {
+            return value % 2 === 0;
+        });
+        expect(result.size).toBe(2);
+        expect(result.get(true)).toEqual([2]);
+        expect(result.get(false)).toEqual([1]);
+    });
+});