1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-09-17 19:36:17 +00:00

LibJS: Refactor Array.prototype callback functions and make them generic

This commit is contained in:
Linus Groh 2020-05-21 21:24:43 +01:00 committed by Andreas Kling
parent 5db9becc4a
commit a4d04cc748
2 changed files with 146 additions and 206 deletions

View file

@ -0,0 +1,47 @@
load("test-common.js");
try {
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
["every"].forEach(name => {
const visited = [];
Array.prototype[name].call(o, function (value) {
visited.push(value);
return true;
});
assert(visited.length === 3);
assert(visited[0] === "foo");
assert(visited[1] === "bar");
assert(visited[2] === "baz");
});
["find", "findIndex"].forEach(name => {
const visited = [];
Array.prototype[name].call(o, function (value) {
visited.push(value);
return false;
});
assert(visited.length === 5);
assert(visited[0] === "foo");
assert(visited[1] === "bar");
assert(visited[2] === undefined);
assert(visited[3] === "baz");
assert(visited[4] === undefined);
});
["filter", "forEach", "map", "some"].forEach(name => {
const visited = [];
Array.prototype[name].call(o, function (value) {
visited.push(value);
return false;
});
assert(visited.length === 3);
assert(visited[0] === "foo");
assert(visited[1] === "bar");
assert(visited[2] === "baz");
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}