mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibJS: Add Array.prototype.filter()
This commit is contained in:
parent
866172a721
commit
f03d005bc4
Notes:
sideshowbarker
2024-07-19 07:36:21 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/f03d005bc4f Pull-request: https://github.com/SerenityOS/serenity/pull/1793
3 changed files with 89 additions and 0 deletions
|
@ -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<Function*>(&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<Array>();
|
||||
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);
|
||||
|
|
|
@ -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&);
|
||||
|
|
61
Libraries/LibJS/Tests/Array.prototype.filter.js
Normal file
61
Libraries/LibJS/Tests/Array.prototype.filter.js
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Reference in a new issue