LibJS: Add Array.prototype.every

This commit is contained in:
Luke 2020-05-21 07:11:34 -07:00 committed by Andreas Kling
parent 4cdbdf0a84
commit 57d15acd4c
Notes: sideshowbarker 2024-07-19 06:16:51 +09:00
3 changed files with 83 additions and 0 deletions

View file

@ -62,6 +62,7 @@ ArrayPrototype::ArrayPrototype()
put_native_function("find", find, 1, attr);
put_native_function("findIndex", find_index, 1, attr);
put_native_function("some", some, 1, attr);
put_native_function("every", every, 1, attr);
put("length", Value(0), Attribute::Configurable);
}
@ -560,4 +561,41 @@ Value ArrayPrototype::some(Interpreter& interpreter)
return Value(false);
}
Value ArrayPrototype::every(Interpreter& interpreter)
{
auto* array = array_from(interpreter);
if (!array)
return {};
auto* callback = callback_from_args(interpreter, "every");
if (!callback)
return {};
auto this_value = interpreter.argument(1);
auto array_size = array->elements().size();
for (size_t i = 0; i < array_size; ++i) {
if (i >= array->elements().size())
break;
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(false);
}
return Value(true);
}
}

View file

@ -57,6 +57,7 @@ private:
static Value find(Interpreter&);
static Value find_index(Interpreter&);
static Value some(Interpreter&);
static Value every(Interpreter&);
};
}

View file

@ -0,0 +1,44 @@
load("test-common.js");
try {
assert(Array.prototype.every.length === 1);
assertThrowsError(() => {
[].every(undefined);
}, {
error: TypeError,
message: "undefined is not a function"
});
var arrayOne = ["serenity", { test: "serenity"} ];
var arrayTwo = [true, false, 1, 2, 3, "3"];
assert(arrayOne.every(value => value === "hello") === false);
assert(arrayOne.every(value => value === "serenity") === false);
assert(arrayOne.every((value, index, arr) => index < 2) === true);
assert(arrayOne.every(value => typeof(value) === "string") === false);
assert(arrayOne.every(value => arrayOne.pop()) === true);
assert(arrayTwo.every((value, index, arr) => index > 0) === false);
assert(arrayTwo.every((value, index, arr) => index >= 0) === true);
assert(arrayTwo.every(value => typeof(value) !== "string") === false);
assert(arrayTwo.every(value => typeof(value) === "number") === false);
assert(arrayTwo.every(value => value > 0) === false);
assert(arrayTwo.every(value => value >= 0 && value < 4) === true);
assert(arrayTwo.every(value => arrayTwo.pop()) === true);
assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true);
assert([].every(value => value === 1) === true);
arrayTwo = [true, false, 1, 2, 3, "3"];
// Every only goes up to the original length.
assert(arrayTwo.every((value, index, arr) => {
arr.push("serenity");
return value < 4;
}) === true);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}