diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 85db06f95d..bca562e9af 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -39,6 +39,7 @@ namespace JS { ArrayPrototype::ArrayPrototype() { + put_native_function("filter", filter, 1); put_native_function("forEach", for_each, 1); put_native_function("pop", pop, 0); put_native_function("push", push, 1); @@ -78,6 +79,32 @@ static Function* callback_from_args(Interpreter& interpreter, const String& name return static_cast(&callback.as_object()); } +Value ArrayPrototype::filter(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + auto* callback = callback_from_args(interpreter, "filter"); + if (!callback) + return {}; + auto this_value = interpreter.argument(1); + auto initial_array_size = array->elements().size(); + auto* new_array = interpreter.heap().allocate(); + for (size_t i = 0; i < initial_array_size; ++i) { + if (i >= array->elements().size()) + break; + auto value = array->elements()[i]; + if (value.is_empty()) + continue; + auto result = interpreter.call(callback, this_value, { value, Value((i32)i), array }); + if (interpreter.exception()) + return {}; + if (result.to_boolean()) + new_array->elements().append(value); + } + return Value(new_array); +} + Value ArrayPrototype::for_each(Interpreter& interpreter) { auto* array = array_from(interpreter); diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index ceb50b32c0..69effbfcd2 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -39,6 +39,7 @@ public: private: virtual const char* class_name() const override { return "ArrayPrototype"; } + static Value filter(Interpreter&); static Value for_each(Interpreter&); static Value pop(Interpreter&); static Value push(Interpreter&); diff --git a/Libraries/LibJS/Tests/Array.prototype.filter.js b/Libraries/LibJS/Tests/Array.prototype.filter.js new file mode 100644 index 0000000000..a5fc677eec --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.filter.js @@ -0,0 +1,61 @@ +load("test-common.js"); + +try { + assert(Array.prototype.filter.length === 1); + + try { + [].filter(); + assertNotReached(); + } catch (e) { + assert(e.name === "TypeError"); + assert(e.message === "Array.prototype.filter() requires at least one argument"); + } + + try { + [].filter(undefined); + assertNotReached(); + } catch (e) { + assert(e.name === "TypeError"); + assert(e.message === "undefined is not a function"); + } + + var callbackCalled = 0; + var callback = () => { callbackCalled++; }; + + assert([].filter(callback).length === 0); + assert(callbackCalled === 0); + + assert([1, 2, 3].filter(callback).length === 0); + assert(callbackCalled === 3); + + var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0); + assert(evenNumbers.length === 4); + assert(evenNumbers[0] === 0); + assert(evenNumbers[1] === 2); + assert(evenNumbers[2] === 4); + assert(evenNumbers[3] === 6); + + var fruits = ["Apple", "Banana", "Blueberry", "Grape", "Mango", "Orange", "Peach", "Pineapple", "Raspberry", "Watermelon"]; + const filterItems = (arr, query) => { + return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1) + }; + + var results; + + results = filterItems(fruits, "Berry"); + assert(results.length === 2); + assert(results[0] === "Blueberry"); + assert(results[1] === "Raspberry"); + + results = filterItems(fruits, "P"); + assert(results.length === 5); + assert(results[0] === "Apple"); + assert(results[1] === "Grape"); + assert(results[2] === "Peach"); + assert(results[3] === "Pineapple"); + assert(results[4] === "Raspberry"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}