mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 15:47:45 +00:00
LibJS: Add String.prototype.indexOf position argument
This commit is contained in:
parent
a4c1666b71
commit
36668893a6
2 changed files with 23 additions and 1 deletions
|
@ -284,7 +284,14 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
|
|||
auto needle = vm.argument(0).to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return Value((i32)string->find(needle).value_or(-1));
|
||||
size_t from = 0;
|
||||
if (vm.argument_count() > 1) {
|
||||
double from_argument = vm.argument(1).to_integer_or_infinity(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
from = clamp(from_argument, static_cast<double>(0), static_cast<double>(string->length()));
|
||||
}
|
||||
return Value((i32)string->find(needle, from).value_or(-1));
|
||||
}
|
||||
|
||||
// 22.1.3.26 String.prototype.toLowerCase ( ), https://tc39.es/ecma262/#sec-string.prototype.tolowercase
|
||||
|
|
|
@ -5,4 +5,19 @@ test("basic functionality", () => {
|
|||
|
||||
expect(s.indexOf("friends")).toBe(6);
|
||||
expect(s.indexOf("enemies")).toBe(-1);
|
||||
|
||||
expect(s.indexOf("friends", 0)).toBe(6);
|
||||
expect(s.indexOf("enemies", 0)).toBe(-1);
|
||||
|
||||
expect(s.indexOf("friends", 4)).toBe(6);
|
||||
expect(s.indexOf("friends", 6)).toBe(6);
|
||||
expect(s.indexOf("friends", 7)).toBe(-1);
|
||||
expect(s.indexOf("friends", 8)).toBe(-1);
|
||||
|
||||
expect(s.indexOf("enemies", 2)).toBe(-1);
|
||||
expect(s.indexOf("enemies", 7)).toBe(-1);
|
||||
|
||||
expect(s.indexOf("e")).toBe(1);
|
||||
expect(s.indexOf("e", 0)).toBe(1);
|
||||
expect(s.indexOf("e", 2)).toBe(9);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue