Skip to content

Instantly share code, notes, and snippets.

Last active December 14, 2015 11:29
Show Gist options
  • Save rwaldron/5079427 to your computer and use it in GitHub Desktop.
Save rwaldron/5079427 to your computer and use it in GitHub Desktop.
ES6 Array.prototype.findIndex spec

15.4.4.x Array.prototype.findIndex ( predicate [ , thisArg ] )

predicate should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. findIndex calls predicate once for each element present in the array, in ascending order, until predicate returns true and immediately returns the index of the current array element. Otherwise, findIndex return -1. predicate is called only for elements of the array which actually exist; it is not called for missing elements of the array.

  1. Let O be the result of ToObject passing the this value as the argument.
  2. ReturnIfAbrupt( O ).
  3. Let lenValue be the result of Get( O, "length" ).
  4. Let len be ToUint32( lenValue ).
  5. ReturnIfAbrupt( len ).
  6. If len is 0, return -1.
  7. If IsCallable( predicate ) is false, throw a TypeError exception.
  8. If thisArg was supplied, let T be thisArg; else let T be undefined
  9. Let k be 0.
  10. Repeat, while k < len
    • a. Let Pk be ToString( k )
    • b. Let kPresent be the result of HasProperty( O, Pk ).
    • c. ReturnIfAbrupt( kPresent )
    • d. If kPresent is true, then
      • i. Let kValue be the result of calling Get( O, Pk ).
      • ii. ReturnIfAbrupt( kValue ).
      • iii. Let result be the result of calling the [[Call]] internal method of predicate with T as thisArgument and a List containing kValue, k, and O as argumentsList.
      • iv. ReturnIfAbrupt( result ).
      • v. If ToBoolean( result ) is true, return k.
    • e. Increase k by 1.
  11. Return -1.

The length property of the findIndex method is 1.

NOTE The findIndex function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the findIndex function can be applied successfully to a host object is implementation-dependent

Copy link

Copy link

Copy link

ES6-shim has this now paulmillr/es6-shim@197a9a0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment