ソースを参照

LibJS: Implement Array.prototype.toSorted()

Linus Groh 3 年 前
コミット
ce17c868c0

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

@@ -76,6 +76,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.group, group, 1, attr);
     define_native_function(vm.names.groupToMap, group_to_map, 1, attr);
     define_native_function(vm.names.toReversed, to_reversed, 0, attr);
+    define_native_function(vm.names.toSorted, to_sorted, 1, attr);
 
     // Use define_direct_property here instead of define_native_function so that
     // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
@@ -103,6 +104,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
     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.toReversed, Value(true)));
+    MUST(unscopable_list->create_data_property_or_throw(vm.names.toSorted, Value(true)));
     MUST(unscopable_list->create_data_property_or_throw(vm.names.values, Value(true)));
 
     define_direct_property(*vm.well_known_symbol_unscopables(), unscopable_list, Attribute::Configurable);
@@ -1818,4 +1820,44 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_reversed)
     return array;
 }
 
+// 1.1.1.5 Array.prototype.toSorted ( comparefn ), https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.toSorted
+JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_sorted)
+{
+    auto comparefn = vm.argument(0);
+
+    // 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+    if (!comparefn.is_undefined() && !comparefn.is_function())
+        return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, comparefn);
+
+    // 2. Let O be ? ToObject(this value).
+    auto* object = TRY(vm.this_value(global_object).to_object(global_object));
+
+    // 3. Let len be ? LengthOfArrayLike(O).
+    auto length = TRY(length_of_array_like(global_object, *object));
+
+    // 4. Let A be ? ArrayCreate(𝔽(len)).
+    auto* array = TRY(Array::create(global_object, length));
+
+    // 5. Let SortCompare be a new Abstract Closure with parameters (x, y) that captures comparefn and performs the following steps when called:
+    Function<ThrowCompletionOr<double>(Value, Value)> sort_compare = [&](auto x, auto y) -> ThrowCompletionOr<double> {
+        // a. Return ? CompareArrayElements(x, y, comparefn).
+        return TRY(compare_array_elements(global_object, x, y, comparefn.is_undefined() ? nullptr : &comparefn.as_function()));
+    };
+
+    // 6. Let sortedList be ? SortIndexedProperties(obj, len, SortCompare, false).
+    auto sorted_list = TRY(sort_indexed_properties(global_object, *object, length, sort_compare, false));
+
+    // 7. Let j be 0.
+    // 8. Repeat, while j < len,
+    for (size_t j = 0; j < length; ++j) {
+        // a. Perform ! CreateDataPropertyOrThrow(A, ! ToString(𝔽(j)), sortedList[j]).
+        MUST(array->create_data_property_or_throw(j, sorted_list[j]));
+
+        // b. Set j to j + 1.
+    }
+
+    // 9. Return A.
+    return array;
+}
+
 }

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

@@ -57,6 +57,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(group);
     JS_DECLARE_NATIVE_FUNCTION(group_to_map);
     JS_DECLARE_NATIVE_FUNCTION(to_reversed);
+    JS_DECLARE_NATIVE_FUNCTION(to_sorted);
 };
 
 ThrowCompletionOr<void> array_merge_sort(GlobalObject&, FunctionObject* compare_func, MarkedVector<Value>& arr_to_sort);

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

@@ -486,6 +486,7 @@ namespace JS {
     P(toPlainYearMonth)                      \
     P(toPrecision)                           \
     P(toReversed)                            \
+    P(toSorted)                              \
     P(toString)                              \
     P(total)                                 \
     P(toTemporalInstant)                     \

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

@@ -348,4 +348,10 @@ describe("ability to work with generic non-array objects", () => {
         expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
         expect(result).not.toBe(o);
     });
+
+    test("toSorted", () => {
+        const result = Array.prototype.toSorted.call(o);
+        expect(result).toEqual(["bar", "baz", "foo", undefined, undefined]);
+        expect(result).not.toBe(o);
+    });
 });

+ 53 - 0
Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toSorted.js

@@ -0,0 +1,53 @@
+describe("normal behavior", () => {
+    test("length is 1", () => {
+        expect(Array.prototype.toSorted).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const a = [2, 4, 1, 3, 5];
+        const b = a.toSorted();
+        expect(a).not.toBe(b);
+        expect(a).toEqual([2, 4, 1, 3, 5]);
+        expect(b).toEqual([1, 2, 3, 4, 5]);
+    });
+
+    test("custom compare function", () => {
+        const a = [2, 4, 1, 3, 5];
+        const b = a.toSorted(() => 0);
+        expect(a).not.toBe(b);
+        expect(a).toEqual([2, 4, 1, 3, 5]);
+        expect(b).toEqual([2, 4, 1, 3, 5]);
+    });
+
+    test("is unscopable", () => {
+        expect(Array.prototype[Symbol.unscopables].toSorted).toBeTrue();
+        const array = [];
+        with (array) {
+            expect(() => {
+                toSorted;
+            }).toThrowWithMessage(ReferenceError, "'toSorted' is not defined");
+        }
+    });
+});
+
+describe("errors", () => {
+    test("null or undefined this value", () => {
+        expect(() => {
+            Array.prototype.toSorted.call();
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+        expect(() => {
+            Array.prototype.toSorted.call(undefined);
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+        expect(() => {
+            Array.prototype.toSorted.call(null);
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+    });
+
+    test("invalid compare function", () => {
+        expect(() => {
+            [].toSorted("foo");
+        }).toThrowWithMessage(TypeError, "foo is not a function");
+    });
+});