mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibJS: Add Array.prototype.includes
This commit is contained in:
parent
a639172760
commit
687096cadd
Notes:
sideshowbarker
2024-07-19 07:21:50 +09:00
Author: https://github.com/kessejones Commit: https://github.com/SerenityOS/serenity/commit/687096cadd0 Pull-request: https://github.com/SerenityOS/serenity/pull/1928 Reviewed-by: https://github.com/awesomekling
3 changed files with 56 additions and 0 deletions
|
@ -56,6 +56,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("indexOf", index_of, 1);
|
||||
put_native_function("reverse", reverse, 0);
|
||||
put_native_function("lastIndexOf", last_index_of, 1);
|
||||
put_native_function("includes", includes, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
|
@ -388,4 +389,38 @@ Value ArrayPrototype::last_index_of(Interpreter& interpreter)
|
|||
return Value(-1);
|
||||
}
|
||||
|
||||
Value ArrayPrototype::includes(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
|
||||
i32 array_size = static_cast<i32>(array->elements().size());
|
||||
if (interpreter.argument_count() == 0 || array_size == 0)
|
||||
return Value(false);
|
||||
|
||||
i32 from_index = 0;
|
||||
if (interpreter.argument_count() >= 2) {
|
||||
from_index = interpreter.argument(1).to_i32();
|
||||
|
||||
if (from_index >= array_size)
|
||||
return Value(false);
|
||||
|
||||
auto negative_min_index = ((array_size - 1) * -1);
|
||||
if (from_index < negative_min_index)
|
||||
from_index = 0;
|
||||
else if (from_index < 0)
|
||||
from_index = array_size + from_index;
|
||||
}
|
||||
|
||||
auto value_to_find = interpreter.argument(0);
|
||||
for (i32 i = from_index; i < array_size; ++i) {
|
||||
auto& element = array->elements().at(i);
|
||||
if (typed_eq(interpreter, element, value_to_find).as_bool())
|
||||
return Value(true);
|
||||
}
|
||||
|
||||
return Value(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,5 +53,6 @@ private:
|
|||
static Value index_of(Interpreter&);
|
||||
static Value reverse(Interpreter&);
|
||||
static Value last_index_of(Interpreter&);
|
||||
static Value includes(Interpreter&);
|
||||
};
|
||||
}
|
||||
|
|
20
Libraries/LibJS/Tests/Array.prototype.includes.js
Normal file
20
Libraries/LibJS/Tests/Array.prototype.includes.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.includes.length === 1);
|
||||
|
||||
var array = ['hello', 'friends', 1, 2, false];
|
||||
|
||||
assert(array.includes('hello') === true);
|
||||
assert(array.includes(1) === true);
|
||||
assert(array.includes(1, -3) === true);
|
||||
assert(array.includes('serenity') === false);
|
||||
assert(array.includes(false, -1) === true);
|
||||
assert(array.includes(2, -1) === false);
|
||||
assert(array.includes(2, -100) === true);
|
||||
assert(array.includes('friends', 100) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Reference in a new issue