mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:27:35 +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&);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue