mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibJS: Add Array.prototype.findIndex
This commit is contained in:
parent
bf9926743a
commit
36c00e8078
Notes:
sideshowbarker
2024-07-19 07:15:10 +09:00
Author: https://github.com/kessejones Commit: https://github.com/SerenityOS/serenity/commit/36c00e8078c Pull-request: https://github.com/SerenityOS/serenity/pull/1972
3 changed files with 65 additions and 0 deletions
|
@ -58,6 +58,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("lastIndexOf", last_index_of, 1);
|
||||
put_native_function("includes", includes, 1);
|
||||
put_native_function("find", find, 1);
|
||||
put_native_function("findIndex", find_index, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
|
@ -458,4 +459,38 @@ Value ArrayPrototype::find(Interpreter& interpreter)
|
|||
return js_undefined();
|
||||
}
|
||||
|
||||
Value ArrayPrototype::find_index(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
|
||||
auto* callback = callback_from_args(interpreter, "findIndex");
|
||||
if (!callback)
|
||||
return {};
|
||||
|
||||
auto this_value = interpreter.argument(1);
|
||||
auto array_size = array->elements().size();
|
||||
|
||||
for (size_t i = 0; i < array_size; ++i) {
|
||||
auto value = array->elements().at(i);
|
||||
if (value.is_empty())
|
||||
continue;
|
||||
|
||||
MarkedValueList arguments(interpreter.heap());
|
||||
arguments.append(value);
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
if (result.to_boolean())
|
||||
return Value((i32)i);
|
||||
}
|
||||
|
||||
return Value(-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -55,5 +55,6 @@ private:
|
|||
static Value last_index_of(Interpreter&);
|
||||
static Value includes(Interpreter&);
|
||||
static Value find(Interpreter&);
|
||||
static Value find_index(Interpreter&);
|
||||
};
|
||||
}
|
||||
|
|
29
Libraries/LibJS/Tests/Array.prototype.findIndex.js
Normal file
29
Libraries/LibJS/Tests/Array.prototype.findIndex.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.findIndex.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].findIndex(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.findIndex(value => value === "hello") === 0);
|
||||
assert(array.findIndex((value, index, arr) => index === 1) === 1);
|
||||
assert(array.findIndex(value => value == "1") === 2);
|
||||
assert(array.findIndex(value => value === 1) === 2);
|
||||
assert(array.findIndex(value => typeof(value) !== "string") === 2);
|
||||
assert(array.findIndex(value => typeof(value) === "boolean") === 4);
|
||||
assert(array.findIndex(value => value > 1) === 3);
|
||||
assert(array.findIndex(value => value > 1 && value < 3) === 3);
|
||||
assert(array.findIndex(value => value > 100) === -1);
|
||||
assert([].findIndex(value => value === 1) === -1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Reference in a new issue