1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:27:35 +00:00

LibJS: Add Array.prototype.find

This commit is contained in:
Kesse Jones 2020-04-23 20:00:27 -03:00 committed by Andreas Kling
parent fd5f05079d
commit b0ca174d49
3 changed files with 65 additions and 0 deletions

View 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);
}