mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Add Array.prototype.find
This commit is contained in:
parent
fd5f05079d
commit
b0ca174d49
Notes:
sideshowbarker
2024-07-19 07:20:22 +09:00
Author: https://github.com/kessejones Commit: https://github.com/SerenityOS/serenity/commit/b0ca174d493 Pull-request: https://github.com/SerenityOS/serenity/pull/1937
3 changed files with 65 additions and 0 deletions
|
@ -57,6 +57,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("reverse", reverse, 0);
|
||||
put_native_function("lastIndexOf", last_index_of, 1);
|
||||
put_native_function("includes", includes, 1);
|
||||
put_native_function("find", find, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
|
@ -423,4 +424,38 @@ Value ArrayPrototype::includes(Interpreter& interpreter)
|
|||
return Value(false);
|
||||
}
|
||||
|
||||
Value ArrayPrototype::find(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
|
||||
auto* callback = callback_from_args(interpreter, "find");
|
||||
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;
|
||||
}
|
||||
|
||||
return js_undefined();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -54,5 +54,6 @@ private:
|
|||
static Value reverse(Interpreter&);
|
||||
static Value last_index_of(Interpreter&);
|
||||
static Value includes(Interpreter&);
|
||||
static Value find(Interpreter&);
|
||||
};
|
||||
}
|
||||
|
|
29
Libraries/LibJS/Tests/Array.prototype.find.js
Normal file
29
Libraries/LibJS/Tests/Array.prototype.find.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.find.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].find(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.find(value => value === "hello") === "hello");
|
||||
assert(array.find((value, index, arr) => index === 1) === "friends");
|
||||
assert(array.find(value => value == "1") === 1);
|
||||
assert(array.find(value => value === 1) === 1);
|
||||
assert(array.find(value => typeof(value) !== "string") === 1);
|
||||
assert(array.find(value => typeof(value) === "boolean") === false);
|
||||
assert(array.find(value => value > 1) === 2);
|
||||
assert(array.find(value => value > 1 && value < 3) === 2);
|
||||
assert(array.find(value => value > 100) === undefined);
|
||||
assert([].find(value => value === 1) === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Reference in a new issue