diff options
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 35 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.h | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.findIndex.js | 29 |
3 files changed, 65 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 674d500fa3..f208973621 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -58,6 +58,7 @@ ArrayPrototype::ArrayPrototype() put_native_function("lastIndexOf", last_index_of, 1); put_native_function("includes", includes, 1); put_native_function("find", find, 1); + put_native_function("findIndex", find_index, 1); put("length", Value(0)); } @@ -458,4 +459,38 @@ Value ArrayPrototype::find(Interpreter& interpreter) return js_undefined(); } +Value ArrayPrototype::find_index(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + + auto* callback = callback_from_args(interpreter, "findIndex"); + if (!callback) + return {}; + + auto this_value = interpreter.argument(1); + auto array_size = array->elements().size(); + + for (size_t i = 0; i < array_size; ++i) { + 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((i32)i); + } + + return Value(-1); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index b5912ee96f..30f4c6f000 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -55,5 +55,6 @@ private: static Value last_index_of(Interpreter&); static Value includes(Interpreter&); static Value find(Interpreter&); + static Value find_index(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Array.prototype.findIndex.js b/Libraries/LibJS/Tests/Array.prototype.findIndex.js new file mode 100644 index 0000000000..0795a66474 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.findIndex.js @@ -0,0 +1,29 @@ +load("test-common.js"); + +try { + assert(Array.prototype.findIndex.length === 1); + + assertThrowsError(() => { + [].findIndex(undefined); + }, { + error: TypeError, + message: "undefined is not a function" + }); + + var array = ["hello", "friends", 1, 2, false]; + + assert(array.findIndex(value => value === "hello") === 0); + assert(array.findIndex((value, index, arr) => index === 1) === 1); + assert(array.findIndex(value => value == "1") === 2); + assert(array.findIndex(value => value === 1) === 2); + assert(array.findIndex(value => typeof(value) !== "string") === 2); + assert(array.findIndex(value => typeof(value) === "boolean") === 4); + assert(array.findIndex(value => value > 1) === 3); + assert(array.findIndex(value => value > 1 && value < 3) === 3); + assert(array.findIndex(value => value > 100) === -1); + assert([].findIndex(value => value === 1) === -1); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |