Skip to content

gpadvs-search-exact-match.js: Added snippet to enable exact match functionality with GP Advanced Select. #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2986058780/85959

💬 Slack: https://gravitywiz.slack.com/archives/GMP0ZMNSE/p1751379142647819

Summary

By default, Advanced Select will return any item whose label contains the search query. This snippet will change the search algorithm to only return items whose label matches the search query exactly.

TomSelect score docs here.

Instruction Video:
https://www.loom.com/share/4266734e5ab14870ba6b8bba28d01f68

Copy link

coderabbitai bot commented Jul 3, 2025

Walkthrough

A new JavaScript snippet was added to modify the search behavior of the Gravity Forms Advanced Select field. The code overrides the default scoring function to only return items whose labels exactly match the search query, using a case-insensitive comparison.

Changes

File(s) Change Summary
gp-advanced-select/gpadvs-search-exact-match.js Added script to override the search scoring function to enforce exact, case-insensitive matches.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AdvancedSelect
    participant CustomScoreFunction

    User->>AdvancedSelect: Enters search query
    AdvancedSelect->>CustomScoreFunction: Passes query and item label
    CustomScoreFunction-->>AdvancedSelect: Returns 1 if exact match, else 0
    AdvancedSelect-->>User: Displays only exact match results
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jul 3, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against e773f09

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 24c21fd and e773f09.

📒 Files selected for processing (1)
  • gp-advanced-select/gpadvs-search-exact-match.js (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: veryspry
PR: gravitywiz/snippet-library#1077
File: gravity-forms/gw-require-alt-text-description-post-image.php:23-32
Timestamp: 2025-05-30T13:53:21.893Z
Learning: In Gravity Forms snippets, use 'gravityforms' as the translation domain for internationalization functions instead of creating custom domains, to maintain consistency with the related plugin.
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Danger JS
🔇 Additional comments (3)
gp-advanced-select/gpadvs-search-exact-match.js (3)

1-15: Excellent documentation and setup instructions.

The header documentation is comprehensive and provides clear context for the snippet's purpose, including helpful links to documentation and instruction videos. The installation instructions are clear and user-friendly.


16-23: Good implementation of the empty search case.

The logic correctly handles the case where no search query is provided by returning all items with a score of 1, which maintains the expected behavior when the search field is empty.


16-35: No action required – the gpadvs_settings hook and callback signature are correct.
Verified that gpadvs_settings is consistently used across multiple snippets (gp-advanced-select/*.js) with the same signature (function(settings, gpadvs)) and optional extra parameters (e.g. selectNamespace) when needed.

Comment on lines +24 to +32
search = search.toLowerCase();
return function(item) {
if ( item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add defensive programming for safer property access.

The exact match logic is sound, but there's a potential runtime error if item.text is undefined, null, or not a string. Consider adding a safety check before calling toLowerCase().

Apply this diff to add defensive programming:

 	search = search.toLowerCase();
 	return function(item) {
-		if ( item.text.toLowerCase() === search ) {
+		if ( item.text && typeof item.text === 'string' && item.text.toLowerCase() === search ) {
 			// High score for items matching search query exactly
 			return 1;
 		}
 		// Zero score for items not matching search query
 		return 0;
 	};

This ensures the code gracefully handles cases where item.text might be undefined, null, or not a string, preventing potential JavaScript errors.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
search = search.toLowerCase();
return function(item) {
if ( item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
search = search.toLowerCase();
return function(item) {
if ( item.text && typeof item.text === 'string' && item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
🤖 Prompt for AI Agents
In gp-advanced-select/gpadvs-search-exact-match.js around lines 24 to 32, the
code calls toLowerCase() on item.text without checking if item.text is defined
and a string, which can cause runtime errors. Add a defensive check to ensure
item.text exists and is a string before calling toLowerCase(), and handle cases
where it is not to avoid errors.

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

Successfully merging this pull request may close these issues.

1 participant