Skip to content

⬅️ Back to Table of Contents

📄 useArrayFindLast

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 4
🟢 Vue Composition API 1
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/useArrayFindLast/index.ts

📦 Imports

Name Source
ComputedRef vue
MaybeRefOrGetter vue
computed vue
toValue vue

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none

Functions

useArrayFindLast(list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>, fn: (element: T, index: number, array: Mayb…): UseArrayFindLastReturn<T>

Reactive Array.findLast

Parameters:

  • list any: the array was called upon.
  • fn any: a function to test each element.

Returns: undefined the last element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

See: https://vueuse.org/useArrayFindLast

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Reactive `Array.findLast`
 *
 * @see https://vueuse.org/useArrayFindLast
 * @param list - the array was called upon.
 * @param fn - a function to test each element.
 *
 * @returns the last element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • computed (from vue)
  • toValue (from vue)
  • findLast
  • fn
  • toValue(list) .findLast
Code
export function useArrayFindLast<T>(
  list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
  fn: (element: T, index: number, array: MaybeRefOrGetter<T>[]) => boolean,
): UseArrayFindLastReturn<T> {
  return computed(() =>
    toValue<T | undefined>(
      !Array.prototype.findLast
        ? findLast(toValue(list), (element, index, array) => fn(toValue(element), index, array))
        : toValue(list)
            .findLast((element, index, array) => fn(toValue(element), index, array)),
    ))
}

findLast(arr: T[], cb: (element: T, index: number, array: T[])…): T | undefined

Parameters:

  • arr T[]
  • cb (element: T, index: number, array: T[]) => boolean

Returns: T | undefined

Calls:

  • cb
Code
function findLast<T>(arr: T[], cb: (element: T, index: number, array: T[]) => boolean): T | undefined {
  let index = arr.length
  while (index-- > 0) {
    if (cb(arr[index], index, arr))
      return arr[index]
  }
  return undefined
}

Type Aliases

UseArrayFindLastReturn<T = any>

type UseArrayFindLastReturn<T = any> = ComputedRef<T | undefined>;

Generated by Syntax Scribe