Skip to content

Conversation

@AshGreyG
Copy link
Owner

@AshGreyG AshGreyG commented Jul 1, 2025

📝 Description

This PR adds a new method Array.LastIndexOf of namespace Array, this method is similar to Array.prototype.lastIndexOf, when the array doesn't contain the target element, this method will return -1.

🌴 PR Type

  • ✨ Feature
  • 🐛 Bugfix
  • 🚑 Hotfix
  • 💡 Doc comment
  • 🧪 Test
  • 📝 Document
  • 🐋 Other (please describe)

🔥 Linked issues

👾 Additional context

⛳ Change log

  • I have updated the changelog/next.md with my changes.

Summary by Sourcery

Add a new type-level LastIndexOf method to the Array namespace and integrate corresponding tests and script updates

New Features:

  • Add Array.LastIndexOf type utility to compute the last occurrence index of an element in a tuple, returning -1 if not found

Build:

  • Update lib-gen.sh script to include test cases for Array.LastIndexOf

Tests:

  • Add type-level tests for Array.LastIndexOf covering empty arrays, missing elements, and nested tuples

@sourcery-ai
Copy link

sourcery-ai bot commented Jul 1, 2025

Reviewer's Guide

This PR introduces a new type-level method Array.LastIndexOf in the Array namespace, implemented via a recursive conditional type that scans an array from the end to locate the last occurrence of a target element, and includes corresponding unit tests and script updates.

Class diagram for the new Array.LastIndexOf type

classDiagram
  class Array {
    <<namespace>>
  }
  class LastIndexOf {
    <<type>>
    +Arr: unknown[]
    +T: unknown
    +Count: 0[]
    +returns: number
  }
  Array <|-- LastIndexOf : type
  class IndexOf {
    <<type>>
  }
  Array <|-- IndexOf : type
  class CreateArrayFromLength {
    <<type>>
  }
  class Integer {
    <<namespace>>
  }
  class Dec {
    <<type>>
  }
  Integer <|-- Dec : type
  LastIndexOf ..> CreateArrayFromLength : uses
  LastIndexOf ..> Integer.Dec : uses
  LastIndexOf ..> IndexOf : similar pattern
Loading

File-Level Changes

Change Details Files
Add new LastIndexOf type definition and implementation
  • Added JSDoc comments and export type LastIndexOf signature
  • Implemented recursion using Rest, Count (via CreateArrayFromLength), and Integer.Dec to compute the last index
  • Defaulted Count to the array length for index tracking
lib/Array/index.d.ts
Extend unit tests for LastIndexOf
  • Added Expect<Equal<Array.LastIndexOf<...>, ...>> cases covering empty arrays, no matches, nested arrays, and multiple occurrences
test/lib-Array.test.ts
Update test generation script with LastIndexOf cases
  • Appended Array.LastIndexOf entry to the test_cases mapping in lib-gen.sh
test/script/lib-gen.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@AshGreyG AshGreyG changed the title ✨ feat(array-methods) add new Array.LastIndexOf method of Array namespace ✨ feat(array-methods): add new Array.LastIndexOf method of Array namespace Jul 1, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @AshGreyG - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `test/lib-Array.test.ts:93` </location>
<code_context>
   Expect<Equal<Array.At<[], 0>, never>>,

+  // Array.LastIndexOf
+  Expect<Equal<Array.LastIndexOf<[1,2,1,2,1,3,4], 1>, 4>>,
+  Expect<Equal<Array.LastIndexOf<[], 1>, -1>>,
+  Expect<Equal<Array.LastIndexOf<[1,2,3], 0>, -1>>,
+  Expect<Equal<Array.LastIndexOf<[[],[[]],[],[]], [[]]>, 1>>,
+
 ]
</code_context>

<issue_to_address>
Missing tests for edge cases with complex types and repeated non-primitive values.

Add tests for arrays with objects, symbols, functions, and cases like different object references or NaN to ensure correct LastIndexOf behavior with complex types and tricky equality scenarios.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +93 to +96
Expect<Equal<Array.LastIndexOf<[1,2,1,2,1,3,4], 1>, 4>>,
Expect<Equal<Array.LastIndexOf<[], 1>, -1>>,
Expect<Equal<Array.LastIndexOf<[1,2,3], 0>, -1>>,
Expect<Equal<Array.LastIndexOf<[[],[[]],[],[]], [[]]>, 1>>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Missing tests for edge cases with complex types and repeated non-primitive values.

Add tests for arrays with objects, symbols, functions, and cases like different object references or NaN to ensure correct LastIndexOf behavior with complex types and tricky equality scenarios.

@AshGreyG AshGreyG merged commit 0839760 into main Jul 1, 2025
2 checks passed
@AshGreyG AshGreyG added enhancement New feature or request feature Add new features to the project labels Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature Add new features to the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants