mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:37:34 +00:00
LibJS: Add Array.prototype.lastIndexOf
This commit is contained in:
parent
09138d542d
commit
b2305cb67d
3 changed files with 58 additions and 0 deletions
|
@ -55,6 +55,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("slice", slice, 2);
|
||||
put_native_function("indexOf", index_of, 1);
|
||||
put_native_function("reverse", reverse, 0);
|
||||
put_native_function("lastIndexOf", last_index_of, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
|
@ -365,4 +366,38 @@ Value ArrayPrototype::reverse(Interpreter& interpreter)
|
|||
return array;
|
||||
}
|
||||
|
||||
Value ArrayPrototype::last_index_of(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(-1);
|
||||
|
||||
i32 from_index = 0;
|
||||
if (interpreter.argument_count() >= 2) {
|
||||
from_index = interpreter.argument(1).to_number().to_i32();
|
||||
|
||||
if (from_index >= array_size)
|
||||
return Value(-1);
|
||||
|
||||
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 search_element = interpreter.argument(0);
|
||||
for (i32 i = array_size - 1; i >= from_index; --i) {
|
||||
auto& element = array->elements().at(i);
|
||||
if (typed_eq(interpreter, element, search_element).as_bool())
|
||||
return Value(i);
|
||||
}
|
||||
|
||||
return Value(-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -52,5 +52,6 @@ private:
|
|||
static Value slice(Interpreter&);
|
||||
static Value index_of(Interpreter&);
|
||||
static Value reverse(Interpreter&);
|
||||
static Value last_index_of(Interpreter&);
|
||||
};
|
||||
}
|
||||
|
|
22
Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js
Normal file
22
Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.lastIndexOf.length === 1);
|
||||
|
||||
var array = [1, 2, 3, 1, "hello"];
|
||||
|
||||
assert(array.lastIndexOf("hello") === 4);
|
||||
assert(array.lastIndexOf(1) === 3);
|
||||
assert(array.lastIndexOf(1, -1) === -1);
|
||||
assert(array.lastIndexOf(1, -2) === 3);
|
||||
assert(array.lastIndexOf(2) === 1);
|
||||
assert(array.lastIndexOf(2, -3) === -1);
|
||||
assert(array.lastIndexOf(2, -4) === 1);
|
||||
assert([].lastIndexOf('hello') === -1);
|
||||
assert([].lastIndexOf('hello', 10) === -1);
|
||||
assert([].lastIndexOf('hello', -10) === -1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue